Idle sitting animation not stopping?

So I think most of the devforum has seen a game much like this: https://www.roblox.com/games/5072973528/Vibe-Room

I am trying to achieve their sitting animation system how every seat has a different animation and my first try went well until it got to the point I needed to remove or stop the animation, the animation doesn’t stop when I jump out of the seat.

The animation is set to idle in priority and is looped.

Here is the script:

local seat = script.Parent

seat.ChildAdded:Connect(function(child)
	if (child.ClassName == "Weld") then
		local human = child.part1.Parent:FindFirstChild("Humanoid")
		if human ~= nil then
			local anim = human:LoadAnimation(seat.sitanim)
			anim:Play()
		end
	end
end)

seat.ChildRemoved:Connect(function(child)
	if seat.sitanim then
		seat.sitanim:Destroy()
		wait()
		local newanim = Instance.new("Animation",seat)
		newanim.Name = "sitanim"
		newanim.AnimationId = "rbxassetid://6435284432"
	end
end)

This is the explorer:
image

This is what it looks like on a seat:
image

This is what happens when you get off:
image

It only tells me this but I don’t know what to do with it:
image

sitanim is not inside the seat… insert a animation name it sitanim and put the id in the animation

it says it right here, but ill recreate it

LoadAnimation returns an entirely separate instance called an AnimationTrack which is the instance actually playing the Animation, you should be stopping that instead of deleting the Animation itself.

1 Like

How do I get to this instance? I don’t know exactly where its located.

no point, if sitanim isn’t a child of seat, it’s going to error. Try:

if seat:FindFirstChild("sitanim") then

end

This doesn’t fix anything, sitanim is an animation located in the seat, I sent an image of the explorer panel and it clearly shows the animation in the seat.

It’s nil and it’s parent is locked, but you could have it as a variable outside of the two connections, and reference the track like you did with anim.

How would I locate it outside of the connections if it requires to find the person sitting in the chair, I think I would need to use a connection.

local seat = script.Parent
local anim

seat.ChildAdded:Connect(function(child)
	if (child.ClassName == "Weld") then
		local human = child.part1.Parent:FindFirstChild("Humanoid")
		if human ~= nil then
			anim = human:LoadAnimation(seat.sitanim)
			anim:Play()
		end
	end
end)

seat.ChildRemoved:Connect(function(child)
    anim:Stop()
    anim = nil
	--Might not be needed, but just in case
    --[[seat.sitanim:Destroy()
	wait()
	local newanim = Instance.new("Animation",seat)
	newanim.Name = "sitanim"
    newanim.AnimationId = "rbxassetid://6435284432"]]
end)

Something like this. I would have done better but I’m on mobile for now.

Okay I found a way to solve it on my own and it involved putting events in events, so thanks for helping!