Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

made a script that should print something when ever player has taken damage (dosen't work?)

Asked by 5 years ago

So i made this script that will (for now) print something when player has taken damage. But it wont work! why?

local hum = game.Players.LocalPlayer.Character.Humanoid
local currHealth = hum.Health

hum.HealthChanged:connect(function(h)
    if h.Health == h.Health -5 then
print "damage dealed"

    end
end)
0
its that deprecated Connect you used. DinozCreates 1070 — 5y
0
No it isn't - deprecated doesn't mean "Broken". fredfishy 833 — 5y
0
i thought it meant "it was a joke". DinozCreates 1070 — 5y

2 answers

Log in to vote
1
Answered by
pidgey 548 Moderation Voter
5 years ago

Humanoid.Health is the current health of the humanoid. Humanoid.Health - 5 is the current health of the humanoid subtracted by 5. The humanoid's health is always equal to itself, therefore, your if statement will never go through.

This is code from https://developer.roblox.com/api-reference/event/Humanoid/HealthChanged

local humanoid = game.Players.LocalPlayer.Character.Humanoid
local currentHealth = humanoid.Health
humanoid.HealthChanged:Connect(function(health)
    local change = currentHealth - health
    print("The humanoid's health "..(currentHealth > health and "decreased by " or "increased by ")..change..".")
    currentHealth = health
end)

If you want to to check if 5 damage was taken to the humanoid, simply check if (currentHealth - health) == 5

0
Comparing a value with the same value will always equal to itself. It's redundant. pidgey 548 — 5y
Ad
Log in to vote
0
Answered by
fredfishy 833 Moderation Voter
5 years ago

You're comparing a number to itself minus 5, rather than comparing your previously saved current health.

Also, don't use == when dealing with decimal values. It will only work if the decimal values are exactly the same to every decimal point, and due to the way computers work with decimals, they probably won't be.

local hum = game.Players.LocalPlayer.Character.Humanoid
local currHealth = hum.Health

hum.HealthChanged:connect(function(h)
    -- Need to compare to currHealth, and need to use < rather than ==
    if h.Health < currHealth -5 then
        print("damage dealed")
        -- update current health for next check
        currHealth = h.Health
    end
end)

Answer this question