Changing a players face texture for a second and then reverting it back

Hi everyone,
I’m trying to change a player’s face when damaged for like one second, and then revert it back to the original face. How can I go about doing this? This is my current local script, located in StartedCharacterScripts:

local Humanoid = script.Parent.Humanoid

local damanim = Instance.new('Animation')
damanim.AnimationId = 'rbxassetid://7399752586' 

repeat wait() until Humanoid:IsDescendantOf(workspace)
local AnimationTrack = Humanoid:LoadAnimation(damanim)
AnimationTrack.Priority = Enum.AnimationPriority.Action

local LastKnownHealth
game:GetService('RunService').RenderStepped:Connect(function()
	if LastKnownHealth then
		if LastKnownHealth > Humanoid.Health then
			print('Playing')
			AnimationTrack:Play()
			script.Parent.Head.face.Texture = "rbxassetid://2223994763"
		end
	end

	LastKnownHealth = Humanoid.Health
end)

This script changes the face just fine, I just don’t know how to revert it back.

Thanks in advance,
kitekite

1 Like

Humanoid:LoadAnimation is deprecated i should try to stop using it

What does that have to do with my problem?

you put local LastKnownHealth and you put if LastKnownHealth then wouldn’t that be nil

You should employ the use of an else statement to respectively set the face back to the default should your health condition not be equivelant to true, your answer lies within that if statement.

3 Likes

Thanks for the reply, but how would I reference the original face?

i was just saying it didn’t relate to your problem

1 Like

Oh, I’ll change it. Thanks for the reply

Let’s assign the default face to a variable and utilise that to “reset” our face upon the if statement falling to your else.

local DefaultFace = Head.face.Texture

--another place in code
Head.face.Texture = DefaultFace
2 Likes

Hmm, now the players face is the default smiling face.
Here’s my script now:

local Humanoid = script.Parent.Humanoid
local DefaultFace = script.Parent.Head.face.Texture
local damanim = Instance.new('Animation')
damanim.AnimationId = 'rbxassetid://7399752586' 

repeat wait() until Humanoid:IsDescendantOf(workspace)
local AnimationTrack = Humanoid:LoadAnimation(damanim)
AnimationTrack.Priority = Enum.AnimationPriority.Action

local LastKnownHealth
game:GetService('RunService').RenderStepped:Connect(function()
	if LastKnownHealth then
		if LastKnownHealth > Humanoid.Health then
			print('Playing')
			AnimationTrack:Play()
			script.Parent.Head.face.Texture = "rbxassetid://2223994763"
			wait(0.5)
		else
			script.Parent.Head.face.Texture = DefaultFace
		end
	end

	LastKnownHealth = Humanoid.Health
end)

This will be due to the default roblox face being on your character as soon as they spawn. Whilst the timing between spawning and your currently equipped face loading in is rather negligable, remember that scripts run exponentially swifty. To avoid this issue we can do a few things!

One way to handle this, would be to listen for :GetPropertyChangedSignal(“Texture”) on the face decal inside the head. You can then ensure that the current face’s texture (upon this event being triggered) is not equal to the texture of the face you’re setting when health is duducted and if so, set the new face texture to the default face.

The reasoning behind checking the new face texture against the texture you set when health changes is to avoid setting the “damaged” face texture as your DEFAULT texture accidently. :+1:

2 Likes

Ah, this works perfectly! Thank you so much for your help!
:smiley: :+1:

Amazing! Glad you could achieve the desired effect.

Remember to always consider implementing an else or elseif into your if statements (where it may be necessary) to handle edge cases within your implementation. Sometimes your conditions will not always equate to being true so it’s always wise to offer some sort of contingency plan to your code in case of this.

Good luck with the rest of your project! :slight_smile: :partying_face:

2 Likes