Bug with movement

The problem is, if I move in any direction without my camera moving, my character will use the default run instead of the one I’ve provided here.

If I move my camera even slightest bit, or switch my move direction, then it will work immediately.

I guess the important thing to note is, when I press sideway key, the magnitude is 1 as I’ve seen from the debug printing, and when I flick my camera it is 0.99999943 something. In my if method, if the magnitude is higher than 0, it should work, but for some reason the run won’t play?

Here’s a video:

And my code:

local TweenService = game:GetService("TweenService")
local Player = game.Players.LocalPlayer
local rs = game:GetService("ReplicatedStorage")
local updateAnim = rs:WaitForChild("UpdateAnimation")
local Tween

local resetWalkValue = script.resetWalkValue
local valueToRun = script.valueToRun
local maxRunValue = script.maxRunValue
local increaseTime = script.increaseTime

local jumpCooldown = 0.26 -- Cooldown in seconds
local lastJumpTime = 0 -- Variable to track the last time the player jumped

local Char = Player.Character
local Humanoid = Char:WaitForChild("Humanoid")
local HRP = Char:WaitForChild("HumanoidRootPart")

local runAnimation = script.Run
local runny = Humanoid:LoadAnimation(runAnimation)

local jumpAnimation = script.Jump
local jumpy = Humanoid:LoadAnimation(jumpAnimation)

local freefallAnimation = script.Freefall
local freefall = Humanoid:LoadAnimation(freefallAnimation)

local fallAnimation = script.Fall
local fally = Humanoid:LoadAnimation(fallAnimation)

local function reloadRunAnimation()
	local success, error_message = pcall(function()
		local NewrunAnimation = script.Run
		runny = Humanoid:LoadAnimation(NewrunAnimation)
	end)

	if not success then
		warn("Error in reloadIdleAnimation:", error_message)
	end
end


local function HandleAnimations(Char)
	local cam = workspace.CurrentCamera
	local tweenService = game:GetService("TweenService")
	-- Load animations
	runAnimation = script.Run
	runny = Humanoid:LoadAnimation(runAnimation)

	jumpAnimation = script.Jump
	jumpy = Humanoid:LoadAnimation(jumpAnimation)

	freefallAnimation = script.Freefall
	freefall = Humanoid:LoadAnimation(freefallAnimation)

	fallAnimation = script.Fall
	fally = Humanoid:LoadAnimation(fallAnimation)

	local debounce = false

	Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
		runny:AdjustSpeed(HRP.Velocity.Magnitude / 28)
		print(Humanoid.MoveDirection.Magnitude)
		if Humanoid.WalkSpeed >= valueToRun.Value and Humanoid:GetState() ~= Enum.HumanoidStateType.Jumping and Humanoid:GetState() ~= Enum.HumanoidStateType.Freefall and Humanoid:GetState() ~= Enum.HumanoidStateType.Landed then
			if Humanoid.MoveDirection.Magnitude > 0 then
				if not debounce then
					debounce = true
					runny:Play()
				end
			else
				debounce = false
				runny:Stop()
			end
		end
		if Humanoid.MoveDirection == Vector3.new(0, 0, 0) then
			print("move direction is 0")
			debounce = false  -- Reset debounce when player stops moving
			if not debounce then
				debounce = true
				local t = tweenService:Create(cam, TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), { FieldOfView = 70 })
				t:Play()
				Humanoid.WalkSpeed = resetWalkValue.Value
				runny:Stop()
				if Tween then
					Tween:Cancel()
				end
				Tween = nil
			end
		elseif not Tween then
			debounce = false
			Tween = tweenService:Create(Humanoid, TweenInfo.new(increaseTime.Value, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), { WalkSpeed = maxRunValue.Value })
			Tween:Play()
			local t = tweenService:Create(cam, TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), { FieldOfView = 80 })
			t:Play()
		end
	end)

	-- Handle jump animation
	Humanoid.Jumping:Connect(function()
		if time() - lastJumpTime > jumpCooldown then
			jumpy:Play()
			lastJumpTime = time() -- Update the last jump time
		end
	end)

	-- Handle fall animations
	Humanoid.StateChanged:Connect(function(oldState, newState)
		if newState == Enum.HumanoidStateType.Freefall and time() - lastJumpTime > jumpCooldown then
			jumpy:Stop()
			freefall:Play()
		elseif newState == Enum.HumanoidStateType.Landed then
			freefall:Stop()
			fally:Play()
		elseif newState == Enum.HumanoidStateType.Seated or newState == Enum.HumanoidStateType.PlatformStanding then
			freefall:Stop()
			fally:Stop()
		end
	end)
end

local function CharacterAdded(character)
	HandleAnimations(character)
end

-- Check if the character already exists
if Player.Character then
	CharacterAdded(Player.Character)
end

-- Listen for future character additions
Player.CharacterAdded:Connect(CharacterAdded)
updateAnim.OnClientEvent:Connect(reloadRunAnimation)

There’s also a bug when I jump and start freefalling, then the run animation will start playing after the jump is done, however when I just fall off a ledge, then the run animation will not play and freefall will play normally. Not my main thing to want a fix for, but just wanted to note, in case anyone knows how to improve or fix my code.

1 Like