Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

is humanoid.MoveDirection the best way to see if a player is walking?

This seems to work fine, but I want to know if it is the most optimized way to do so.

--// Services
local repStr = game:GetService'ReplicatedStorage'

--// References
local plr = game.Players.LocalPlayer
repeat wait() until plr.Character
local char = plr.Character
local hum = char:WaitForChild'Humanoid'
local step = repStr:WaitForChild'step'

--// Variables
local timeElapsed
local lastStep = 0

while wait(.13) do
	if hum.MoveDirection ~= Vector3.new(0,0,0) then
		wait(.13)
			local elapsed = tick()-lastStep
			lastStep = tick()
			if elapsed >= .13 then
				step:FireServer()
			end
	end
end

3 comments

0

no you can use HumanoidStateType

0

Wait! Who deleted my comment???

2 answers

No, you are using an infinite loop and most of the time (if not always) they are inefficient.

Instaid of the loop you can do something like this:

hum.StateChanged:Connect(function(old, new) --will run every time a state of the humanoid changes.
	if new ~= Enum.HumanoidStateType.Running then --check if the state is running
		--stopped walking
		step:FireServer()
	else
		--started walking again
	end
end)

Hope this helped!

Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
	if Humanoid.MoveDirection ~= Vector3.new(0,0,0) then
		
	else
		
	end
end)

from what i know this is the most efficient way of detecting if the character is moving or not. it's not a loop that goes forever and the function only runs when the move direction is changed. then followed by an if that checks if you have stopped (movedirection is (0,0,0)) or you are running.

This discussion is preserved as it was. New answers, comments, and votes are closed.