Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to make a more responsive Stamina system?

Im trying to make my own stamina system, and my script works almost exactly as intended with one minor problem.

local StaminaFunc = function(CurrentStam, IsRunning, PlrCar)
	while IsRunning.Value do
		if CurrentStam.Value > 0 then
			CurrentStam.Value = CurrentStam.Value - 10
			print(CurrentStam.Value)
			wait(1)
		else
			PlrCar.Humanoid.WalkSpeed = 16
			IsRunning.Value = false
		end
	end
end

local StaminaRegenFunc = function(CurrentStam, IsRunning, PlrCar)
	while IsRunning.Value == false do
		if CurrentStam.Value < 100 then
		  CurrentStam.Value = CurrentStam.Value + .5
		  wait(.5)
		end
	end
end

Im using functions and more the more specifically while loops to determine when someone loses stamina as you can see. However, this current code allows a player to technically have negative stamina. Because when the code subtracts the stamina it does not check if the stamina is above 0 until the next time the while loops cycles. Any ideas on how to make this more responsive. (and before anyone asks this is in a server script being triggered by a local script via a remote event)

1 answer

Now I don't exactly know how you can still get these but I remember there used to be Double Constrained Values,where you set a max number aswell as a minimum number.And if the value went below or past the maximum it would instantly go back to the max value set.They are now deprecated and you can use LuaLibrary.math in order to set these kinds of things.

Now if you don't wanna use Double Constrained Values or LuaLibrary you can still make something within your script to make it work.

local StaminaFunc = function(CurrentStam, IsRunning, PlrCar)
    while IsRunning.Value do
        if CurrentStam.Value > 0 then
            CurrentStam.Value = CurrentStam.Value - 10
            print(CurrentStam.Value)

            if CurrentStam.Value < 0 then
             CurrentStam.Value = 0
            end

             -- This will check if the Stamina is below 0 and if so then its instantly set back to 0

            wait(1)
        else
            PlrCar.Humanoid.WalkSpeed = 16
            IsRunning.Value = false
        end
    end
end

2 comments

0

Thanks for your help but that will still lead to the same problem.

0

Then simply just use Double Constrained Values as im not that great at using LuaLibrary

This discussion is preserved as it was. New answers, comments, and votes are closed.