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

When something changes do something ?

Asked by
Bulvyte 388 Moderation Voter
7 years ago
Edited 7 years ago

Idk how to explain but example if a guy loses 10 hp, everytime he loses health his screen gets red how can u do that ?

EDIT: Okay heres the script i've done

local humanoid = game.Players.LocalPlayer.Character.Humanoid
local currentHealth = humanoid.Health
local Flash = script.Parent.Flash

humanoid.HealthChanged:connect(function(health)
    local change = math.abs(currentHealth - health)
    Flash.Visible = true
    wait(0.2)
    Flash.Visible = false
    currentHealth = health
end)

But when he regenerates health it shows up too! i only want to make it show ONLY when hes losing not getting health.

1 answer

Log in to vote
1
Answered by
2eggnog 981 Moderation Voter
7 years ago
Edited 7 years ago

Use the Humanoid HealthChanged event. This even fires each time the humanoid's health has changed, with the new health as the argument. You can use this to detect how much damage a humanoid has taken.

local gui = script.Parent.Frame --The gui element
local humanoid = game.Players.LocalPlayer.Character.Humanoid
local currentHealth = humanoid.Health --Store the previous health before the change.
humanoid.HealthChanged:connect(function(health) --Every time the health changes...
    local change = health - currentHealth --Get the change in health from the stored value.
    currentHealth = health --Store the new health so we can compare it later.
    if change <= -10 then --If the humanoid lost more than 10 health...
        gui.Visible = true
        wait(.2)
        gui.Visible = false
    end
end)
0
Your script what it does is when u get HP the frame appears but i want it vice versa :P :/ Bulvyte 388 — 7y
0
Actually, wait your script doesn't even work ;-; Bulvyte 388 — 7y
0
The script is just an example. You need to edit it to fit your needs. 2eggnog 981 — 7y
Ad

Answer this question