This script is to make a custom seat animation and to change the face of the player while he is sitting, but if the player leaves the chair, the animation doesn’t go away.
local isPlaying = false
local OldFace = nil
function SeatAnim(Child)
local Humanoid = Child.part1.Parent.Humanoid
local Face = Humanoid.Parent.Head.face
local Animation = script.Parent.SeatAnimation
local LoadedAnimation = Humanoid:LoadAnimation(Animation)
if not isPlaying then
print("Animation loaded and played")
OldFace = ""..Face.Texture
Face.Texture = "http://www.roblox.com/asset/?id=161124757"
LoadedAnimation:Play()
isPlaying = true
else
print("animation stopped")
Face.Texture = OldFace
LoadedAnimation:Stop()
LoadedAnimation:Remove()
isPlaying = false
end
end
script.Parent.ChildAdded:Connect(SeatAnim)
script.Parent.ChildRemoved:Connect(SeatAnim)
1 Like
You’re telling the script to stop playing the seat animation when isPlaying = true, which isn’t efficient. Instead, you should make two separate functions for the ChildAdded/ChildRemoved functions.
Like so:
local OldFace = nil
local LoadedAnimation
function onChildAdded(Child)
local Animator = Child.part1.Parent.Humanoid.Animator
local Face = Humanoid.Parent.Head.face
local Animation = script.Parent.SeatAnimation
LoadedAnimation = Animator:LoadAnimation(Animation)
print("Animation loaded and played")
OldFace = ""..Face.Texture
Face.Texture = "http://www.roblox.com/asset/?id=161124757"
LoadedAnimation:Play()
end
function onChildRemoved(Child)
local Face = Humanoid.Parent.Head.face
print("animation stopped")
Face.Texture = OldFace
LoadedAnimation:Stop()
LoadedAnimation:Remove()
end
script.Parent.ChildAdded:Connect(onChildAdded)
script.Parent.ChildRemoved:Connect(onChildRemoved)
Also, just a friendly reminder. Using the Humanoid to load animations was deprecated, so instead you should use the Animator inside the Humanoid (which I already fixed in the script).
2 Likes
Thank you for helping me and also thank you for the information about the Animator, I didn’t know that.
1 Like