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

Change face on damage?

Asked by
Aimarekin 345 Moderation Voter
6 years ago

I'm trying to make a script so that when you get hurt your face changes for a little time, but I can't get it work. The script is a script inside StarterCharacterScripts, and It's the following:

local previousface = script.Parent.Head.face.Texture
local humanoidhealth = script.Parent.Humanoid.Health

if humanoidhealth > script.Parent.Humanoid.Health then
    script.parent.Head.face.Texture = "rbxassetid://732910174"
    wait (0.5)
    script.parent.Head.face.Texture = previousface
    local humanoidhealth = script.Parent.Head.face.Texture
end

if humanoidhealth < script.Parent.Humanoid.Health then
    local humanoidhealth = script.Parent.Humanoid.Health
end

I can't get what's wrong. The first if then is for changing the face when you get hurt. The second one is for setting humanoidhealth to the humanoids health if it rehealhs.

Help!

1 answer

Log in to vote
0
Answered by
RayCurse 1518 Moderation Voter
6 years ago

Your script is checking the health of the character immediately when it begins to execute when it should be running a block of code every time the character's health changes. In order to do this, you can use the HealthChanged event of the humanoid.

local previousface = script.Parent.Head.face.Texture
local humanoid = script.Parent:WaitForChild("Humanoid")
local humanoidHealth = humanoid.Health
humanoid.HealthChanged:Connect(function(newHealth)
    if newHealth < humanoidHealth then
        script.parent.Head.face.Texture = "rbxassetid://732910174"
        wait (0.5)
        script.parent.Head.face.Texture = previousface
    end
    humanoidHealth = newHealth
end)
0
Hi! works fine! Thank you! Aimarekin 345 — 6y
0
PD: I added "and newhealth ~= 0" because I have another script that sets the face to a different one on death. Aimarekin 345 — 6y
Ad

Answer this question