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.
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.
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.
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.