I have this sprint script that is mostly functional, although I'm struggling to make the stamina accessible by other scripts
what I'm thinking of doing is making the globalstamina.value mirror the stamina value and vice versa without causing too much server lag, but idk much about doing that (I'm trying to learn lua for the first time)
local ContextActionService = game:GetService("ContextActionService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
local gui = localPlayer.PlayerGui:WaitForChild("StaminaGUI")
local staminaBar = gui:WaitForChild("StaminaFrame"):WaitForChild("StaminaBar")
local isMoving = false
local UserInputState = Enum.UserInputState
local SPRINT_ACTION = "ShiftKey"
local character = script.Parent.Parent
local humanoid = character:WaitForChild("Humanoid")
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local DownTime = script.Parent.DownTime
local walkSpeed = script.Parent.WalkSpeed
local sprintSpeed = script.Parent.SprintSpeed
local gainRate = script.Parent.GainRate
local drainRate = script.Parent.DrainRate
local IsExhausted = script.Parent.IsExhausted
local Stamina = script.Parent.MaxStamina.Value
local maxStamina = script.Parent.MaxStamina
local isSprinting = script.Parent.IsSprinting
local GlobalStamina = script.Parent.Stamina
local function sprintInput(actionName, inputState, inputObj)
if inputState == UserInputState.Begin then
humanoid.WalkSpeed = sprintSpeed.Value
isSprinting.Value = true
elseif inputState == UserInputState.End then
humanoid.WalkSpeed = walkSpeed.Value
isSprinting.Value = false
end
end
ContextActionService:BindAction(
SPRINT_ACTION,
sprintInput,
false,
Enum.KeyCode.LeftShift,
Enum.KeyCode.RightShift
)
humanoid.Running:Connect(function(speed)
if speed > 0.5 then
isMoving = true
else
isMoving = false
end
end)
local function Exhausted()
local elapsedTime = 0
humanoidRootPart.Anchored = true
while IsExhausted.Value do
local dt = task.wait()
elapsedTime += dt
if elapsedTime >= DownTime.Value then
IsExhausted.Value = false
Stamina = maxStamina.Value
end
end
humanoidRootPart.Anchored = false
end
RunService.RenderStepped:Connect(function(deltaTime)
-- God will 100% bring this shit back to me at the pearly gates
if IsExhausted.Value then return end
staminaBar.Size = UDim2.new(Stamina / maxStamina.Value, 0, 1, 0)
if isSprinting.Value == true then
if isMoving then
Stamina -= drainRate.Value * deltaTime
else
Stamina += gainRate.Value * deltaTime
end
else
Stamina += gainRate.Value * deltaTime
end
Stamina = math.clamp(Stamina, 0, maxStamina.Value)
if Stamina <= 0 then
IsExhausted.Value = true
Exhausted()
end
end)
** You are now Level 1! **