StUF is a little goofy - that code is safe to use in any frame, the "player" reference in the second half is to deal with StUF's config mode.
StUF can fake stuff on it's own really well - if you're using the tags that StUF provides.
I'll go line by line through the code, so you can see what's going on.
Code:
function(unit, cache, textframe)
if UnitExists(unit) then
local perchp = 100 * UnitHealth(unit)/UnitHealthMax(unit)
return "%.1f%%",perchp
else
local perchp = 100 * UnitHealth("player")/UnitHealthMax("player")
return "%.1f%%",perchp
end
end
Code:
function(unit, cache, textframe)
function header required by StUF Custom Lua
Code:
if UnitExists(unit) then
if there's actually such a unit (target, party, etc) do the next chunk (in this case, the next 2 lines)
Code:
local perchp = 100 * UnitHealth(unit)/UnitHealthMax(unit)
make a local variable called "perchp", and set it equal to their current percentage of HP.
Code:
return "%.1f%%",perchp
tell StUF to show the current HP percentage, with 1 decimal place, and a % sign after it
refers to the 2nd line - if Unit does NOT exist, do the next chunk (next 2 lines)
Code:
local perchp = 100 * UnitHealth("player")/UnitHealthMax("player")
make that local variable perchp, set it to the player's health percentage (why? so StUF config mode doesn't bug out)
Code:
return "%.1f%%",perchp
return the player's health percentage, formatted the same as earlier
closes the If-Then-Else decision
closes the function