I have a tool that has an Idle animation, that plays once you are holding the tool.
The problem is i can’t make it so when you unequip the tool it stops the animation.
This is my current script, any help would be appreciated:
local players = game:GetService("Players")
local plr = players.LocalPlayer.Character
local Animation = script.Parent:WaitForChild("Animation")
script.Parent.Equipped:Connect(function()
local humanoid = script.Parent.Parent:WaitForChild("Humanoid")
local loadAnim = humanoid.Animator:LoadAnimation(Animation)
loadAnim:Play()
end)
script.Parent.Unequipped:Connect(function()
local humanoid = plr.Humanoid
local loadAnim = humanoid.Animator:LoadAnimation(Animation)
loadAnim:Stop()
end)
Additionally, i also have a different animation that plays when i click the tool which is not working.
This is the script:
local cooldown = false
local Animation = script.Parent.Swing
local plr = game.Players.LocalPlayer
script.Parent.Activated:Connect(function()
if cooldown == false then
local humanoid = plr.Character.Humanoid
local loadAnim = humanoid.Animator:LoadAnimation(Animation)
loadAnim:Play()
cooldown = true
elseif cooldown == true then
wait(2)
cooldown = false
end
end)
Output says:
LoadAnimation requires an Animation object
Even though i am clearly using an Animation Object
If you want all players to see the animation, you should load it from the server side (i think) with a script in the serverscriptservice (maybe with a remote event controlled by the tool)
local players = game:GetService("Players")
local plr = players.LocalPlayer.Character
local Animation = script.Parent:WaitForChild("Animation")
local loadAnim = nil
script.Parent.Equipped:Connect(function()
local humanoid = script.Parent.Parent:WaitForChild("Humanoid")
loadAnim = humanoid.Animator:LoadAnimation(Animation)
loadAnim:Play()
end)
script.Parent.Unequipped:Connect(function()
if loadAnim then
loadAnim:Stop()
end
end)
the other script:
local enabled = true
local Animation = script.Parent.Swing
local plr = game.Players.LocalPlayer
script.Parent.Activated:Connect(function()
if enabled then
enabled = false
local humanoid = plr.Character.Humanoid
local loadAnim = humanoid.Animator:LoadAnimation(Animation)
loadAnim:Play()
wait(2) -- delay
enabled = true
end
end)
You need to rename the animation. Rename it “SwingAnimation”, and reference it with the new name. The problem is that you have 2 things with the same name (an animation and a script), so the script thinks you mean the script names “Swing”, not the animation named “Swing”.