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

How I can print this? :

I want to print "Health Reduced" when a player loses health. For example: A player has 60% of health and then a zombie hits him so his current health now is 40%. How I can print "Health Reduced" at the moment he was hurt without mattering if his health is not 100%?

This works only if his health is 100 only

if Humanoid.Health < Humanoid.MaxHealth then
print "Health Reduced"
end

3 answers

✓ Accepted answer

Using both of these people's answers this is the least complicated way to do this in a Local Script:

local plyr = game:GetService("Players").LocalPlayer

plyr.CharacterAdded:connect(function(char)
	local h = char:WaitForChild("Humanoid")
	h.Changed:connect()
		if h.Health < h.MaxHealth then
			print"Health Reduced"
		end
	end
end

Hope it helps!(BTW I was the first one to say "Hope it helps!" dsboy stole from me! :O)

1 comment

0

Thanks LordDragon :)

You'll need to use a whileloop. This will test if the players' health has less than the first number. Here's an example. This script would be in a localScriptinside StarterGui.

while wait() do
oldhealth=game.Players.LocalPlayer.Character.Humanoid.Health
if game.Players.LocalPlayer.Character.Humanoid.Health<oldhealth then
print ("Health Reduced")
end
end

Hope this helped! -ds

In order to do this, we need to let the script know each time the player takes damage. We'll do this with the HealthChanged() event:

game.Players.PlayerAdded:connect(function(Plr)
	game.Players.CharacterAdded:connect(function()
		local Humanoid=Plr.Character:FindFirstChild("Humanoid")
		local Health=100 --Sets previous health
		Humanoid.HealthChanged:connect(function(NewHealth) --Fires when health changes
			if NewHealth<Health then --Checks if health is higher or lower than before
				print("Health Reduced!")
			end
			Health=NewHealth --Sets the previous health to the new health.
		end)
	end)
end)

Whenever the players health changes, this checks if it's lower or higher than the previous health, and if it's lower, it prints "Health Reduced!"


Anyways, hope this helped you a bit. if you have any further questions/problems, please leave a comment below. Hope I helped :P

3 comments

1

Although this will work, there may have been a simpler way to test for this, such as using localPlayer.

0

Also this'd only work once, what if the player dies? This won't work on a second respawn, so I advise that instead of the repeat loop, you use the CharacterAdded event instead.

0

Ah, ok thanks for pointing that out

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