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

How to change a variable inside a function to activate a while true do loop?

Asked by
pilotly 15
4 years ago

Hello! I have made few posts on this but here are the basics of this issue I'm trying to learn from.

Basically I'm trying to get a boolean variable to turn true so that a while true loop can activate.

It would help me greatly to tell me what the mistake is so I won't do it again. :)

Trouble code:

01local function buttonPressed(Messed)
02    local character = Messed.Parent
03    local humanoid = character:FindFirstChildWhichIsA("Humanoid")
04    --Functions cant be called in if statements
05    if humanoid and sentinal2 == true then     
06        activetime = true
07    end
08end    
09 
10 
11 
12finishline.Touched:Connect(partTouched)
13button.Touched:Connect(buttonPressed)
14 
15while activetime == true do
16        print("Debug3")
17    wait(1) timepassed = timepassed + 1
18        print(timepassed)
19end

Full code:

01local finishline = script.Parent
02local timepassed = 0
03local sentinal = true
04 
05local button = workspace.startr
06local sentinal2 = true
07 
08local activetime = false
09 
10local function finish()
11    sentinal = false
12    print("you finished in" .. timepassed)
13    if timepassed <= 10 then
14 
15        print("gold")
View all 54 lines...
0
It's not a very good setup, but you could have an if statement wrapped around everything inside of your while loop. Then it will only run your code when the boolean is true.. Nickuhhhhhhhhhhhhhhh 834 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago
  • You can use BindableEvents to wait until something happens or tables to store information about all players
  • Functions can be called in if statements
  • (Style) You have inconsistent capitalization; in Roblox, camelCase is common but you have things like timepassed (which is harder to read)
01local Players = game.Players
02local finishLine = script.Parent
03local startButton = workspace.startr
04 
05local playerToStartTime = {} -- we'll use this to keep track of when a player hit the start button last
06 
07local function finish(player, timePassed)
08    print(player, "finished in", timePassed)
09    if timePassed <= 10 then
10        print("gold")
11    elseif timePassed <= 20 then -- note: no need to check if "timePassed > 10" since you already checked to see if it's <= 10
12        print("silver")
13    elseif timePassed <= 30 then
14        print("bronze")
15    else
View all 36 lines...
Ad
Log in to vote
1
Answered by 4 years ago

The reason why this doesn't work is because the while loop isn't constantly being checked to see if the condition is true. While loops only run if the condition is met. Just like an if statement. The only difference being that it is repeating the code on a condition and not once (if statement). Because the while loop's condition is not met, it will not run and will be ignored for the script's lifetime. If you want the while loop to be checked again, try using a function.

Answer this question