So I already asked about an older version of this script which I've added a lot more too
This is split up into 2 scripts and uses a Gui and remote events
StaminaClient - consists of the inputs used (located in; StarterPlayer.StarterCharacterScripts)
local userInputService = game:GetService("UserInputService")
local replicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
replicatedStorage.RemoteEvents.Run:FireServer("Run ended")
-- Sprint Key Pressed
userInputService.InputBegan:Connect(function(input)
if input.KeyCode ==Enum.KeyCode.LeftShift then
replicatedStorage.RemoteEvents.Sprint:FireServer("Sprint Began")
-- Sprint Key Relased
elseif input.KeyCode ==Enum.KeyCode.LeftShift then
replicatedStorage.RemoteEvents.Sprint:FireServer("Sprint Ended")
-- Jump Key Pressed
elseif input.KeyCode ==Enum.KeyCode.Space then
replicatedStorage.RemoteEvents.Sprint:FireServer("Jump")
--Caps lock Pressed
elseif input.KeyCode ==Enum.KeyCode.CapsLock then
replicatedStorage.RemoteEvents.Sprint:FireServer("Run Began")
--Caps Lock Relased
elseif input.KeyCode ==Enum.KeyCode.CapsLock then
replicatedStorage.RemoteEvents.Sprint:FireServer("Run Ended")
end
end)
replicatedStorage.RemoteEvents.StaminaUpdate.OnClientEvent:Connect(function(stamina, maxStamina)
players.LocalPlayer.PlayerGui.Hud.StaminaBar.Stamina.Size = UDim2.new((stamina / maxStamina) * 0.98, 0, 0, 10)
end)
I then have 3 remote events (located in; ReplicatedStorage.RemoteEvents)
Run - controls the caps lock input Sprint - controls the shift input StaminaUpdate - to do with visual side
I then have my main Run/Stamina script in ServerScriptStorage tis called Stamina Server
local replicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
local runService = game:GetService("RunService")
local maxStamina = 500
local staminaRegen = 2
local minStamina = 0
local sprintModifier = 1.8
local runModifier = 1.2
local sprintStaminaCost = 4
local sprintingPlayers = { }
local runningPlayers = { }
players.PlayerAdded:Connect (function(player)
local stamina = Instance.new("IntValue", player)
stamina.Value = maxStamina
stamina.Name = "Stamina"
stamina.Changed:Connect(function(property)
replicatedStorage.RemoteEvents.StaminaUpdate:FireClient(player, stamina.Value, maxStamina)
end)
end)
-- Adjust Stamina for each player every frame
runService.Heartbeat:Connect(function()
for index, player in pairs (players:GetChildren() ) do
local stamina = player.Stamina
local name = player.Name
--Sprint
if sprintingPlayers[name] then
if stamina.Value > maxStamina or
stamina.Value <=minStamina then
stamina.Value = 0
elseif stamina.Value < maxStamina then
stamina.Value = stamina.Value - staminaRegen
else
if stamina.Value >= sprintStaminaCost then
stamina.Value = stamina.Value - sprintStaminaCost
else
player.Character.Humanoid.WalkSpeed = sprintingPlayers[name]
sprintingPlayers[name] = nil
end
end
end
--Run
if runningPlayers[name] then
if stamina.Value > maxStamina or
stamina.Value <= minStamina then
stamina.Value = stamina.Value
elseif stamina.Value < maxStamina then
stamina.Value = stamina.Value
else
if stamina.Value >= sprintStaminaCost then
stamina.Value = stamina.Value
else
player.Character.Humanoid.WalkSpeed = runningPlayers[name]
runningPlayers[name] = nil
end
end
end
end
end)
-- Key pressed/released using the remote event
replicatedStorage.RemoteEvents.Sprint.OnServerEvent:Connect (function(player, state)
local humanoid = player.Character.Humanoid
local stamina = player.Stamina
local name = player.Name
local run = false
local sprint = false
--Jump Stamina deduction
if state == "Jump" then
stamina.Value = stamina.Value - 50
end
--Sprint Stamina deduction
if state == "Sprint Began" and humanoid:GetState () == Enum.HumanoidStateType.RunningNoPhysics and humanoid.MoveDirection.Magnitude > 0 and stamina.Value > minStamina then
sprintingPlayers [player.Name] = humanoid.WalkSpeed
humanoid.WalkSpeed = humanoid.WalkSpeed * sprintModifier
sprint = true
print("Sprint = ",sprint) --prints are just for testing
elseif state == "Sprint Ended" and sprintingPlayers[player.Name] or stamina.Value < minStamina then
humanoid.WalkSpeed = sprintingPlayers[player.Name]
sprintingPlayers[player.Name] = nil
sprint = false
print("Sprint = ",sprint) --prints are just for testing
end
--Run detection
if state == "Run Began" and humanoid:GetState () == Enum.HumanoidStateType.RunningNoPhysics and humanoid.MoveDirection.Magnitude > 0 then
runningPlayers [player.Name] = humanoid.WalkSpeed
humanoid.WalkSpeed = humanoid.WalkSpeed * runModifier
run = true
print("Run =",run) --prints are just for testing
elseif state == "Run Ended" and runningPlayers[player.Name] then
humanoid.WalkSpeed = runningPlayers [player.Name]
runningPlayers[player.Name] = nil
run = false
print("Run =",run) --prints are just for testing
end
-- Stamina add
while stamina.Value < maxStamina or run == false or sprint == false do
stamina.Value = stamina.Value + staminaRegen
print(stamina.Value) --prints are just for testing
wait(1)
end
end)
Issue 1. the first issue is with the If statements in the Stamina Server, I've tried meany things to get them to work and to figure out what the issue is (although I'm only learning lua)
The main problem I have noticed is when you run out of Stamina it causes a different effect on the sprint depending on if you where sprinting when you ran out
so say I was sprinting and i ran out of stamina while sprinting I could carry on sprinting but if I stop then I cant sprint until Stamina is regend
Issue 2. so my second issue is with the while loop at the very end, when the player spawns both sprint and run are = to false and stamina.Value is = maxStamina meaning you do not regain stamina but as soon as your stamina.Value decreases It causes the stamina.Value to increase till its = to maxStamina even if you do run or sprint
an example of this is if i start sprinting (meaning stamina.Value stats to drop to < maxStamina) it will run the while loop which is an issue
Thanks for any help you can give me, I've been so frustrated at this the past few days XD I used a youtube video to base this off but I've added my own modifications I also do know server side is an annoying way of doing a sprint script but I have my reasons ;)

2 comments
You don't need that many inputbegan, put them in one event as an elseif or if statement
Thx, didn't relies twas that messy