How to play and stop animations

So I wanted to make it so when you press a specific key it plays an animation and pauses it then after you press that specific key again it stops the animation. But everytime I press the specifc key it ONLY plays the animation. Any help is very appreciated!
Server:

local saluteRemote = game:GetService("ReplicatedStorage"):WaitForChild("sendData")

saluteRemote.OnServerEvent:Connect(function(plr, id)
	local animation = Instance.new("Animation")
	animation.Name = "Animation".. math.random(1,101)
	animation.AnimationId = "http://www.roblox.com/asset/?id=" .. id
	animation.Parent = plr.Character.Humanoid
	local track = plr.Character.Humanoid.Animator:LoadAnimation(animation)
	track.Looped = true
	local bool = script:GetAttribute("isPlaying")
	if bool ~= true then
		track:Play()
		bool = script:SetAttribute("isPlaying", true)
		task.wait(1)
		track:AdjustSpeed(0)
	elseif bool == true then
		track:Stop()
		bool = script:SetAttribute("isPlaying", false)
	end
end)

Client:

local Context = game:GetService("ContextActionService")
local EaseId = script:GetAttribute("AtEaseId")
local SaluteId = script:GetAttribute("SaluteId")
local sendData = game:GetService("ReplicatedStorage"):WaitForChild("sendData")

local function Salute()
	sendData:FireServer(SaluteId)
end

local function AtEase()
	sendData:FireServer(EaseId)
end

Context:BindAction("Salute", Salute, true, Enum.KeyCode.E)
Context:BindAction("Stand at Ease", AtEase, true, Enum.KeyCode.F)

First of all - you don’t need to use a RemoteEvent, as playing an animation from a client will make it replicate to the server and other clients. The reason why your animations didn’t stop is the script kept creating new animation tracks, stopping those instead of the old ones. Here’s an example of the Salute function.

local function Salute(actionName, inputState)
	if inputState == Enum.UserInputState.Begin then
		local animation = plr.Character.Humanoid:FindFirstChild("Animation" .. SaluteId) or Instance.new("Animation", plr.Character.Humanoid)
		animation.Name = "Animation" .. SaluteId
		animation.AnimationId = "http://www.roblox.com/asset/?id=" .. SaluteId
		if track == nil or track.Animation ~= animation then
			if track ~= nil then
				track:Stop()
			end
			track = plr.Character.Humanoid.Animator:LoadAnimation(animation)
		end
		track.Looped = true
		if track.IsPlaying ~= true then
			track:Play()
		elseif track.IsPlaying == true then
			track:Stop()
		end
	end
end

What have changed: The script checks if the InputState is Begin, so it won’t get called after you stop pressing the button. Animation instances are now named after the id, so the humanoid won’t get cluttered with animations. Animation declaration searches if it exists, if it does it defines itself as the found one, else it creates a fresh instance. There is a check where if the track is nil or isn’t the current playing one, if it isn’t nil it stops animating, after that it loads the track with the animation. And finally, instead of relying on an attribute the script checks if the track is playing, and Plays/Stops the animation track.
Hope this helps.

2 Likes

Just a side note, the looping might not replicate:

1 Like

One question. I’m having a hard time trying to figure out where to put this. If you could quote where I should put it would be very appreciated! :+1:

The code which I gave should replace the Salute function which exists in your local script.

I actually had to just add the plr variable. Thanks for all your help!

1 Like