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

Humanoid Health Changed Problem?

Asked by
R_ngar 10
5 years ago
local player = game.Players.LocalPlayer
local humanoid = player.Character.Humanoid
local currentHp = humanoid.Health 
humanoid.HealthChanged:connect(function(health)
    print(currentHp)
    if health < currentHp then
    script.Parent:WaitForChild("RemoteEvent"):FireServer(health)
    end
end)

Whenever I use this script the currentHp printed is 100. Is there any way to fix this?

2 answers

Log in to vote
1
Answered by 5 years ago

Try putting the line local currentHp = humanoid.Health inside the function.

0
if I do that the currentHp will be the humanoid's health after the health change xd R_ngar 10 — 5y
0
So, if that's not what you want, then what do you want? SirDerpyHerp 262 — 5y
0
^^ LollipopCraft 1 — 5y
0
like I am doing a rpg game with a defense stat. Each time you are hurt you gain a defense point. I want to do it so you can only fire the event when you are hurt not healed. I am a noob in scripting so I don't know how xd. R_ngar 10 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
local player = game.Players.LocalPlayer
local humanoid = player.Character.Humanoid
humanoid.HealthChanged:connect(function(health)
local currentHp = humanoid.Health   
  print(currentHp)
    if health < currentHp then
    script.Parent:WaitForChild("RemoteEvent"):FireServer(health)
    end
end)

Always be sure that you check what the health value is after the humanoid takes damage. The way you had it, the script would only check once it ran and never again.

0
Use Connect, not connect. Also, please indent the code properly. Other than that, though, this should work fine. lunatic5 409 — 5y
0
if I try to do this it won't fire the event xd R_ngar 10 — 5y

Answer this question