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

Variable is false but loop is still not running?

local stamina = Instance.new("IntValue")
stamina.Name = "Stamina"
stamina.Value = 100
stamina.Parent = script.Parent

local running = false

local humanoid = script.Parent.Humanoid

function regen ()
    while true do
	    while stamina.Value < 100 and running == false do
		    print("Regenerating..", stamina.Value)
		    wait(0.1)
		    stamina.Value = stamina.Value + 1
	    end
	    stamina.Changed:Wait()
	end
end

spawn(regen)

function onKeyPress(actionName, userInputState, inputObject)
    if userInputState == Enum.UserInputState.Begin then
	    print("Running")
	    running = true
    end
    if userInputState == Enum.UserInputState.End then
	    print("Walking")
	    running = false
    end
end

game.ContextActionService:BindAction("keyPress", onKeyPress, false, Enum.KeyCode.LeftShift)

This is a test script i made for sprinting so not all parts is included but still works the same way, this is a local script inside the StarterCharacterScript. The concept is that while I'm not running I'm regenerating 1/100 stamina every second if my stamina is not 100, while running I'm loosing 2/100 stamina every second. The problem is when I started running my stamina won't regenerate (which is how it's supposed to work)

but when i stopped running my stamina still doesn't regenerate

, when I change the value of the stamina to something lower than 100 via explorer it starts to regenerate again.

7 comments

0

Do you have something in the script that makes the player lose stamina while they're running? When I tested your script, I saw that it did print "running" and "walking" at the right times, but the value of stamina doesn't go down at all (in which case it wouldn't regenerate, because stamina.Value < 100 isn't true).

0

I told that this script is a test script, not all parts are included but everything works the same way, the part where the player lose stamina is removed since its not the problem and may confuse the readers

0

You can change the value via explorer, it works the same way.

0

It doesn't show any errors, copy pasting this code and running it wont show any results.

0

I added print("Running") and print("Walking") to show that the sprint function itself works

0

Did you try changing while stamina.Value < 100 and running == false do to if stamina.Value < 100 and running == false then

0

the question is already answered.

1 answer

✓ Accepted answer

I put this exact code into the script and it works fine for me. (Probably could be better, but it works.)

function onKeyPress(actionName, userInputState, inputObject)
    if userInputState == Enum.UserInputState.Begin then
	    running = true
	    while true do
		    wait(0.1)
		    stamina.Value = stamina.Value - 2
		end
	end
	if userInputState == Enum.UserInputState.End then
		running = false
		while true do
			wait(0.1)
			stamina.Value = stamina.Value + 2
		end
	end
end

1 comment

1

This is great, though there's some "errors" I'll just fix them my self.

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