Wax

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.

Where the numbers are

Your character has a part called ActorState. It holds the numbers.

local state = game.Character.ActorState
Read thisTo get
state.HealthYour health right now.
state.MaxHealthYour health when it is full.
state:IsAlive()true while your character is alive.
state.StaminaYour stamina.
state.FoodLevelHow well fed you are.
state.WaterLevelHow much water you have had.
state.OxygenLevelHow 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.Health

The log shows a number, for example = 300.

Show it on screen

init.lua
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

  1. ui.Overlay makes a panel that stays on screen while you play. See Overlays.
  2. hud:Progress adds a bar. hud:Field adds a name with a value. Each one gives back a control with a Set function.
  3. refresh reads the numbers and puts them into the controls.
  4. The task calls refresh four 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 refresh does. game.Character is nil at 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.

init.lua (continued)
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.

On this page