Stopping an Animation

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

  1. What do you want to achieve? Stop an animation when a player touches a brick.

  2. What is the issue? Despite having no errors showing up in output, the animation fails to come to a stop.

  3. What solutions have you tried so far? There was a similar post to mine, but it didn’t work either. Can’t Stop Animation

The script pasted below is what I have. I would also like to add that the animation that I am trying to stop is Looped and that this is under a Server Script, if that happens to change anything.

local part = script.Parent

local function onTouched(other)
	local humanoid = other.Parent:FindFirstChildWhichIsA("Humanoid")
	if humanoid then
		local animation = part:FindFirstChild("Animation")
		if animation then
			animation:Stop()
		end
	end
end

part.Touched:Connect(onTouched)
1 Like

I’m not sure if this is right but you might have defined “animation” wrongly. I think you have to do Humanoid:LoadAnimationTrack(Insert animation instance here), and then do :Stop() on that

3 Likes
local part = script.Parent

local function onTouched(other)
	local humanoid = other.Parent:FindFirstChildWhichIsA("Humanoid")
	if humanoid then
		local animation = humanoid:LoadAnimation(script.Parent.Animation)
		if animation then
			animation:Stop()
		end
	end
end

part.Touched:Connect(onTouched)

Despite using :LoadAnimation(), the animation still doesn’t stop.

humanoid.animator:LoadAnimation

3 Likes

Your suggestion still didn’t work.

1 Like

Sorry I’m not sure then, maybe someone else can figure it out…

1 Like

Is the animation looped? Try setting Animation.Looped to false

You can only call :Stop() on the animation if you have saved the animation track somewhere such as a variable in the script that plays it.

If the animation is not looped you could do:

local Player = game.Players.LocalPlayer
local AnimationTracks = Player.Character.Humanoid:GetPlayingAnimationTracks()
for i, v in pairs(AnimationTracks) do
	v:Stop()
end

You need to load an animation to the humanoid to use :Stop()
Here is some basic code that explains what I am trying to say:

local Character = game.Players.LocalPlayer.Character
local Humanoid = Character.Humanoid
local Animation = script.Animation -- The animation

local AnimationTrack = Humanoid:LoadAnimation(Animation) -- This is where you load the animation on the humanoid.
AnimationTrack:Stop()

The code shown above was made using a local script because it is easier to stop or play an animation in a local script than a server script. (from personal experience)

What I would do with your code is launch a RemoteEvent from your server script that leads to a local script and that local script will disable the animation.

For example:

(Server Script)

local part = script.Parent
local RemoteEvent = game.ReplicatedStorage.RemoteEvent -- doesn't have to be in the replicated storage or be named 'RemoteEvent'

local function onTouched(other)
	local humanoid = other.Parent:FindFirstChildWhichIsA("Humanoid")
	if humanoid then
		local animation = part:FindFirstChild("Animation")
		if animation then
			RemoteEvent:FireClient(humanoid) -- :FireClient() requires the local player property in order to fire.
		end
	end
end

part.Touched:Connect(onTouched)

Local Script placed inside of the part:

local Part = script.Parent
local Animation = Part:FindFirstChild("Animation")
local RemoteEvent = game.ReplicatedStorage.RemoteEvent

RemoteEvent.OnClientEvent:Connect(function()
    local AnimationTrack = Part:LoadAnimation(Animation)
    AnimationTrack:Stop()
end)

I assumed that you wanted to stop an animation for a part with the script you provided, but if not you can change the variables to make it stop an animation for another thing. Hopefully this helps, I know this isnt the most efficient way of doing this but it gets the job done.

There’s an error in the Server Script saying that the player argument must be a Player object.

1 Like

When you fire the remote event in a server script, you need to get the property of the player.

Try this

local part = script.Parent
local RemoteEvent = game.ReplicatedStorage.RemoteEvent -- doesn't have to be in the replicated storage or be named 'RemoteEvent'

local function onTouched(other)
	local humanoid = other.Parent:FindFirstChildWhichIsA("Humanoid")
	if humanoid then
		local animation = part:FindFirstChild("Animation")
		if animation then
			RemoteEvent:FireClient(other.Parent) -- instead if using 'humanoid' use other.Parent
		end
	end
end

part.Touched:Connect(onTouched)