Advertisement
Quoteory

Run

Feb 28th, 2020
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.23 KB | None | 0 0
  1. local UserInputService = game:GetService("UserInputService")
  2.  
  3. local character = script.Parent
  4. local humanoid = character.Humanoid
  5.  
  6. local WALKSPEED = 15
  7. local RUNSPEED = 20
  8.  
  9. local walkAnim = humanoid:LoadAnimation(WALKANIMATION)
  10. local runAnim = humanoid:LoadAnimation(RUNANIMATION)
  11. local idleAnim =  humanoid:LoadAnimation(IDLEANIMATION)
  12.  
  13. local runKeyCode = Enum.KeyCode.LeftShift
  14.  
  15. local currentlyMoving = false -- set to true whenever move direction not 0
  16. local currentlyIdle = false -- idle state, true whenever running (idle anim playing)
  17. local currentlyRunning = false -- run state, true whenever running (running anim playing)
  18. local currentlyWalking = false -- walk state, true whenever running (walk anim playing)
  19.  
  20. local holdingShift = true
  21.  
  22. local function run()
  23.     if currentlyIdle then
  24.         currentlyIdle = false
  25.         idleAnim:Stop()
  26.     end
  27.     if currentlyWalking then
  28.         currentlyWalking = false
  29.         walkAnim:Stop()
  30.     end
  31.     currentlyRunning = true
  32.     humanoid.WalkSpeed = RUNSPEED
  33.     runAnim:Play()
  34. end
  35.  
  36. local function walk()
  37.     if currentlyIdle then
  38.         currentlyIdle = false
  39.         idleAnim:Stop()
  40.     end
  41.     if currentlyRunning then
  42.         currentlyRunning = false
  43.         runAnim:Stop()
  44.     end
  45.     currentlyWalking = true
  46.     humanoid.WalkSpeed = WALKSPEED
  47.     walkAnim:Play()
  48. end
  49.  
  50. local function idle()
  51.     if currentlyWalking then
  52.         currentlyWalking = false
  53.         walkAnim:Stop()
  54.     end
  55.     if currentlyRunning then
  56.         currentlyRunning = false
  57.         runAnim:Stop()
  58.     end
  59.     currentlyIdle = true
  60.     idleAnim:Play()
  61. end
  62.  
  63.  
  64. UserInputService.InputBegan:Connect(function(input, gameprocess)
  65.     if gameprocess or not input.KeyCode == runKeyCode then return end
  66.     holdingShift = true
  67.     if currentlyMoving then    
  68.         run()
  69.     end
  70. end)
  71.  
  72. UserInputService.InputEnded:Connect(function(input, gameprocess)
  73.     if gameprocess or not input.KeyCode == runKeyCode then return end
  74.     holdingShift = false
  75.     if currentlyMoving and currentlyRunning then
  76.         walk()
  77.     end
  78. end)
  79.  
  80. humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
  81.     if humanoid.MoveDirection.Magnitude > 0 then
  82.         if currentlyMoving then return end
  83.         currentlyMoving = true
  84.        
  85.         if holdingShift then
  86.             run()
  87.         else
  88.             walk()
  89.         end
  90.     else
  91.         currentlyMoving = false
  92.         idle()
  93.     end
  94. end)
  95.  
  96.  
  97. idle() -- default state
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement