Starting and stopping animation not working

I don’t know why, but this just isn’t working. It plays the animation and sets your speed to 0 but if you press it again it does nothing. I don’t know why its not working because it seems like everything should work but I’ve literally been on and off trying to fix this for hours and I’m finally giving in and asking for help, thank you so much!.

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local remoteEvent = ReplicatedStorage:WaitForChild("A1Crazy")

function playAnim(player, animationID)
	local humanoid = game.Workspace[player.Name].Humanoid
	local animation = Instance.new("Animation")
	animation.AnimationId = "rbxassetid://"..animationID
	local loadedAnimation = humanoid:LoadAnimation(animation)
	if loadedAnimation.IsPlaying == false then
		humanoid.WalkSpeed = 0
		loadedAnimation:Play()
	else
		loadedAnimation:Stop()
		humanoid.WalkSpeed = 16
	end
end

remoteEvent.OnServerEvent:Connect(playAnim)

This is probably why, there’s actually an easier way to reference the Player’s Character instead, where you can use local Character = player.Character

Try this and see what you get?

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local remoteEvent = ReplicatedStorage:WaitForChild("A1Crazy")

function playAnim(player, animationID)
    local Character = player.Character
    local humanoid = Character:WaitForChild("Humanoid", 3)
	local animation = Instance.new("Animation")
	animation.AnimationId = "rbxassetid://"..animationID
	local loadedAnimation = humanoid:LoadAnimation(animation)
	if loadedAnimation.IsPlaying == false then
		humanoid.WalkSpeed = 0
		loadedAnimation:Play()
	else
		loadedAnimation:Stop()
		humanoid.WalkSpeed = 16
	end
end

remoteEvent.OnServerEvent:Connect(playAnim)

Thanks, I’ll try it out and see!

Sadly, it’s doing the same thing.
On a side note, what does the 3 on this line do?

Does it wait an additional 3 seconds?

Are you getting any Outputs? And that’s just a timeout thing, if it can’t find the Humanoid after 3 seconds have passed then it’ll return back as a nil value

No, I’m not getting any outputs. No errors or anything. I also put prints in both the first and second part of the if statement and as expected it only printed the first. But, when I click the button 2 times it prints the one in the first part twice so I have a slight suspicion there’s something wrong with the if statement.

Oh this makes more sense, thanks!

Oh my… I think we were reloading the animation setting it back to not playing which makes it then play again.

Okay, while that was the problem, now there’s another issue, it just doesn’t stop the animation. They alternate, but it keeps playing.

I am pretty sure that this is because every time you fire the event, it loads the animation again so you do not get the same animation that is playing.

I instead suggest using a for loop on Humanoid:GetPlayingAnimationTracks() and do an if statement like:

for i,v in pairs(hum:GetPlayingAnimationTracks()) do
if v.Animation.Name == (YourAnimationName) then
v:Stop()
end
end

:slight_smile:

Yeah, I finally realized that. I can’t believe I wasted that long of my life on that.

Everything is fixed and it finally works! I can finally move on to the easy stuff, animating! Thank you guys so much for helping!