Check if Character is Walking, Without Any loop?

I am trying to connect a function when player starts to walk, the Humanoid.StateChanged is not reliable and i dont want to have a continuous loop running in the background.

Also the Humanoid.MoveDirection.Magnitude is not reliable when player turns left or right, the condition Humanoid.MoveDirection.Magnitude > 0 changes to false.

What else can i try??

Current Code

local player = game.Players.LocalPlayer
local Character = player.Character
local Hum = Character.Humanoid
local CurrentCamera = workspace.CurrentCamera
local PreFOV = CurrentCamera.FieldOfView

local WalkingStart = nil
local IsSprinting = false

local function ChangeFOV(Speed)
	print(Speed, workspace.CurrentCamera.FieldOfView)
	if Speed > 0 then
		if IsSprinting then return end
		IsSprinting = true
		if not WalkingStart then 
			WalkingStart = tick() 
			print("Walking started at:", WalkingStart) -- Add this line for debugging
		end
		if tick() - WalkingStart < 2 then
			print(tick()-WalkingStart)
			warn("return")
			return
		end

		print(CurrentCamera.FieldOfView)
		CurrentCamera.FieldOfView = CurrentCamera.FieldOfView + 10
		print(CurrentCamera.FieldOfView)
	elseif Hum:GetState() ~= Enum.HumanoidStateType.Running then
		IsSprinting = false
		CurrentCamera.FieldOfView = PreFOV
		WalkingStart = nil
	end
end

Hum.Running:Connect(ChangeFOV)
1 Like

MoveDirection should also work when moving left or right. This code works for me 100% of the time:

script.Parent.Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function(): ()
	local magnitude: number = script.Parent.Humanoid.MoveDirection.Magnitude
	if magnitude > 0 then
		print('moving')
	else
		print('stopped')
	end
end)
1 Like

Perfect!! Working (30charcharchar)

1 Like

Humanoid.Running was designed for this purpose.

local function onRunning(speed) --speed of humanoid
    if speed > 0 then
        --is running
    else
        --no longer running        
    end
end

humanoid.Running:Connect(onRunning)
1 Like

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