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)
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)
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
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)
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?
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