Advertisement
CatalinPog

Stam

Apr 13th, 2025 (edited)
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.34 KB | Gaming | 0 0
  1. local playerSettings = {
  2.     SprintSpeed = 22,
  3.     WalkSpeed = 16,        
  4.     MaxStamina = 100,      
  5.     DrainRate = 20,        
  6.     RegenRate = 15,        
  7.     RegenDelay = 1,        
  8.     JumpCost = 25,
  9.     SprintKey = Enum.KeyCode.LeftShift
  10. }
  11.  
  12. local Players = game:GetService("Players")
  13. local RunService = game:GetService("RunService")
  14. local UserInputService = game:GetService("UserInputService")
  15.  
  16. local player = Players.LocalPlayer
  17. local character = player.Character or player.CharacterAdded:Wait()
  18. local humanoid = character:WaitForChild("Humanoid")
  19.  
  20. local currentStamina = playerSettings.MaxStamina
  21. local isSprinting = false
  22. local lastStaminaUse = tick()
  23.  
  24. ------------------------------------------------------------
  25. -- GUI Setup
  26. ------------------------------------------------------------
  27. local screenGui = Instance.new("ScreenGui")
  28. screenGui.Name = "StaminaGUI"
  29. screenGui.Parent = player:WaitForChild("PlayerGui")
  30.  
  31. local frame = Instance.new("Frame", screenGui)
  32. frame.Size = UDim2.new(0, 250, 0, 30)
  33. frame.Position = UDim2.new(0.5, -125, 0.9, 0)
  34. frame.BackgroundColor3 = Color3.fromRGB(50, 50, 50)
  35. frame.BorderSizePixel = 0
  36.  
  37. local bar = Instance.new("Frame", frame)
  38. bar.Size = UDim2.new(1, 0, 1, 0)
  39. bar.BackgroundColor3 = Color3.fromRGB(0, 255, 0)
  40. bar.BorderSizePixel = 0
  41.  
  42. local staminaText = Instance.new("TextLabel", frame)
  43. staminaText.Size = UDim2.new(1, 0, 1, 0)
  44. staminaText.BackgroundTransparency = 1
  45. staminaText.TextScaled = true
  46. staminaText.TextColor3 = Color3.new(1, 1, 1)
  47. staminaText.Font = Enum.Font.SourceSansBold
  48. staminaText.Text = currentStamina .. "/" .. playerSettings.MaxStamina
  49.  
  50. local function updateGUI()
  51.     bar.Size = UDim2.new(currentStamina / playerSettings.MaxStamina, 0, 1, 0)
  52.     staminaText.Text = math.floor(currentStamina) .. "/" .. playerSettings.MaxStamina
  53.  
  54.     -- Change bar color based on stamina level
  55.     if currentStamina < playerSettings.MaxStamina * 0.3 then
  56.         bar.BackgroundColor3 = Color3.fromRGB(255, 0, 0)
  57.     elseif currentStamina < playerSettings.MaxStamina * 0.6 then
  58.         bar.BackgroundColor3 = Color3.fromRGB(255, 255, 0)
  59.     else
  60.         bar.BackgroundColor3 = Color3.fromRGB(0, 255, 0)
  61.     end
  62. end
  63.  
  64. ------------------------------------------------------------
  65. -- Sprinting & Stamina Mechanics
  66. ------------------------------------------------------------
  67. UserInputService.InputBegan:Connect(function(input, gameProcessed)
  68.     if gameProcessed then return end
  69.     if input.KeyCode == playerSettings.SprintKey then
  70.         if currentStamina > 0 then
  71.             isSprinting = true
  72.             humanoid.WalkSpeed = playerSettings.SprintSpeed
  73.         end
  74.     end
  75. end)
  76.  
  77. UserInputService.InputEnded:Connect(function(input, gameProcessed)
  78.     if gameProcessed then return end
  79.     if input.KeyCode == playerSettings.SprintKey then
  80.         isSprinting = false
  81.         humanoid.WalkSpeed = playerSettings.WalkSpeed
  82.     end
  83. end)
  84.  
  85. -- Deduct stamina when jumping; prevent jump if insufficient stamina
  86. humanoid.Jumping:Connect(function(isActive)
  87.     if isActive then
  88.         if currentStamina >= playerSettings.JumpCost then
  89.             currentStamina = currentStamina - playerSettings.JumpCost
  90.             lastStaminaUse = tick()
  91.         else
  92.             humanoid.Jump = false
  93.         end
  94.     end
  95. end)
  96.  
  97. RunService.Heartbeat:Connect(function(deltaTime)
  98.     if isSprinting then
  99.         if currentStamina > 0 then
  100.             currentStamina = math.max(0, currentStamina - playerSettings.DrainRate * deltaTime)
  101.             lastStaminaUse = tick()
  102.             if currentStamina == 0 then
  103.                 isSprinting = false
  104.                 humanoid.WalkSpeed = playerSettings.WalkSpeed
  105.             end
  106.         else
  107.             isSprinting = false
  108.             humanoid.WalkSpeed = playerSettings.WalkSpeed
  109.         end
  110.     elseif currentStamina < playerSettings.MaxStamina and tick() - lastStaminaUse > playerSettings.RegenDelay then
  111.         currentStamina = math.min(playerSettings.MaxStamina, currentStamina + playerSettings.RegenRate * deltaTime)
  112.     end
  113.  
  114.     updateGUI()
  115. end)
  116.  
  117. player.CharacterAdded:Connect(function(newCharacter)
  118.     character = newCharacter
  119.     humanoid = character:WaitForChild("Humanoid")
  120.     currentStamina = playerSettings.MaxStamina
  121.     isSprinting = false
  122.     humanoid.WalkSpeed = playerSettings.WalkSpeed
  123. end)
  124.  
  125. print("Stamina system loaded.")
  126.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement