The animation won't stop working

The animation won’t stop working

I want to play the animation repeatedly when the player clicks the mouse

I want to stop animation when I don’t click the mouse What should I do


local tool =script.Parent

local function Equipped()
	

script.Parent.Activated:Connect(function()

		
		local anim = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Animation)
		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 0
		anim:Play()
	
	
end)
	
	
end


local function UnEquipped()
	

local anim = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Animation)
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16
	anim:Stop()
	
	
end

tool.Equipped:Connect(Equipped)
tool.Unequipped:Connect(UnEquipped)

robloxapp-20230327-0025537.wmv (1.1 MB)

1 Like

In UnEquipped() you are loading an entirely seperate animation and trying to stop that one instead of the one which is playing.

You should just load a single animation and just stop and start that when needed.

Also, please try formatting your script before posting it to the devforum to make reading it easier. (This wasn’t that long so it’s not like it took more than a few seconds but for more complex ones it’s really appreciated).

local Tool = script.Parent
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")

local Animation = Humanoid:LoadAnimation(script.Parent.Animation)

function Equipped()
	script.Parent.Activated:Connect(function()
		Humanoid.WalkSpeed = 0
		Animation:Play()
	end)
end


function UnEquipped()
	Humanoid.WalkSpeed = 16
	Animation:Stop()
end

Tool.Equipped:Connect(Equipped)
Tool.Unequipped:Connect(UnEquipped)

Strangely, if you pull the animation out of the function, it doesn’t work

What is script.Parent?

Because inside of Equipped() I have .Activated wrapped inside of the equip aswell. I wasn’t sure if it was necessary or not so I just copied it over. Try removing it and just replacing it with:

function Equipped()
		Humanoid.WalkSpeed = 0
		Animation:Play()
end

Nothing’s going on…

local Tool = script.Parent
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")

local Animation = Humanoid:LoadAnimation(script.Parent.Animation)

function Equipped()
	
		Humanoid.WalkSpeed = 0
		Animation:Play()
	
end


function UnEquipped()
	Humanoid.WalkSpeed = 16
	Animation:Stop()
end

Tool.Equipped:Connect(Equipped)
Tool.Unequipped:Connect(UnEquipped)

robloxapp-20230327-0057468.wmv (638.2 KB)

Wow I feel a bit dumb for omitting this.
If the tool is in the starter pack you should add a Player.CharacterAdded:Wait() so that it doesn’t just result in it not finding anything.

local Tool = script.Parent
local Player = game.Players.LocalPlayer
Player.CharacterAdded:Wait() -- here
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")

local Animation = Humanoid:LoadAnimation(script.Parent.Animation)

function Equipped()
	
		Humanoid.WalkSpeed = 0
		Animation:Play()
	
end


function UnEquipped()
	Humanoid.WalkSpeed = 16
	Animation:Stop()
end

Tool.Equipped:Connect(Equipped)
Tool.Unequipped:Connect(UnEquipped)

If it STILL doesn’t work, then I have no idea because I just tried the one above and it worked completely fine. Maybe check if your animation instance if the fix above doesn’t work?

1 Like

Did you try this?

local humanoid = char:FindFirstChildOfClass("Humanoid") or char:FindFirstChildOfClass("AnimationController")
local animator = humanoid:FindFirstChildOfClass("Animator")
for i,v in ipairs(animator:GetPlayingAnimationTracks()) do
	v:Stop()
end

It works very, very perfectly thank you!!

local Animation = Humanoid.Animator:LoadAnimation(script.Parent.Animation)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.