How would i script this? simple but cant seem to do it

I want to make a server sided script that constantly checks the players health, and when they take damage, run a function. Very simple, but do i use while task.wait() do and humanoid.HealthChanged while also defining the players old health? cant seem to come up with a good way to do it. Any help is appreciated

you can use task.delay or task.wait and define the old player’s health to a certain variable before running a function.

Could do something like this

local function CheckPlayerHealth(player)
    local char = player.Character or player.CharacterAdded:Wait()
    local currentHealth = char:WaitForChild("Humanoid").Health

    while wait() do
        local playerHealth = char:WaitForChild("Humanoid").Health
        if playerHealth < currentHealth then
            -- Run your function for when their health is lower
        end
        currentHealth = playerHealth
    end
end

for _, plr in pairs(game:GetService("Players"):GetPlayers()) do
    coroutine.wrap(CheckPlayerHealth)(plr)
end

i think its better to use Humanoid.HealthChanged than a loop

but how would you know the old health?

it will be the same thing, what will change its that you will be using a connection and not a loop

Yh would be a bit more optimized, but as long as it’s just a few players in the lobby it won’t be a huge deal

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.