Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Humanoid Health Changed Problem?

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

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

4 comments

0

if I do that the currentHp will be the humanoid's health after the health change xd

0

So, if that's not what you want, then what do you want?

0

^^

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.

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.

2 comments

0

Use Connect, not connect. Also, please indent the code properly. Other than that, though, this should work fine.

0

if I try to do this it won't fire the event xd

This discussion is preserved as it was. New answers, comments, and votes are closed.