I currently want to make an idle animation, when the player isn’t moving and has the tool equipped the idle animation would play. The problem is that when a player is moving the idle animation still plays.
local tool = script.Parent.Parent
local IdleAnim = script.Parent.Idle
local loadAnim
tool.Equipped:Connect(function()
local Char = tool.Parent
local animator = Char:WaitForChild("Humanoid"):WaitForChild("Animator")
loadAnim = animator:LoadAnimation(IdleAnim)
loadAnim:Play()
end)
tool.Unequipped:Connect(function()
loadAnim:Stop()
end)
NOTE: not tested, probably won’t work, just use this script as a reference.
local tool = script.Parent.Parent
local IdleAnim = script.Parent.Idle
local loadAnim
tool.Equipped:Connect(function()
local Char = tool.Parent
Char.Humanoid.Running:Connect(function(speed)
if speed <= 0 then
local animator = Char:WaitForChild("Humanoid"):WaitForChild("Animator")
loadAnim = animator:LoadAnimation(IdleAnim)
loadAnim:Play()
else
loadAnim:Stop()
end
end)
end)
tool.Unequipped:Connect(function()
loadAnim:Stop()
end)
The major problem with the script was that even if the tool isn’t equipped but the player is idle the idle animation would still play. This updated version fixed it, there is still a slight delay but I think that is roblox’s running function’s fault.
local tool = script.Parent.Parent
local IdleAnim = script.Parent.Idle
local loadAnim
local Equipped = false
tool.Equipped:Connect(function()
local Char = tool.Parent
Equipped = true
local animator = Char:WaitForChild("Humanoid"):WaitForChild("Animator")
loadAnim = animator:LoadAnimation(IdleAnim)
loadAnim:Play()
Char.Humanoid.Running:Connect(function(speed)
if Equipped == false then return end
if speed <= 0 then
loadAnim:Play()
else
loadAnim:Stop()
end
end)
end)
tool.Unequipped:Connect(function()
Equipped = false
loadAnim:Stop()
end)