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
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.