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

Loop wont Complete?

Asked by 9 years ago

The Loop will run once, but then will end, and I'm not quite sure why, Thanks for reading! In this code im attempting to make a text go in in numbers such as 1,2,3,4, esc as a way to count waves of a game, 1 second being a basis

Game_Over = game.Workspace.Values.Status.Value
WaveNum = game.Workspace.Values.Wave.Value
while Game_Over == false do
    wait(1)
script.Parent.Text = WaveNum +1 
end
0
This needs a little more explanation. Can you explain exactly what you're trying to do with that code? adark 5487 — 9y

2 answers

Log in to vote
0
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

I still don't quite understand what you're trying to do, fully, but this should help:

First, you should only save references to the ValueObjects themselves, not a specific property of them. When you update the ValueObjects, the variables don't change because you took a 'snapshot' of the Property, not a reference to it.

The loop is 'ending' because you gave it an end condition. I would suggest wrapping the loop with an infinite one:

Game_Over = game.Workspace.Values.Status --I also suggest putting these Values in ServerStorage
WaveNum = game.Workspace.Values.Wave

while true do
    while not Game_Over.Value do
        wait(1)
        WaveNum = WaveNum.Value + 1
        script.Parent.Text = WaveNum.Value
    end
    Game_Over.Value = true
end
0
i think it will crash the server... davness 376 — 9y
0
Thanks so much for the help with all my questions! your awesome :D ioutragous 0 — 9y
0
@dav; you're right. I added line 10 just now to fix it. adark 5487 — 9y
Ad
Log in to vote
0
Answered by
davness 376 Moderation Voter
9 years ago

It's quite simple to solve this error.

First, if you replace this == false operator for an not one, your code will look more professional.

Game_Over = game.Workspace.Values.Status.Value
WaveNum = game.Workspace.Values.Wave.Value
while not Game_Over do
    wait(1)
script.Parent.Text = WaveNum +1 
end

And then, you are replacing the code with the WaveNum value. But you never change it, so it will be the same number forever. So we need to call an addition before the loop ends.

-- requests the values
Game_Over = game.Workspace.Values.Status.Value
WaveNum = game.Workspace.Values.Wave.Value

while not Game_Over do
    wait(1)
    script.Parent.Text = WaveNum +1 -- replaces the old value by the new value
    WaveNum = WaveNum + 1 -- adds 1 to the value
end

Answer this question