What is the best way to detect if a player is moving?

Hello,

I was just wondering what is the most efficient way to detect if a player is moving. I usually use MoveDirection.Magnitude, but I don’t know if that’s the best way to know if a player is moving.

1 Like

you can use the Humanoid.Running event which fires when a player starts running.

humanoid.Running:Connect(function(speed)

end)

The speed parameter tells you the speed they’re running at. check if it’s greater than 0 and you’re done!

16 Likes

So (me being me i’m probably not understanding at all) so you want to detect what way a characters moving,so with a locked mosue?

1 Like

Yes.

abcdefghijklmnopqrstuvwxyz

local function wfc(parent,child)
return parent:WaitForChild(child)
end

local char = character
local hum = wfc(char,"Humanoid")
local hr = wfc(char, "HumanoidRootPart")

hum.AutoRotate = false

local anims = {
["Forward"] = folder.Walk_Forward.AnimationId,
["Backward"] = folder.Walk_Backward.AnimationId,
["Left"] = folder.Walk_Left.AnimationId,
["Right"] = folder.Walk_Right.AnimationId,
["Stopped"] = "Stop"
}

local anim
local track
local OldDirection

local direc = "Stopped"
key press related movement detection
local keys = {}
--//read keys to dedect direction moving 
local rf = game.ReplicatedStorage.Events
rf.OnServerEvent:connect(function(p,args)
	local mode = args[1]
	if mode == "CharacterMovementSystem" then
		keys = args[2]
		direc = args[3]
	end
end)

char.Humanoid.Running:connect(function(s) if s>2 then
	local mov_spped = (hr.RotVelocity+hr.Velocity).magnitude
	local movement_speed = char.Humanoid.WalkSpeed
	if mov_spped > 5 then
		local Direction = anims[direc]
		local cor = coroutine.wrap(function()
			if Direction ~= OldDirection and char:FindFirstChild("Walk") then
				char.Walk:remove()
				track:Stop()
			end
		end)
		cor()
		if OldDirection ~= Direction then OldDirection = Direction end
		if Direction ~= "Stop" then
			if char:FindFirstChild("Walk") == nil then
				anim = Instance.new("Animation",char) anim.Name = "Walk"
				anim.AnimationId = Direction
				track = hum:LoadAnimation(anim)
				track:Play()
				track:AdjustSpeed(movement_speed/16)
			end
		else
			if anim ~= nil then
				track:Stop()
				anim:remove()
			end
		end
	end
end end)
9 Likes