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.

while game.Workspace.VAR.timeformon.Value==true do
    local modal = game.ServerStorage.IODOLLARS:Clone()
    modal.Parent=game.Workspace
    modal.Position=Vector3.new(98.05, 4.05, 228.65)
    local modol = game.ServerStorage.IODOLLARS:Clone()
    modol.Parent=game.Workspace
    modol.Position=Vector3.new(76.75, 2.85, 219.85)
    local modl = game.ServerStorage.IODOLLARS:Clone()
    modl.Parent=game.Workspace
    modl.Position=Vector3.new(89.45, 6.95, 191.55)
    local modlo = game.ServerStorage.IODOLLARS:Clone()
    modlo.Parent=game.Workspace
    modlo.Position=Vector3.new(82.05, 18.05, 228.55)
    game.Workspace.VAR.timeformon.Value=false

end

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.

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

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