Custom seat animation kind of works, but not like i want it to

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)

You could just use the occupant property of seat to use the player that’s sitting on the chair.

What will I need to add to the script to make that work? (Sorry. Again, I am very bad at scripting)

I have now tried changing various things in the script but it still doesn’t work.
I think it has something to do with this part.

function removed(child2)
	if anim ~= nil then
		anim:Stop()
		anim:Remove()

Don’t know what to change about it though.

Try thsi out perhaps

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

1 Like

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

I don’t think my script will work :confused:

That did it! I also tried @OofMayy’s script, but sadly that didn’t loop it.

The problem is solved now though. Thanks!

Anytime! If you have anymore issues don’t be afraid to make another post!

1 Like