Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 6 years ago

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
0
no you can use HumanoidStateType Is_Hunter 152 — 6y
0
Wait! Who deleted my comment??? mixgingengerina10 223 — 6y

2 answers

Log in to vote
0
Answered by
jaschutte 324 Moderation Voter
6 years ago

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!

Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago
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.

Answer this question