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
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)
You'll need to use a while
loop. This will test if the players' health has less than the first number. Here's an example. This script would be in a localScript
inside 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