O_P

StaminaScript

O_P
Sep 28th, 2024 (edited)
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local player = game.Players.LocalPlayer
  2. local character = player.Character or player.CharacterAdded:Wait()
  3. local humanoid = character:WaitForChild("Humanoid")
  4. local userInputService = game:GetService("UserInputService")
  5.  
  6.  
  7. local maxStamina = 100
  8. local currentStamina = maxStamina
  9. local isSprinting = false
  10.  
  11.  
  12. local staminaBar = script.Parent:WaitForChild("StaminaBar")
  13. local staminaText = script.Parent:WaitForChild("StaminaText")
  14.  
  15.  
  16. local function updateStamina()
  17.     staminaBar.Size = UDim2.new(currentStamina / maxStamina, 0, 1, 0)
  18.     staminaText.Text = "Stamina: " .. math.floor(currentStamina)
  19. end
  20.  
  21.  
  22. userInputService.InputBegan:Connect(function(input, gameProcessed)
  23.     if input.UserInputType == Enum.UserInputType.Touch and humanoid.MoveDirection.Magnitude > 0 then
  24.         isSprinting = true
  25.         isSprinting = true
  26.     end
  27. end)
  28.  
  29. userInputService.InputEnded:Connect(function(input)
  30.     if input.UserInputType == Enum.UserInputType.Touch then
  31.         isSprinting = false
  32.         isSprinting = false
  33.     end
  34. end)
  35.  
  36.  
  37. game:GetService("RunService").RenderStepped:Connect(function()
  38.     if humanoid.MoveDirection.Magnitude > 0 then
  39.        
  40.         if isSprinting and currentStamina > 0 then
  41.             currentStamina = math.max(0, currentStamina - 0.5)
  42.         elseif currentStamina > 0 then
  43.             currentStamina = math.max(0, currentStamina - 0.1)
  44.         end
  45.     else
  46.         currentStamina = math.min(maxStamina, currentStamina + 0.5)
  47.     end
  48.     updateStamina()
  49. end)
  50.  
  51.  
  52. updateStamina()
  53.  
Advertisement
Add Comment
Please, Sign In to add comment