Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Variable is false but loop is still not running?

Asked by
CjayPlyz 643 Moderation Voter
5 years ago
Edited 5 years ago
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.

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). Recruitsoldier 40 — 5y
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 CjayPlyz 643 — 5y
0
You can change the value via explorer, it works the same way. CjayPlyz 643 — 5y
0
It doesn't show any errors, copy pasting this code and running it wont show any results. CjayPlyz 643 — 5y
View all comments (3 more)
0
I added print("Running") and print("Walking") to show that the sprint function itself works CjayPlyz 643 — 5y
0
Did you try changing while stamina.Value < 100 and running == false do to if stamina.Value < 100 and running == false then IceAndNull 142 — 5y
0
the question is already answered. CjayPlyz 643 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago

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
This is great, though there's some "errors" I'll just fix them my self. CjayPlyz 643 — 5y
Ad

Answer this question