This is my first post on the dev forum, apologies if I’ve done anything incorrectly.
What I’m attempting to achieve
I have a tool/weapon that has an idle animation, and I’m trying to make it so that the idle animation plays whenever my character isn’t moving.
ISSUE
The idle animation plays when I equip the weapon, but after I walk or run, the idle animation doesn’t play until I re-equip the tool
Local script inside the tool is below:
local tool = script.Parent
local anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://9386775434"
local track
local players = game:GetService("Players")
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local RunService = game:GetService("RunService")
tool.Equipped:Connect(function()
track = script.Parent.Parent.Humanoid:LoadAnimation(anim)
track.Priority = Enum.AnimationPriority.Idle
track.Looped = true
track:Play()
RunService.RenderStepped:Connect(function()
if humanoid.MoveDirection.Magnitude == 0 then
if not track then
track:Play()
end
end
end)
end)
tool.Unequipped:Connect(function()
if track then
track:Stop()
end
end)
RunService.RenderStepped:Connect(function()
if humanoid.MoveDirection.Magnitude > 0 then
if track then
track:stop()
end
end
end)
RunService.RenderStepped:Connect(function()
if humanoid.MoveDirection.Magnitude > 0 then
if track and tool.Unequipped == true then
track:stop()
end
end
end)
RunService.RenderStepped:Connect(function()
if humanoid.MoveDirection.Magnitude > 0 then
if track and tool.Unequipped == true and not humanoid:GetState(Enum.HumanoidStateType.Idle) then
track:stop()
elseif humanoid.MoveDirection.Magnitude > 0 and Tool.Equipped == true then
track:Stop()
end
end
end)
I managed to fix it after a while, here’s the code:
local tool = script.Parent
local anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://9386775434"
local track
local players = game:GetService("Players")
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local RunService = game:GetService("RunService")
tool.Equipped:Connect(function()
track = script.Parent.Parent.Humanoid:LoadAnimation(anim)
track.Priority = Enum.AnimationPriority.Idle
track.Looped = true
track:Play()
end)
tool.Unequipped:Connect(function()
if track then
track:Stop()
end
end)
RunService.RenderStepped:Connect(function()
local equipped = false
tool.Equipped:Connect(function()
equipped = true
end)
tool.Unequipped:Connect(function()
equipped = false
end)
if equipped == true then
local humanoid = character:FindFirstChildOfClass("Humanoid") or character:FindFirstChildOfClass("AnimationController")
local animator = humanoid:FindFirstChildOfClass("Animator")
if humanoid.MoveDirection.Magnitude > 0 and track.IsPlaying then
track:Stop()
elseif humanoid.MoveDirection.Magnitude == 0 and tool.Equipped and not track.IsPlaying then
for i,v in ipairs(animator:GetPlayingAnimationTracks()) do
v:Stop()
end
track:Play()
end
end
end)