Animation not stopping when a event is fired

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? I’m trying to stop my animation when a event fires and knows the humanoid is no longer seated

  2. What is the issue? Include screenshots / videos if possible! The animation keeps playing even though the humanoid sit property is false

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub? I tried to debug it

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

-- Animation
local BuildAnimation = game.ServerStorage.BuildAnimation
local Build_Animation_Id = "rbxassetid://9073531192"
print("Past line 4")
-- Seat
local Seat = game.Workspace.SeatModel.SeatPart.Seat
print("past lien 7")
-- Getting Players
local Player = game:GetService("Players")
-- getting player character
print("past line 11")
Player.PlayerAdded:Connect(function(Player)
	print("past line 13")
	Player.CharacterAdded:Connect(function(Character)
		print("Past line 15")
		-- Getting Humanoid
		local Humanoid = Character:WaitForChild("Humanoid")
		Humanoid.Seated:Connect(function(Seated, Part)
			local BuildAnimation = Humanoid:LoadAnimation(BuildAnimation)
			if Seated then
				BuildAnimation:Play()
				print("past line 20")
				-- Play Animation
			else
				if not Seated then
				BuildAnimation:Stop()
				-- Do not play the animation
			end
		end
	end)
	end)
end)

So I’m having a bug in my game where when a player is no longer sitting the animation won’t stop why is this happening?

Each time the player sits down, you load an animation track and call :Stop() on the new track, rather than the already playing one.

You can keep a reference to the one animation track outside of the Seated event listener, next to your humanoid reference declaration. Try that and see if it works.

Another thing, you might want to execute this code locally instead, as all character related animations should be handled on the client.

1 Like
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Animator = Humanoid:WaitForChild("Animator")
local Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://9073531192"
Animation.Parent = script
local Track = Animator:LoadAnimation(Animation)

Humanoid.Seated:Connect(function(Seated, SeatPart)
	if Seated then
		Track:Play()
	elseif not Seated then
		Track:Stop()
	end
end)

Implementing the suggestions which have been made, local script.

@C_Sharper
Finally! This is where I personally was having my issues and I was stuck on it for longer than I’d like to admit. I knew it was simple and something about placement.
Thank you for your post! My issue is fixed now

In my case someone else is traveling the same road, I’ll write it more simply in code to another understanding and at least this was also how my issue was set up.

Problem.



bla.bla.bla:Connect(function(result)

local myAnimation = humanoid:LoadAnimation(myAnimationLocation) --/!!/  NO!

		if result == "true" then
            --/!!/  Is going to play a whole new LoadAnimation thread you defined inside the function
			myAnimation:Play()
		else
			myAnimation:Stop()
            --/!!/  Won't stop because you defined a whole new LoadAnimation
		end
end)

Fix.

local myAnimation = humanoid:LoadAnimation(myAnimationLocation) --/!!/  YES!
    
bla.bla.bla:Connect(function(result)
	
	--/delete/  local myAnimation = humanoid:LoadAnimation(myAnimationLocation)
		
		if result == "true" then
			myAnimation:Play()
		else
			myAnimation:Stop()
		end
end)
1 Like