Advertisement
Velinquish

Coroutines Solution to Running faster and faster

Apr 16th, 2020
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.92 KB | None | 0 0
  1. --[[
  2.     This script uses coroutines and pauses acceleration and resets when the humanoid stops moivng.
  3. --]]
  4.  
  5. local player = game.Players.LocalPlayer
  6. local hum = player.Character:WaitForChild("Humanoid")
  7.  
  8. local WALK_SPEED = 16 -- Beginning walk speed
  9. local INCREASE = 0.5 -- How much it accelerates each time
  10. local INTERVAL = 0.1
  11.  
  12. local isRunning = false -- Whether the humanoid is running
  13.  
  14. local accelerate = coroutine.create(function() 
  15.     while wait(INTERVAL) do
  16.         if hum.MoveDirection == Vector3.new() then -- if the humanoid is not moving
  17.             isRunning = false
  18.             hum.WalkSpeed = WALK_SPEED -- reset walk speed
  19.             coroutine.yield() -- Pause until the player starts running again.
  20.         end
  21.         hum.WalkSpeed = hum.WalkSpeed + INCREASE
  22.     end
  23. end)
  24.  
  25. hum.Running:Connect(function(speed)
  26.     if speed > 0 and not isRunning then -- When the player is just starting to run
  27.         isRunning = true
  28.         coroutine.resume(accelerate)
  29.     end
  30. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement