So basically when I equip the gun tool and I sprint and jump and unequip then it keeps doing the walking and idle animation.
here is my code:
script.Parent.Equipped:Connect(function()
local character = script.Parent.Parent
local humanoid = character:FindFirstChild("Humanoid")
if not humanoid then return end
-- Load animations
local idle = humanoid:LoadAnimation(script.Idle)
local walk = humanoid:LoadAnimation(script.Walk)
local sprint = humanoid:LoadAnimation(script.Sprint) -- Sprint animation
local jump = humanoid:LoadAnimation(script.Jump) -- Jump animation
idle:Play()
-- State variables
local equipped = true
local walking = false
local sprinting = false
local jumping = false
-- Monitor jumping
humanoid.StateChanged:Connect(function(_, newState)
if newState == Enum.HumanoidStateType.Jumping then
if not jumping then
jumping = true
jump:Play()
idle:Stop()
walk:Stop()
sprint:Stop()
end
elseif newState == Enum.HumanoidStateType.Landed then
if jumping then
jumping = false
jump:Stop()
if humanoid.MoveDirection.Magnitude > 0 then
if humanoid.WalkSpeed >= 14 then
sprint:Play()
sprinting = true
else
walk:Play()
walking = true
end
else
idle:Play()
end
end
end
end)
local function disableAnimations()
idle:Stop()
walk:Stop()
sprint:Stop()
jump:Stop()
print("Disabled All Animations")
end
-- Coroutine to handle walking and sprinting
coroutine.wrap(function()
while equipped do
wait(0.1)
if not jumping then
if humanoid.MoveDirection.Magnitude > 0 then
if humanoid.WalkSpeed == 22 then
if not sprinting then
sprinting = true
walking = false
idle:Stop()
walk:Stop()
sprint:Play()
end
else
if not walking then
walking = true
sprinting = false
idle:Stop()
sprint:Stop()
walk:Play()
end
end
else
if walking or sprinting then
walking = false
sprinting = false
walk:Stop()
sprint:Stop()
idle:Play()
end
end
end
end
end)()
-- Unequipped event
script.Parent.Unequipped:Once(function()
disableAnimations()
sprinting = false
walking = false
jumping = false
end)
end)```
Any way I could fix this?