Advertisement
XiByteGam123457890

Shift To Run + Stamina Bar

Jan 18th, 2025
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.21 KB | Source Code | 0 0
  1. ----- SHIFT TO RUN SCRIPT WITH STAMINA ------
  2. -- Server Script
  3.  
  4. local realSprintScript = script:FindFirstChild("RealSprintScript")
  5.  
  6. if realSprintScript and game:GetService("StarterGui") then
  7.     local clonedScript = realSprintScript:Clone()
  8.     clonedScript.Parent = game:GetService("StarterGui")
  9. end
  10.  
  11.  
  12. -- Local Script
  13.  
  14. repeat wait() until game.Players.LocalPlayer
  15.  
  16. local player = game.Players.LocalPlayer
  17. local character = player.Character or player.CharacterAdded:Wait()
  18. local humanoid = character:WaitForChild("Humanoid")
  19. local UserInputService = game:GetService("UserInputService")
  20. local VRService = game:GetService("VRService")
  21.  
  22. local walkSpeed = 16
  23. local runSpeed = 24
  24.  
  25. local maxStamina = 100
  26. local currentStamina = maxStamina
  27. local staminaDrain = 10
  28. local staminaRegen = 5
  29.  
  30. local function createStaminaBar()
  31.     local screenGui = Instance.new("ScreenGui")
  32.     local frame = Instance.new("Frame")
  33.     local bar = Instance.new("Frame")
  34.     local frameUICorner = Instance.new("UICorner")
  35.     local barUICorner = Instance.new("UICorner")
  36.  
  37.     screenGui.Name = "StaminaBarGui"
  38.     screenGui.Parent = player:WaitForChild("PlayerGui")
  39.  
  40.     frame.Name = "StaminaFrame"
  41.     frame.Size = UDim2.new(0, 200, 0, 25)
  42.     frame.Position = UDim2.new(0, 10, 1, -35)
  43.     frame.BackgroundColor3 = Color3.new(0, 0, 0)
  44.     frame.BorderSizePixel = 0
  45.     frame.Parent = screenGui
  46.  
  47.     frameUICorner.CornerRadius = UDim.new(0, 10)
  48.     frameUICorner.Parent = frame
  49.  
  50.     bar.Name = "StaminaBar"
  51.     bar.Size = UDim2.new(1, -4, 1, -4)
  52.     bar.Position = UDim2.new(0, 2, 0, 2)
  53.     bar.BackgroundColor3 = Color3.new(0, 0.8, 0)
  54.     bar.BorderSizePixel = 0
  55.     bar.Parent = frame
  56.  
  57.     barUICorner.CornerRadius = UDim.new(0, 8)
  58.     barUICorner.Parent = bar
  59.  
  60.     return bar
  61. end
  62.  
  63. local staminaBar = createStaminaBar()
  64.  
  65. local function updateStamina(deltaTime)
  66.     if humanoid.WalkSpeed == runSpeed then
  67.         currentStamina = math.max(0, currentStamina - staminaDrain * deltaTime)
  68.     else
  69.         currentStamina = math.min(maxStamina, currentStamina + staminaRegen * deltaTime)
  70.     end
  71.  
  72.     staminaBar.Size = UDim2.new(currentStamina / maxStamina, -4, 1, -4)
  73.  
  74.     if currentStamina <= 0 then
  75.         humanoid.WalkSpeed = walkSpeed
  76.     end
  77. end
  78.  
  79. local function setRunSpeed(isRunning)
  80.     if isRunning and currentStamina > 0 then
  81.         humanoid.WalkSpeed = runSpeed
  82.     else
  83.         humanoid.WalkSpeed = walkSpeed
  84.     end
  85. end
  86.  
  87. local function createRunButton()
  88.     local screenGui = Instance.new("ScreenGui")
  89.     local button = Instance.new("TextButton")
  90.  
  91.     screenGui.Name = "RunButtonGui"
  92.     screenGui.Parent = player:WaitForChild("PlayerGui")
  93.  
  94.     button.Name = "RunButton"
  95.     button.Text = "Sprint"
  96.     button.TextColor3 = Color3.new(1, 1, 1)
  97.     button.BackgroundColor3 = Color3.new(0, 0.5, 1)
  98.     button.Size = UDim2.new(0, 150, 0, 50)
  99.     button.Position = UDim2.new(0.85, -75, 0.5, -25)
  100.     button.Font = Enum.Font.SourceSansBold
  101.     button.TextSize = 24
  102.     button.BorderSizePixel = 0
  103.     button.Parent = screenGui
  104.  
  105.     local uiCorner = Instance.new("UICorner")
  106.     uiCorner.CornerRadius = UDim.new(0, 12)
  107.     uiCorner.Parent = button
  108.  
  109.     local isRunning = false
  110.  
  111.     button.MouseButton1Down:Connect(function()
  112.         isRunning = true
  113.         setRunSpeed(isRunning)
  114.     end)
  115.  
  116.     button.MouseButton1Up:Connect(function()
  117.         isRunning = false
  118.         setRunSpeed(isRunning)
  119.     end)
  120. end
  121.  
  122. if UserInputService.TouchEnabled then
  123.     createRunButton()
  124. end
  125.  
  126. UserInputService.InputBegan:Connect(function(input, gameProcessed)
  127.     if gameProcessed then return end
  128.  
  129.     if input.UserInputType == Enum.UserInputType.Keyboard then
  130.         if input.KeyCode == Enum.KeyCode.LeftShift or input.KeyCode == Enum.KeyCode.RightShift then
  131.             setRunSpeed(true)
  132.         end
  133.     elseif input.UserInputType == Enum.UserInputType.Gamepad1 then
  134.         if input.KeyCode == Enum.KeyCode.ButtonL3 then
  135.             setRunSpeed(true)
  136.         end
  137.     end
  138. end)
  139.  
  140. UserInputService.InputEnded:Connect(function(input, gameProcessed)
  141.     if gameProcessed then return end
  142.  
  143.     if input.UserInputType == Enum.UserInputType.Keyboard then
  144.         if input.KeyCode == Enum.KeyCode.LeftShift or input.KeyCode == Enum.KeyCode.RightShift then
  145.             setRunSpeed(false)
  146.         end
  147.     elseif input.UserInputType == Enum.UserInputType.Gamepad1 then
  148.         if input.KeyCode == Enum.KeyCode.ButtonL3 then
  149.             setRunSpeed(false)
  150.         end
  151.     end
  152. end)
  153.  
  154. game:GetService("RunService").RenderStepped:Connect(updateStamina)
  155.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement