Hello Developers! I have made an custom seat brick, but when I use it, the animation only plays once. Is there a way to fix this?
Don’t mind the audio, I always have radio on when using studio lol
.
.
This is the script. I can’t figure out the problem myself, since I’m not that good of a scripter.
seat = script.Parent
function added(child)
if (child.className=="Weld") then
human = child.part1.Parent:FindFirstChild("Humanoid")
if human ~= nil then
anim = human:LoadAnimation(seat.sitanim)
anim:Play()
end
end
end
function removed(child2)
if anim ~= nil then
anim:Stop()
anim:Remove()
end
end
seat.ChildAdded:connect(added)
seat.ChildRemoved:connect(removed)
local seat = script.Parent
local anim = nil
seat:GetPropertyChangedSignal("Occupant"):Connect(function()
local human = seat.Occupant
if human then -- If there's a humanoid
local animator = human:FindFirstChild("Animator")
anim = animator:LoadAnimation(seat.sitanim)
anim:Play() --Load it in them and play it
else
anim:Stop() --Stop the animation
anim:Destroy() --Destroy it
anim = nil --Set to nil
end
end)
Using the Occupant property is way better to determine if a player is sitting on your seat, when a palyer or any NPC is sitting on your chair, Occupant gets changed to the Humanoid of the Character, but if they get off, it’s set to nil.
What this does is if someone is sitting, it sets the anim variable to the animation to paly for that character and plays it, but if there’s no occupant, aka, they got up, stop the animation, destroy it, and set it to nil
Edit: Wait hang on the issue is not that. The issue is the animation isn’t set to Looped, you forgot to make it loop
Loop the animation.
Using the animation editor. (in studio → plugin → animation editor
or you can loop the animation in the script too
try it
seat = script.Parent
function added(child)
if (child.className=="Weld") then
human = child.part1.Parent:FindFirstChild("Humanoid")
if human ~= nil then
anim = human:LoadAnimation(seat.sitanim)
anim.Looped = true
anim:Play()
end
end
end
function removed(child2)
if anim ~= nil then
anim:Stop()
anim:Remove()
end
end
seat.ChildAdded:connect(added)
seat.ChildRemoved:connect(removed)
Like @OofMayy said, you can loop the animation in the editor.
Or use this script I made:
local AmTime = 0.5 --Time it takes for the animation to run
local Player = --Player Siting on seat
local animation = --The Humanoid LoadAnimation() Variadle
while Player.Character.Humanoid.Sit == true do
wait(AmTime)
animation:Play()
end
if Player.Character.Humanoid.Sit == false then
animation:Stop()
end