Sprinting and stamina system wont work correctly?

i tried to use a tutorial on yt about how to make a stamina and sprinting system. heres the vid: ROBLOX Sprint + Stamina Bar - 2019 Scripting Tutorial (Simple Endurance/Run Bind) - YouTube

the script does work, but not correctly, it does speed up the player, but doesnt lower the stamina bar. the stamina bar is a frame inside a screen gui inside startergui.
Screenshot_29

i have two scripts, a local script inside startercharacterscripts
Screenshot_30
staminaclient script:

local uis = game:GetService(“UserInputService”)
local repstore = game:GetService(“ReplicatedStorage”)
local players = game:GetService(“Players”)
– sprint key pressed

uis.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
repstore.RemoteEvents.Sprint:FireServer(“Began”)
end
end)

– sprint key ended

uis.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
repstore.RemoteEvents.Sprint:FireServer(“Ended”)
end
end)

repstore.RemoteEvents.StaminaUpdate.OnClientEvent:Connect(function(stamina, maxStamina)
players.LocalPlayer.PlayerGui.Stamina.Bar.Size = UDim2.new((stamina / maxStamina)* 0,300, 0,25)
end)

and a serverscript

Screenshot_31

staminaserver script:

local replicatedStorage = game:GetService(“ReplicatedStorage”)
local players = game:GetService(“Players”)
local runService = game:GetService(“RunService”)

local maxStamina = 500
local staminaRegen = 2

local sprintModifier = 1.8
local sprintStaminaCost = 5

local sprintingplayers = {

}

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)

– sprint key pressed or released
replicatedStorage.RemoteEvents.Sprint.OnServerEvent:Connect(function(player, state)
local hum = player.Character.Humanoid

if state == “Began” and hum:GetState() == Enum.HumanoidStateType.RunningNoPhysics and hum.MoveDirection.Magnitude > 0 then
sprintingplayers[player.Name] = hum.WalkSpeed
hum.WalkSpeed = hum.WalkSpeed * sprintModifier
elseif state == “Ended” and sprintingplayers[player.Name] then
hum.WalkSpeed = sprintingplayers[player.Name]
sprintingplayers[player.Name] = nil

end
end)

–adjust stamina

runService.Heartbeat:Connect(function()
for index, player in pairs(players:GetChildren()) do
local stamina = player.Stamina
local name = player.Name

  if not sprintingplayers[name] then
  	if stamina.Value > maxStamina then
  		stamina.Value = maxStamina
  	elseif stamina.Value < maxStamina then
  		stamina.Value = stamina.Value + staminaRegen
  	end
  else
  	if stamina.Value >= sprintStaminaCost then
  		stamina.Value = stamina.Value - sprintStaminaCost
  	else
  		player.Character.Humanoid.WalkSpeed = sprintingplayers[name]
  		sprintingplayers[name] = nil
  	end
  end

end
end)

im not sure what is wrong, please help.

1 Like

There is a problem with your stamina client try doing this:

local userInputService = game:GetService("UserInputService")

local replicatedStorage = game:GetService("ReplicatedStorage")

local players = game:GetService("Players")

-- Sprint Key Pressed

userInputService.InputBegan:Connect(function(input)

if input.KeyCode == Enum.KeyCode.LeftShift then

replicatedStorage.RemoteEvents.Sprint:FireServer("Began")

end

end)

-- Sprint Key Released

userInputService.InputEnded:Connect(function(input)

if input.KeyCode == Enum.KeyCode.LeftShift then

replicatedStorage.RemoteEvents.Sprint:FireServer("Ended")

end

end)

-- Update Stamina GUI

replicatedStorage.RemoteEvents.StaminaUpdate.OnClientEvent:Connect(function(stamina, maxStamina)

players.LocalPlayer.PlayerGui.Stamina.Bar.Size = UDim2.new((stamina / maxStamina) * .2, 0, .044, 0)

end)

If you need any more help tell me :heart:

dont really know how to explain with words but heres a vid

You can use math.clamp(xValue, MinValue, MaxValue), stamina.Value = math.clamp(stamina.Value, 0, maxStamina).

You can see I made some changes to your code, but you don’t have to make all the changes I did. If you look carefully, there are some things in our codes that differ. You can keep your Code as it is, but add those new lines of code. If you face any issues, be sure to DM me!