CCCPaste

Stamina System Roblox Studio

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local stamina = 100
local maxStamina = 100
local staminaDrain = 1
local staminaRegen = 0.5 
local isSprinting = false
local normalspeed = 16
local sprintspeed = 32

local staminaGui = player.PlayerGui:WaitForChild("StaminaGui")
local staminaBackground = staminaGui:WaitForChild("StaminaBackground") 
local staminaFill = staminaBackground:WaitForChild("StaminaFill")

staminaFill.Size = UDim2.new(stamina / maxStamina, 0, 1, 0)

local function regenerateStamina()
	if stamina < maxStamina then
		stamina = math.min(stamina + staminaRegen, maxStamina)
		staminaFill.Size = UDim2.new(stamina / maxStamina, 0, 1, 0)
	end
end

local function toggleSprint()
	if stamina > 0 and humanoid.WalkSpeed > 0 then
		isSprinting = true
		humanoid.WalkSpeed = sprintspeed
	else
		isSprinting = false
		humanoid.WalkSpeed = normalspeed
	end
end

local function onInputBegan(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.LeftShift then
			if stamina > 0 and humanoid.WalkSpeed > 0 then
				toggleSprint()
			end
		end
	end
end

local function onInputEnded(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.LeftShift then
			isSprinting = false
			humanoid.WalkSpeed = normalspeed
		end
	end
end

game:GetService("UserInputService").InputBegan:Connect(onInputBegan)
game:GetService("UserInputService").InputEnded:Connect(onInputEnded)

game:GetService("RunService").Heartbeat:Connect(function()
	if isSprinting and stamina > 0 then
		stamina = math.max(stamina - staminaDrain, 0)
		staminaFill.Size = UDim2.new(stamina / maxStamina, 0, 1, 0)
	else
		regenerateStamina()
	end

	if stamina == 0 and isSprinting then
		isSprinting = false
		humanoid.WalkSpeed = normalspeed
	end
end)