Alright so here I have a running stamina script, or two actually with the local script, that takes away the player’s stamina as he runs, however I’ve run into two issues.
The Stamina Bar does not update as the player runs
2.Whenever I toggle left shift which is the keyCode for the function, the player runs for 2 seconds then stops as opposed to continually running for as long as I have the left shift held down.
Here’s the local script:
local UIS = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
UIS.InputBegan:Connect(function(Input)
if Input.KeyCode == Enum.KeyCode.LeftShift then
ReplicatedStorage.RemoteEvents.Sprint:FireServer("StartedState")
end
end)
UIS.InputEnded:Connect(function(Input)
if Input.KeyCode == Enum.KeyCode.LeftShift then
ReplicatedStorage.RemoteEvents.Sprint:FireServer("EndedState")
end
end)
ReplicatedStorage.RemoteEvents.StaminaUpdate.OnClientEvent:Connect(function(Stamina,MaxStamina)
Players.LocalPlayer.PlayerGui.Stamina.StaminaBar.Size = UDim2.new((Stamina/MaxStamina) * 0,410,0,31)
end)
And the Server Script:
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local MaxStamina = 300
local StaminaRegeneration = 4
local SprintModifier = 2
local SprintStaminaCost = 7
local SprintingPlayers = {}
game.StarterPlayer.EnableMouseLockOption = false
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)
ReplicatedStorage.RemoteEvents.Sprint.OnServerEvent:Connect(function(Player,State)
local Humanoid = Player.Character.Humanoid
if State == "StartedState" and Humanoid:GetState() == Enum.HumanoidStateType.RunningNoPhysics and Humanoid.MoveDirection.Magnitude > 0 then
SprintingPlayers[Player.Name] = Humanoid.WalkSpeed
Humanoid.WalkSpeed = Humanoid.WalkSpeed * SprintModifier
elseif
State == "EndedState" and SprintingPlayers[Player.Name] then
Humanoid.WalkSpeed = SprintingPlayers[Player.Name]
SprintingPlayers[Player.Name] = nil
end
end)
RunService.Heartbeat:Connect(function()
for i,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 + StaminaRegeneration
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)
I can’t find the bugs in the code for the life of me and there are no errors in the output, it’ll be great if one of y’all can sniff out the issue for me, thanks.
You’re updating the stamina value at 60 fps. So if I start at full stamina (300) and I’m sprinting (-7 stamina per frame) it takes less than 60 frames to get to 0 stamina (-7*60 = -420 which gives total stamina of -120).
Now, you say it lasts 2 seconds, but it should last less than one second. And the thing I’m most confused about is that you expect it to not run out - you implemented a max stamina so it has to run out.
For those who might comment on the 60 fps math I did above, server-client network traffic runs at 20 Hz, but since the server is updating stamina independently from the client, stamina is updated at 60 Hz. The stamina bar would be updated at 20 Hz but this doesn’t reflect the true stamina value.
(Your stamina bar doesn’t change because you’re multiplying by 0)
The 2 second thing is a rough estimate, it’s just really short. I multiplied it by 0 as according to the tutorial this was based off of, one should multiply the Stamina/MaxStamina by the left most value of the stamina bar size which so happens to be 0. So what must I do to fix this? and what exactly is the issue? Edit: Also is the sprinting -7 stamina per frame a standard info?
You’re right I have no idea what it does, I’m a novice scripter and I’ve been picking apart scripts to learn how stuff works. If you’re reluctant to help me, I kindly ask you to move on.
If you don’t want to explain what you’ve tried, well, I can’t say much.
If I was reluctant to help you think I would have even replied to start. Marking the solution on a petty reply just sounds like you’ve moved on instead. Everyone was a novice scripter at some point, there’s no reason to give up on something silly.
If you don’t want the stamina to run out (infinite sprint) just set the cost to 0. Ideally though,
this is unneeded if you don’t want stamina to run out. It would just be doing nothing since you set the cost to 0.
As for this,
have you tried changing the value to anything else other than 0? The other thing to take into consideration is, the way you are sizing your stamina bar. You are sizing it by pixels (410 by 31 pixels) so you need to multiply by (Stamina/MaxStamina) to the pixel dimensions so it changes the size of your bar.
I want the stamina to run out slowly over time along with the bar like normal stamina sprint systems in games.
This is the visual reference of the issue: robloxapp-20201210-1029493.wmv (1.3 MB)
Don’t worry about it, I know you mean well and I was a bit harsh. I know you are looking to further your scripting skills - the best advice I can give is that the more you struggle with it, the stronger you will be. When you’ve seen all the bad things that can happen then the good stuff is just the best. I don’t like to give the answer straight away because you will benefit more from tinkering and solving it after falling a few times. I guess I have that “learning to ride a bike” mentality - you learn what not to do so if you run into future bugs, you can deal with them.