So I was making a script that when you equip the tool :
Plays Idle Animation when idling
Plays Walking Animation when walking
The problem is that when I unequip the tool, the animations are still playing.
You can test the script by yourself to see how to looks in-game.
Code
-- Variables
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Tool = script.Parent
local Equipped = script.Parent:WaitForChild("Equipped_")
local IdleAnimationID = 9969328857
local IdleAnimation = Instance.new("Animation")
IdleAnimation.AnimationId = "rbxassetid://"..IdleAnimationID
local IdleAnimationTrack = Player.Character:WaitForChild("Humanoid"):LoadAnimation(IdleAnimation)
local IdleAnimationSpeed = 0.25
local WalkAnimationID = 9969492200
local WalkAnimation = Instance.new("Animation")
WalkAnimation.AnimationId = "rbxassetid://"..WalkAnimationID
local WalkAnimationTrack = Player.Character:WaitForChild("Humanoid"):LoadAnimation(WalkAnimation)
----------------------------------------------------------------
-- Code
local function PlayIdle()
if IdleAnimationTrack then
IdleAnimationTrack.Priority = Enum.AnimationPriority.Action
IdleAnimationTrack.Looped = true
IdleAnimationTrack:Play()
IdleAnimationTrack:AdjustSpeed(IdleAnimationSpeed)
end
end
local function StopIdle()
if IdleAnimationTrack then
IdleAnimationTrack:Stop()
end
end
local function StopWalk()
if WalkAnimationTrack then
WalkAnimationTrack:Stop()
end
end
local function PlayWalk()
if WalkAnimationTrack then
WalkAnimationTrack.Priority = Enum.AnimationPriority.Movement
WalkAnimationTrack.Looped = true
WalkAnimationTrack:Play()
end
end
local function StopAllAnimations()
wait(0.1)
if IdleAnimationTrack and WalkAnimationTrack then
IdleAnimationTrack:Stop()
WalkAnimationTrack:Stop()
end
end
local function WalkHandler()
while wait() do
if Humanoid.MoveDirection ~= Vector3.new(0,0,0) then
StopIdle()
PlayWalk()
end
if Humanoid.MoveDirection == Vector3.new(0,0,0) then
PlayIdle()
StopWalk()
end
if Equipped.Value == false then break end
end
end
Tool.Equipped:Connect(WalkHandler)
Tool.Unequipped:Connect(StopIdle)
Tool.Unequipped:Connect(StopWalk)
Tool.Unequipped:Connect(StopAllAnimations)
Thanks for your help.
It now works, but it still doesn’t stop the animations when unequipped.
Any ideas why?
Current script :
local function PlayIdle()
if IdleAnimationTrack then
IdleAnimationTrack.Priority = Enum.AnimationPriority.Action
IdleAnimationTrack.Looped = true
IdleAnimationTrack:Play()
IdleAnimationTrack:AdjustSpeed(IdleAnimationSpeed)
end
end
local function StopIdle()
if IdleAnimationTrack then
IdleAnimationTrack:Stop()
end
end
local function StopWalk()
if WalkAnimationTrack then
WalkAnimationTrack:Stop()
end
end
local function PlayWalk()
if WalkAnimationTrack then
WalkAnimationTrack.Priority = Enum.AnimationPriority.Movement
WalkAnimationTrack.Looped = true
WalkAnimationTrack:Play()
end
end
local function WalkHandler()
while wait() do
if Humanoid.MoveDirection ~= Vector3.new(0,0,0) then
StopIdle()
PlayWalk()
end
if Humanoid.MoveDirection == Vector3.new(0,0,0) then
PlayIdle()
StopWalk()
end
while true do
if Equipped.Value == false then break end
end
end
end
local function StopAllAnimations()
wait(0.1)
if IdleAnimationTrack and WalkAnimationTrack then
IdleAnimationTrack:Stop()
WalkAnimationTrack:Stop()
end
end
Tool.Equipped:Connect(WalkHandler)
Tool.Unequipped:Connect(function()
StopIdle()
StopWalk()
StopAllAnimations()
end)
you are making same mistake, you did the endless loop again.
please dont use
while true do
end
without understanding how it would work.
Anyways it should look like this
local function WalkHandler()
while wait() do
if Humanoid.MoveDirection ~= Vector3.new(0,0,0) then
StopIdle()
PlayWalk()
end
if Humanoid.MoveDirection == Vector3.new(0,0,0) then
PlayIdle()
StopWalk()
end
if Equipped.Value == false then break end
end
end
local Enumeration = Enum
local Script = script
local Tool = Script.Parent
local IdleAnimation = Instance.new("Animation")
IdleAnimation.AnimationId = "rbxassetid://9969328857"
local IdleTrack
local WalkAnimation = Instance.new("Animation")
WalkAnimation.AnimationId = "rbxassetid://9969492200"
local WalkTrack
local Connection
local function OnHumanoidMoveDirectionChanged(Humanoid, Override)
local Magnitude = Humanoid.MoveDirection.Magnitude
if Override then
IdleTrack:Stop()
WalkTrack:Stop()
elseif Magnitude > 0 then
IdleTrack:Stop()
WalkTrack:Play()
else
WalkTrack:Stop()
IdleTrack:Play()
IdleTrack:AdjustSpeed(0.25)
end
end
local function LoadAnimations()
local Character = Tool.Parent
if not Character then return end
local Humanoid = Character:FindFirstChildOfClass("Humanoid")
if (not Humanoid) or Humanoid.Health <= 0 then return end
if not (IdleTrack and WalkTrack) then
local Animator = Humanoid:FindFirstChildOfClass("Animator")
if not Animator then return end
IdleTrack = Animator:LoadAnimation(IdleAnimation)
IdleTrack.Looped = true
IdleTrack.Priority = Enumeration.AnimationPriority.Action
WalkTrack = Animator:LoadAnimation(WalkAnimation)
WalkTrack.Looped = true
WalkTrack.Priority = Enumeration.AnimationPriority.Action
end
return Humanoid
end
local function OnEquipped()
if Connection then return end
local Humanoid = LoadAnimations()
if not Humanoid then return end
Connection = Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function() OnHumanoidMoveDirectionChanged(Humanoid) end)
end
local function OnUnequipped()
if Connection then Connection:Disconnect() end
OnHumanoidMoveDirectionChanged(nil, true)
end
Tool.Equipped:Connect(OnEquipped)
Tool.Unequipped:Connect(OnUnequipped)