How to stop a tool animation after the tool is transferred to someone else?

Hi, I want to make a ball that upon touch is transferred to another player, hence the original player will lose the ball from it’s inventory and the player that touched it will keep the ball.

I’ve made the ball and animation work, but I cannot seem to stop the animation on the original player after the ball is transferred to someone else

I’ve made use of a bindable event (named StopAnimation) that gets fired every time the ball is touched, and thus transferred to someone else. Via a Serverscript I then try to stop the animation by doing animationtrack:Stop(). But this doesn’t seem to do the trick.

Here’s the code to transfer the ball and fire the bindeable event upon touch:

local tool = script.Parent					--Tool variable
local debounce = true						--Debounce (to make only 1 touch each 5 seconds)

local stopanimation = game:GetService("ReplicatedStorage"):WaitForChild("StopAnimation")

tool.Handle.Touched:Connect(function(Hand)
	if Hand.Parent:FindFirstChild("Humanoid") and debounce then 
		debounce = false

		local humanoid = tool.Parent:FindFirstChild("Humanoid") -- check if it's a player by checking if it has a humanoid
		local Animation = script.Parent:FindFirstChild("Animation")
		local animationtrack = humanoid:LoadAnimation(Animation)
		
		if humanoid then
			tool.Parent = Hand.Parent
			stopanimation:Fire(humanoid,Animation)
		end
		wait(5)
		debounce = true
	end
end)

Here’s the code to play the animation when the ball is equipped:

local tool = script.Parent
local Animation = script.Parent:FindFirstChild("Animation")

tool.Equipped:Connect(function()
	local character = tool.Parent
	local humanoid = character.Humanoid
	local animationtrack = humanoid:LoadAnimation(Animation)
	animationtrack:Play()
	
end)

And finally here is the serverscript to stop the animation:

local stopanimation = game:GetService("ReplicatedStorage"):WaitForChild("StopAnimation")

stopanimation.Event:Connect(function(humanoid,animation)
	print("stopanimation fired")
	local animationtrack = humanoid:LoadAnimation(animation)	
	animationtrack:Stop()
end)

I am very new to scripting and any help, tutorial suggestions or feedback would therefor be greatly appreciated.
I’ll leave the tool I’ve made here: Ball animation test.rbxm (4.5 KB)

Thanks in advance!

Your code doesn’t do what you think it does. The StopAnimation event creates a new AnimationTrack from the animation data it is given (LoadAnimation is called) and then said new AnimationTrack is stopped. It doesn’t stop the original AnimationTrack that was played.

Does Unequipped not work for you here? You can include your animation as an upvalue. You can define and play the animation when the tool is equipped and stop it when it is unequipped.

1 Like

Yeah I suspected that would be the case, I am very new to scripting but that was my main suspicion too. Unequipped doesn’t work for me as the original player with the ball doesn’t actually enequip the ball, but the ball is given to someone else, So I am kinda stuck there too.

If anyone has a solution for this, it’s still not solved. :frowning:

Any tutorial, tips and tricks would be greatly appreciated

This would probably help you.

1 Like

I’ll check it out, thank you! I’ve done a quick check at it and it is probably a solution to my problem!

1 Like

I got time to look into it, it instantly worked, thanks alot!

1 Like