Example: player stats
Read your health, stamina, food, water and oxygen, and show them on the screen.
This page builds a small overlay that shows your character's health, stamina, food, water and oxygen. It uses the game tree, an overlay and a task.
Wax 0.3.0 has a shorter way to the same numbers. It is at the end of this page.
Where the numbers are
Your character has a part called ActorState. It holds the numbers.
local state = game.Character.ActorState| Read this | To get |
|---|---|
state.Health | Your health right now. |
state.MaxHealth | Your health when it is full. |
state:IsAlive() | true while your character is alive. |
state.Stamina | Your stamina. |
state.FoodLevel | How well fed you are. |
state.WaterLevel | How much water you have had. |
state.OxygenLevel | How much oxygen you have. |
All of them are whole numbers, except IsAlive(), which gives true or false.
Try it in the command bar
Load into a prospect. Open the Log page of the Wax panel and type this into the command bar:
game.Character.ActorState.HealthThe log shows a number, for example = 300.
Show it on screen
local hud = ui.Overlay({ title = "Vitals", anchor = "top-left", width = 260 })
local health = hud:Progress("Health", 0, { color = ui.Theme().bad })
local alive = hud:Field("Alive", "")
local stamina = hud:Field("Stamina", "", { mono = true })
local food = hud:Field("Food", "", { mono = true })
local water = hud:Field("Water", "", { mono = true })
local oxygen = hud:Field("Oxygen", "", { mono = true })
local function refresh()
-- There is a character with these numbers only inside a prospect.
local character = game.InProspect and game.Character
if not character then
hud:SetVisible(false)
return
end
hud:SetVisible(true)
local state = character.ActorState
if state.MaxHealth > 0 then
health:Set(state.Health / state.MaxHealth)
end
alive:Set(state:IsAlive() and "yes" or "no")
stamina:Set(state.Stamina)
food:Set(state.FoodLevel)
water:Set(state.WaterLevel)
oxygen:Set(state.OxygenLevel)
end
task.spawn(function()
while true do
refresh()
task.wait(0.25)
end
end)How it works
ui.Overlaymakes a panel that stays on screen while you play. See Overlays.hud:Progressadds a bar.hud:Fieldadds a name with a value. Each one gives back a control with aSetfunction.refreshreads the numbers and puts them into the controls.- The task calls
refreshfour times a second.task.wait(0.25)pauses the task, not the game. See Tasks.
Do these two things in your own mods too:
- Ask for the character every time, as
refreshdoes.game.Characterisnilat the main menu, and it is a new object after every map change. Never keep it for long. - Give a progress bar a value from 0 to 1. Here health is divided by the full health.
Warn when health is low
Add this below the code above. It shows one warning each time your health drops under a quarter.
local warned = false
task.spawn(function()
while true do
local character = game.InProspect and game.Character
if character then
local state = character.ActorState
local low = state.MaxHealth > 0 and state.Health / state.MaxHealth < 0.25
if low and not warned then
ui.Notify("Your health is low.", { title = "Careful", kind = "warn" })
end
warned = low
end
task.wait(1)
end
end)warned remembers that the message was shown. Without it, the message would come back every second.
The same with game.Me
game.Me is your character by name. It has the numbers as fields of their own, and
signals that fire when they change. So the overlay needs no loop, and no check for the main menu or a new
map: game.Me finds the character each time, and its signals stay connected.
local hud = ui.Overlay({ title = "Vitals", anchor = "top-left", width = 260 })
local health = hud:Progress("Health", 0, { color = ui.Theme().bad })
local stamina = hud:Field("Stamina", "", { mono = true })
local food = hud:Field("Food", "", { mono = true })
local water = hud:Field("Water", "", { mono = true })
local oxygen = hud:Field("Oxygen", "", { mono = true })
local function refresh()
local me = game.Me
hud:SetVisible(me.Exists)
if not me.Exists then return end
if (me.MaxHealth or 0) > 0 then
health:Set(me.Health / me.MaxHealth)
end
stamina:Set(me.Stamina)
food:Set(me.Food)
water:Set(me.Water)
oxygen:Set(me.Oxygen)
end
refresh()
for _, signal in ipairs({
game.Me.HealthChanged, game.Me.StaminaChanged, game.Me.FoodChanged, game.Me.WaterChanged,
game.Me.OxygenChanged, game.Me.Spawned, game.Me.Despawned,
}) do
signal:Connect(refresh)
end
game.Me.HealthChanged:Connect(function(now, before)
local low = (game.Me.MaxHealth or 0) * 0.25
if now < low and before >= low then
ui.Notify("Your health is low.", { title = "Careful", kind = "warn" })
end
end)ActorState still works. game.Me is the shorter way to the numbers.