Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Loop wont Complete?

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

1 comment

0

This needs a little more explanation. Can you explain exactly what you're trying to do with that code?

2 answers

✓ Accepted answer

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

3 comments

0

i think it will crash the server...

0

Thanks so much for the help with all my questions! your awesome :D

0

@dav; you're right. I added line 10 just now to fix it.

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

This discussion is preserved as it was. New answers, comments, and votes are closed.