Why is my stamina script so buggy?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

A less buggy stamina script

  1. What is the issue? Include screenshots / videos if possible!

For some reason, whenever it’s recovering some stamina it works fine but when it’s about to go back to 100 (the maximum), it takes longer than the others to iterate. and also when it’s depleting the stamina, it somehow doesn’t reach 0. it stops at 1

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

My stamina script is fairly different than others so it wouldn’t compare.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

My stamina script works when you only walk and does not need to press a button to “sprint” like the others. just when walking or interacting with “wasd”.

in a local script:

local player = game.Players.LocalPlayer
repeat task.wait() until player.Character
local char = player.Character
local Humanoid = char:WaitForChild("Humanoid")
local stamina = 100
stamina = math.clamp(stamina, 0 ,100)

while task.wait(0.1) do
	if Humanoid.MoveDirection.Magnitude > 0 and stamina > 0 then
		repeat
				print("walking " .. tostring(stamina))
				stamina -= 1
			task.wait(0.05)
		until Humanoid.MoveDirection.Magnitude == 0 or stamina <= 0
	elseif Humanoid.MoveDirection.Magnitude == 0 and stamina <= 100 then
		repeat
			print("not walking " .. tostring(stamina))
			stamina += 1
			task.wait(0.05)
		until Humanoid.MoveDirection.Magnitude > 0 or stamina >= 100
	end
end

Help

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like
while task.wait(0.5) do
    if Humanoid.MoveDirection.Magnitude > 0 and stamina > 0 then
         stamina -= 1
    elseif Humanoid.MoveDirection.Magnitude == 0 and stamina <= 100 then
        stamina += 1
    end
    if stamina <0 then stamina = 0 end
    if stamina >100 then stamina = 100 end
end
1 Like

There’s lot of wasted code here.
The logic you had was fine, but the additional repeat loop is redundant as the logic checks are already covered in the while loop.

Also the math.clamp at the start will do nothing but set the value to 100 again at start which only occurs once.

Finally i removed the <= 100 logic on the stamina increase for < 100, as if the stamina equals 100, it will add one and stop at 101.


local player = game.Players.LocalPlayer
repeat task.wait() until player.Character
local char = player.Character
local Humanoid = char:WaitForChild("Humanoid")
local stamina = 100

while task.wait(0.1) do
	if Humanoid.MoveDirection.Magnitude > 0 and stamina > 0 then
		print("walking " .. tostring(stamina))
		stamina -= 1
	elseif Humanoid.MoveDirection.Magnitude == 0 and stamina < 100 then
		print("not walking " .. tostring(stamina))
		stamina += 1
	end
end


while task.wait(0.5) do
    if Humanoid.MoveDirection.Magnitude > 0 and stamina >= 1 then
         stamina -= 1
    elseif Humanoid.MoveDirection.Magnitude == 0 and stamina <= 100 then
        stamina += 1
    end
    if stamina < 1 then stamina = 0 end
    if stamina >= 100 then stamina = 100 end
end

Edited your math a bit