Currently if you use an fps unlocker the stamina drains incredibly fast, this is obviously due to me not using delta time… but I don’t know how I would implement that in this script
local outofstam = false
local maxstamina = 100
local minstamina = 0
local stamina = 100
local staminaclamped = math.clamp(stamina, minstamina, maxstamina)
local hum = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
function speed(sp)
hum.WalkSpeed = sp
end
game:GetService("StarterGui"):SetCoreGuiEnabled(Enum.CoreGuiType.All, false)
game:GetService("RunService").RenderStepped:Connect(function()
if stamina > 100 then stamina = math.clamp(stamina, minstamina, maxstamina) end
if not outofstam then
if game:GetService("UserInputService"):IsKeyDown(Enum.KeyCode.LeftShift) then
if staminaclamped < 1 then
outofstam = true
script:WaitForChild("norun").Value = true
speed(16)
else
stamina -= 0.3
speed(32)
end
else
stamina += 0.1
speed(16)
end
else
if stamina < 30 then
stamina += 0.1
speed(16)
else
outofstam = false
script:WaitForChild("norun").Value = false
end
end
staminaclamped = math.clamp(stamina, minstamina, maxstamina)
script:WaitForChild("stamina").Value = staminaclamped
--print(staminaclamped)
end)
there is also a gui that goes across the screen depending on the stamina level, but that isn’t important and wouldn’t be changed by delta time usage in the gui because it uses the stamina value in this.
stamina value = a number value that the gui can use
norun value = a bool that will turn the stamina bar red if you are out of stamina/recharging stamina, and will turn green if you can run again
please help this is a serious concern and makes the game on anything higher then 60fps unbearable
1 Like
I do also realize how incredibly inefficient this code is now…
I dont even need stamina clamped as it already clamps the stamina from going above 100 and under 0…
Edit:
oh and if your getting anything less than 60 fps, you suddenly gain a stupid high amount of stamina!
It still sets your speed but is draining less stamina then it should because it drains it a specific amount every frame, less frames = more stamina
1 Like
You could try a while loop with a wait function
local outofstam = false
local maxstamina = 100
local minstamina = 0
local stamina = 100
local staminaclamped = math.clamp(stamina, minstamina, maxstamina)
local hum = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
function speed(sp)
hum.WalkSpeed = sp
end
game:GetService("StarterGui"):SetCoreGuiEnabled(Enum.CoreGuiType.All, false)
function Update()
if stamina > 100 then stamina = math.clamp(stamina, minstamina, maxstamina) end
if not outofstam then
if game:GetService("UserInputService"):IsKeyDown(Enum.KeyCode.LeftShift) then
if staminaclamped < 1 then
outofstam = true
script:WaitForChild("norun").Value = true
speed(16)
else
stamina -= 0.3
speed(32)
end
else
stamina += 0.1
speed(16)
end
else
if stamina < 30 then
stamina += 0.1
speed(16)
else
outofstam = false
script:WaitForChild("norun").Value = false
end
end
staminaclamped = math.clamp(stamina, minstamina, maxstamina)
script:WaitForChild("stamina").Value = staminaclamped
--print(staminaclamped)
end
while true do
coroutine.wrap(Update)(task.wait(1/60))
end
Thanks a lot, that works and well, I do think I am going to have to tweak the values again because it seems to be draining at a different rate then usual, but its consistent between 60 and 60+ fps. Although I do still want to learn how to use delta time because there are other places I know I am going to need it, but that can wait because now I feel the incredible urge to rewrite my sprinting script for no reason.
if you want to get deltatime in the source i gave you, simply change
function Update()
to
function Update(deltaTime)
It appears that your version is doing some wild stuff to the memory
it keeps increasing the longer im in the game
oh no
edit:
aaand my game froze around 2-5 minutes later
i think its because of the
while true coroutine
so im just going to change it to this, and see if it works to stop the memory going up constantly
local outofstam = false
local maxstamina = 100
local minstamina = 0
local stamina = 100
local staminaclamped = math.clamp(stamina, minstamina, maxstamina)
local hum = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
function speed(sp)
hum.WalkSpeed = sp
end
game:GetService("StarterGui"):SetCoreGuiEnabled(Enum.CoreGuiType.All, false)
function Update()
if stamina > 100 then stamina = math.clamp(stamina, minstamina, maxstamina) end
if not outofstam then
if game:GetService("UserInputService"):IsKeyDown(Enum.KeyCode.LeftShift) then
if staminaclamped < 1 then
outofstam = true
script:WaitForChild("norun").Value = true
speed(16)
else
stamina -= 0.3
speed(32)
end
else
stamina += 0.1
speed(16)
end
else
if stamina < 30 then
stamina += 0.1
speed(16)
else
outofstam = false
script:WaitForChild("norun").Value = false
end
end
staminaclamped = math.clamp(stamina, minstamina, maxstamina)
script:WaitForChild("stamina").Value = staminaclamped
--print(staminaclamped)
end
--[[
while true do
coroutine.wrap(Update)(task.wait(1/60))
end]]
while task.wait(1/60) do
Update()
end
and i know its not assets being loaded to memory, because the memory goes up and down normally, but here it goes up at an unusually fast rate
edit 2:
help its still freezing what is wrong, even when i disable the script its still doing it oh god
ok i actually actually fixed it now. cool.
hey i just wanna say please dont use a while loop. your og script using renderstepped was so much better, and your one problem was fps affecting stamina usage. to fix that multiply the stamina cost/regen with the fps:
RunService.RenderStepped:Connect(function(FPS)
stamina += 1 * FPS
stamina -= 1 * FPS
end)
2 Likes
yea I knew I shouldn’t use a loop, there is a “slight” issue though, using that makes the stamina drain INCREDIBLY slow.
Buuut since yours is a better solution I’m going to mark it
edit:
the drain at unlocked fps, around 250, the print output is (0.15 * dt, dt)

the drain at locked 60

code:
local outofstam = false
local stamina = 100
local hum = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
function speed(sp)
hum.WalkSpeed = sp
end
game:GetService("StarterGui"):SetCoreGuiEnabled(Enum.CoreGuiType.All, false)
function Update(DT)
if stamina > 100 then stamina = math.clamp(stamina, 0, 100) end
if not outofstam then
if game:GetService("UserInputService"):IsKeyDown(Enum.KeyCode.LeftShift) then
if stamina < 10 then
outofstam = true
script:WaitForChild("norun").Value = true
speed(16)
else
stamina -= 0.15 * DT
print(0.15 * DT, DT)
speed(32)
end
else
stamina += 0.1 * DT
speed(16)
end
else
if stamina < 30 then
stamina += 0.035 * DT
speed(16)
else
outofstam = false
script:WaitForChild("norun").Value = false
end
end
script:WaitForChild("stamina").Value = stamina
--print(staminaclamped)
end
game:GetService("RunService").RenderStepped:Connect(Update)
another edit:
ok i just had to up the values for drain a TON
drain = 10 * dt
refill = 8 * dt
refill to 10% from empty = 3 * dt, once at 10% resumes at normal refill speed
3 Likes