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

My while loop has a problem with a variable?

Asked by 5 years ago

I have a variable in a folder called VAR in the workspace, which DEFINITELY becomes true after the end of a script, because I got it to print it. However, this while loop isn't working. It's supposed to be cloning a model in ServerStorage into different positions, but the script doesn't run at all. Please help! Thank you.

01while game.Workspace.VAR.timeformon.Value==true do
02    local modal = game.ServerStorage.IODOLLARS:Clone()
03    modal.Parent=game.Workspace
04    modal.Position=Vector3.new(98.05, 4.05, 228.65)
05    local modol = game.ServerStorage.IODOLLARS:Clone()
06    modol.Parent=game.Workspace
07    modol.Position=Vector3.new(76.75, 2.85, 219.85)
08    local modl = game.ServerStorage.IODOLLARS:Clone()
09    modl.Parent=game.Workspace
10    modl.Position=Vector3.new(89.45, 6.95, 191.55)
11    local modlo = game.ServerStorage.IODOLLARS:Clone()
12    modlo.Parent=game.Workspace
13    modlo.Position=Vector3.new(82.05, 18.05, 228.55)
14    game.Workspace.VAR.timeformon.Value=false
15 
16end
0
Is it a model or is it a part that your cloning? Lava_Scripter 109 — 5y

1 answer

Log in to vote
0
Answered by
Psudar 882 Moderation Voter
5 years ago
Edited 5 years ago

I think what's happening here is that the script is going past the while loop before the value is true, so it never runs that code. Since the script cant move back up when you change it to true, you need to use something like an event. Theres an event called :GetPropertyChangedSignal() that should do you well.

1local timeformon = game.Workspace.VAR.timeformon
2timeformon:GetPropertyChangedSignal("Value"):Connect(function()
3    while game.Workspace.VAR.timeformon.Value == true do
4        wait()
5        --put all that code from earlier here
6    end
7end)

Instance:GetPropertyChangedSignal() triggers when the given property is changed, and the while loop will stop running whenever its condition goes to false, so it should work just fine.

0
Psudar I solved it by wrapping the code in an if statement inside of the while loop. That way, it then permanently checks to see if the value is true or false, then leads a line, makes it false, and then destroys itself. Primrose_Studio 53 — 5y
Ad

Answer this question