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

How I can print this? :

Asked by 8 years ago

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

Log in to vote
1
Answered by 8 years ago

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)

0
Thanks LordDragon :) oby11omi 5 — 8y
Ad
Log in to vote
1
Answered by 8 years ago

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

Log in to vote
0
Answered by
dyler3 1510 Moderation Voter
8 years ago

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

1
Although this will work, there may have been a simpler way to test for this, such as using localPlayer. ChemicalHex 979 — 8y
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. DragonODeath 50 — 8y
0
Ah, ok thanks for pointing that out dyler3 1510 — 8y

Answer this question