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

Change face on damage?

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

✓ Accepted answer

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)

2 comments

0

Hi! works fine! Thank you!

0

PD: I added "and newhealth ~= 0" because I have another script that sets the face to a different one on death.

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