How would I add a Stamina Bar to my dash script?

Hello there!
Recently, I’ve been working on a Dashing script to use if I ever needed one in games, and, also as practice.
I wanted to implement a Stamina Bar similar to Ultrakill’s.
Then I realized, my small brain doesn’t know how to code one in.
I’m a beginner scripter so I’m not familiar with the things I need and need to frequently visit the documentation for help.

Here’s the script.

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local UserInputService = game:GetService("UserInputService")
local Humanoid = Character:WaitForChild("Humanoid")
local HRP = Character:WaitForChild("HumanoidRootPart")
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:WaitForChild("ReplicatedStorage")


local AbletoDash = true
local Time = 0.5
local Multiplier = 130
local StaminaValue = ReplicatedStorage:WaitForChild("StaminaValue").Value



UserInputService.InputBegan:Connect(function(Input, gameProcessedEvent)
	if Input.KeyCode == Enum.KeyCode.LeftShift then
		if gameProcessedEvent then return end
		if StaminaValue ~= 0 then
					if AbletoDash then
						AbletoDash = false
						Character.HumanoidRootPart.Velocity = Character.HumanoidRootPart.CFrame.lookVector*Multiplier Vector3.new(0,20,0)
						StaminaValue = StaminaValue - 1
						task.wait(Time)
						AbletoDash = true
			elseif StaminaValue ~= 1 then
				if Humanoid.FloorMaterial == Enum.Material.Air then
				AbletoDash = false
				Character.HumanoidRootPart.Velocity = Character.HumanoidRootPart.CFrame.lookVector*Multiplier Vector3.new(0,20,0)
				StaminaValue = StaminaValue - 2
				task.wait(Time)
					AbletoDash = true

						return
				end
			end
		end
	end
end)

local function AddStamina()
	if StaminaValue < 3 then
		StaminaValue = StaminaValue + 1
	end
end

while task.wait(1.5) do
	print(StaminaValue)
	AddStamina()
end

If possible, please put some sort of step-by-step instructions. Even if it’s just a sentence or phrase.

Any amount of help is appreciated.

The best way to do this would be to use TweenService on a bar UI.
For the bar you’d do something like this:


image
The “Bar” is what we’ll use to show the amount of stamina used/remaining.

As for the code you’d need to do something like this:

local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")

local Bar = script.Parent.Stamina.Bar -- Route this to wherever your UI is and such
local StaminaTime = 5 -- Set this to how long they can run/sprint for
local StaminaRefreshTim = 10 -- Set this to how long it takes to refill stamina

local TweenBarEmpty = TweenService:Create(Bar, TweenInfo.new(StaminaTime, Enum.EasingStyle.Linear), {Size = UDim2.fromScale(0,1)})
local RefillBar = TweenService:Create(Bar, TweenInfo.new(StaminaRefreshTim, Enum.EasingStyle.Linear), {Size = UDim2.fromScale(1,1)})


UserInputService.InputBegan:Connect(function(key, gameProcessed)
	if key.KeyCode == Enum.KeyCode.LeftShift and not gameProcessed then
		game.Players.LocalPlayer.Character:FindFirstChild("Humanoid").WalkSpeed = 50
		RefillBar:Pause()
		TweenBarEmpty:Play()
	end
end)

UserInputService.InputEnded:Connect(function(key, gameProcessed)
	if key.KeyCode == Enum.KeyCode.LeftShift and not gameProcessed then
		game.Players.LocalPlayer.Character:FindFirstChild("Humanoid").WalkSpeed = 16
		TweenBarEmpty:Pause()
		RefillBar:Play()
	end
end)

Here’s a gif: Sprint Test.gif

The code here is the very basics of it. You’d have to go through and do whatever you need to the bar and disable sprinting when it empties and the such. However this should be all you need to get started!

Hope this helps!

2 Likes