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

While loop only runs through 1 iteration?

Asked by
AronYstad 101
3 years ago

I'm trying to make a code that clones a part called "Gobbler" every few seconds using a while loop. Every round the value of roundActive is temporarily set to false and then changed back to true. And the map is destroyed and then cloned back again. But for some reason, the while loop seems to only work the first round (the first time the value of roundActive is changed to true) and not for any other round (after it has temporarily been set to false). During the second round though, it seems like it clones the gobbler once for some reason.

wait(5)

local gobbler = game.Workspace.map1:WaitForChild("Gobbler")
local backup = gobbler:Clone()
local difficulty = game.Workspace.Difficulty
local roundActive = game.Workspace.RoundActive

while roundActive.Value == true do
    if roundActive.Value == true then
        if difficulty.Value < 10 then
            wait(10 - difficulty.Value)
            gobbler = backup:Clone()
            gobbler.Parent = game.Workspace.map1
            gobbler.Position = Vector3.new(0, 64, 0)
        else
            wait(10 / difficulty.Value)
            gobbler = backup:Clone()
            gobbler.Parent = game.Workspace.map1
            gobbler.Position = Vector3.new(0, 64, 0)
        end
    end 
end

I am pretty sure it is this code that's the problem, since the value of roundActive is being changed, and this is the code that actually clones the gobblers, but I might be wrong. Please tell me if you think there might be some other code that causes the error.

0
add a loop twice that way it will go to the previous loop and check if roundactive is true again and wont continue after line 22. do what the guy who answered did greatneil80 2647 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago
Edited by JesseSong 3 years ago

try this instead:

while true do
wait(0.5)
while roundActive.Value == true do
    if roundActive.Value == true then
        if difficulty.Value < 10 then
            wait(10 - difficulty.Value)
            gobbler = backup:Clone()
            gobbler.Parent = game.Workspace.map1
            gobbler.Position = Vector3.new(0, 64, 0)
        else
            wait(10 / difficulty.Value)
            gobbler = backup:Clone()
            gobbler.Parent = game.Workspace.map1
            gobbler.Position = Vector3.new(0, 64, 0)
        end
    end 
end
end


0
Thanks. And sorry for being stupid. I made it in the middle of the night, so I didn't realize the error. AronYstad 101 — 3y
Ad

Answer this question