so I have a script I partly wrote with some help form youtube and websites, Its a server side stamina script (there is a reason to why I'm using a server side instead of local) Ill try my best to say where the scripts are located
In Replicated storage I have two events Sprint and StaminaUpdate
the Stamina client is located in: StarterPlayer StarterCharacterScripts local script
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")
wait(0.01)
replicatedStorage.RemoteEvents.Sprint:FireServer("Ended")
end
end)
-- Sprint Key Pressed
userInputService.InputBegan:Connect(function(input)
if input.KeyCode ==Enum.KeyCode.LeftShift then
replicatedStorage.RemoteEvents.Sprint:FireServer("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 also then have the Stamina Server script in: ServerScriptService
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 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)
-- 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
if sprintingPlayers[name] then
if stamina.Value > maxStamina or
stamina.Value <= maxStamina - 500 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
end
end)
-- sprint key is pressed or released
replicatedStorage.RemoteEvents.Sprint.OnServerEvent:Connect (function(player, state)
local humanoid = player.Character.Humanoid
local stamina = player.Stamina
if state == "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
elseif state == "Ended" and sprintingPlayers[player.Name] or stamina.Value < minStamina then
humanoid.WalkSpeed = sprintingPlayers[player.Name]
sprintingPlayers[player.Name] = nil
end
end)
So I think my issue is with the last part of the Stamina Server, I've realized that its hanging on the if statement, I have no idea on how to check every frame if the player has enough stamina to be able to run.
Any help is much appropriated :D and just say if you need me to explain anything more.
