How to detect if a player is moving

Hi devforum! I’m trying to make a script where if the player isn’t moving, it return ends. like if not moving then return end. Problem is, I can’t figure out how to go about it.

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

local function Something()
	if Humanoid.MoveDirection.Magnitude == 0 then --Not moving.
		--Do code.
	else
		return
	end
end

You would essentially put the if conditional statement wherever in the function the check should be performed.

2 Likes

this technically wont work. You never call the function!!!

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

local function Something()
	if Humanoid.MoveDirection.Magnitude == 0 then --Not moving.
		--Do code.
	else
		return
	end
end
Humanoid.Changed:Connect(Something)
2 Likes

That’s fairly obvious, the thread’s poster was just asking how you would check if a humanoid is moving while a function is executing, hence I provided an example.

1 Like

Thank you for the help, I appreciate it.