Character's walking animation keeps playing when idle

Hello! My character’s walk animation plays when I unequip a weapon while walking. There is no problem when I stop with the tool, but when changing on or off when walking it bugs out.

-- Cancel all animations
local function STOPALL()
	if DashAnim then
		DashAnim:Stop()
	end
	if WalkAnim then
		WalkAnim:Stop()
	end
	if RunAnim then
		RunAnim:Stop()
	end
	if IdleAnim then
		IdleAnim:Stop()
	end
end


-- Idle animation
local function IDLE()
	IsIdle = true
	IsWalking, IsRunning = false, false
	STOPALL()
	if IdleAnim then
		IdleAnim:Play()
	end
	local revertFOVTween = game.TweenService:Create(camera, TweenInfo.new(0.3), { FieldOfView = originalFOV })
	revertFOVTween:Play()
end

-- Walking animation
local function WALKING()
	IsWalking = true
	IsIdle, IsRunning = false, false
	STOPALL()
	WalkAnim:Play()
end

-- Running animation
local function RUNNING()
	IsRunning = true
	IsWalking, IsIdle = false, false
	STOPALL()
	if RunAnim then
		RunAnim:Play()
	end
end


hum.Running:Connect(function(speed, gp)
	if gp then return end
	if hum.MoveDirection.Magnitude == 0 then
		IDLE()
	end
	if hum.MoveDirection.Magnitude > 0 and IsInAir == false and not IsRunning then
		hum.WalkSpeed = normspeed
		WALKING()
	end
end)


-- Character tool check
char.ChildAdded:Connect(function(tool)
	if tool:IsA("Tool") then
		IsTool = true
		IdleAnim:Stop()
		LoadAnimations(tool)
		if hum.MoveDirection.Magnitude == 0 then
			IDLE()
		end
	end
end)

char.ChildRemoved:Connect(function(tool)
	if tool:IsA("Tool") then
		IsTool = false
		IdleAnim:Stop()
		LoadAnimations()
		if hum.MoveDirection.Magnitude == 0 then
			IDLE()
		end
	end
end)
2 Likes

This is because Roblox doesn’t like easy. Humanoid.Running only applies when the player is walking with wasd, and if you stand still, it stops being called. Sometimes, it could even forgot to stop the animation.

The best thing you could do is make a loop with Humanoid.MoveDirection instead. If the Vector is not equal to 0,0,0 they’re moving.

3 Likes

It’s less likely it is easy that is the factor but more like legacy code lying in wait within the Humanoid’s functionalities. It just seems not so intuitive that it is hard to predict. Usually they don’t factor in the Humanoid.Running on their animation scripts, from the last time I have touched them.

1 Like

I’m new to coding, how would that look like?

change:
hum.Running:Connect(function(speed, gp)
to
game:GetService("RunService").Heartbeat:Connect(function()
and remove if gp then return end

if that doesn’t work:
send a screenshot of the LoadAnimations() function

problem might be

if hum.MoveDirection.Magnitude == 0 then
			IDLE()
		end

only checks if the player is idle when the weapon is unequipped not when player stops walking after player unequips.

2 Likes

@Zunevi

this, mark this as the solution.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.