Repeat function when player is walking

Im making a game where i need the player to be able to train their movement speed by walking, but i don’t know how could i detect if the player is moving. I need it to add +1 movement speed every second while the player is moving. Maybe like check the players velocity?

Any help will be appreciated!

Just do this and check it under a while loop that has a wait of 1 second:

local IsWalking = Humanoid.MoveDirection.Magnitude > 0

while true do
if not IsWalking then continue end

--Code

 task.wait(1)
end

2 Likes

hmm this gives script timeout. I dont know if im just using it wrong

Did u put the wait inside of the while true?

Try this:

local IsWalking = Humanoid.MoveDirection.Magnitude > 0

while task.wait(1) do
if not IsWalking then continue end

--Code

end

You can also use the humanoid.Running event to change a variable as so.

local isRunning = false

Humanoid.Running:Connect(function(speed)
      if speed <= 0 then isRunning = false return end
      isRunning = true
end)

while task.wait(1) do
      if isRunning then
          -- code
     end
end

However, you would need to re-establish this event if the players character were to respawn, so you can put the event under a CharacterAdded event aswell.

This works nice and easy to use! Thank you.

This didnt work neither, but thanks for trying to help

i think its because where he put the variable, it only gets assigned once. he would have had to put the condition inside the loop so it can be checked each time

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.