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

In the middle of a loop, how to I make it start back to the beginning?

--[MAIN GAME SCRIPT]
while true do
	wait()
if checkPlayers() then --if the required amount of players is reached, then
		deleteLastMap()
		wait(1)
		intermission()
		wait(1)
		choosemap()
		wait(3)
		displayChosen()
		wait(2)
		loadMinigame()
		wait(2)
		startGame()
		wait(1)
		round()
	end 
end

Note: This is only part of a script for a minigames game I'm making.

When the function named "round" is happening, for each 0.01 seconds, I want the script to detect if game.Workspace.GameEnded.Value == true. However, I cannot do this in the function named "round" because the loop would be stuck on "round()".

So, I made a separate script for detecting if the GameEnded value is true. When it is true, all of the players get teleported to a random spawn in the lobby. Here is that script:

--[GAME ENDED DETECTOR]
while true do
		wait(0.01)
	if game.Workspace.GameEnded.Value == true then
		game.Workspace.GameEnded.Value = false
			local spawns = game.Workspace.Lobby.Spawns:GetChildren()
			local r = math.random(#spawns)
			local randomspawn = spawns[r]
	for i, player in ipairs(game.Players:GetChildren()) do
		if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
			player.Character.HumanoidRootPart.CFrame = randomspawn.CFrame+Vector3.new(0,3,0)
			wait(0.01)
		end 
	end 
end
end

Everything works fine -- except when the player wins the game. What I did is when the player wins the game, the value, "GameEnded" equals true. Since the GameEnded value is true, the players get teleported to the lobby, but, the loop doesn't restart because I haven't messed with that. I know that it hasn't restarted because when I test the game, and win a round, the GUI timer for the round is still going.

So, when the GameEnded value is true, I need to cut off the function, "round" and start the loop from the beginning, but it's not that simple in this situation (for me at least)...

I've thought about changing the "if checkplayers()" part by adding to it, "and game.Workspace.GameEnded.Value == true then", but here's an issue: GameEnded isn't set to true when the game server starts. Even if it was set to true when the server starts, then that wouldn't work out because I've tried it before and it just plain doesn't work...

I'm extremely lost and I don't know what to do! I've searched up information about loops, made a previous question about this (but deleted it because it was unclear). It's been hard trying to fix this because the scripts are like... I don't know how to describe it... Fragile! If I change one thing, then I have to change the other... Could someone help please?

2 answers

✓ Accepted answer

I'm sorry if I can't offer much, but if you use "return" in your loop when the condition is fulfilled, it will stop the function it is in, so maybe your script would look like this:

while true do
	--blah blah blah
	if game.workspace.GameEnded.Value == true then
		--Everything that happens
		return
	end
end

Of course, you would need to call it again since it would stop the whole function, but I guess that counts as starting the loop from the beginning.

3 comments

1

I'm not sure if that is really the solution since I see return only used for returning variables, function arguments, and etc.

1

Return can also be used to end a function, but you're correct in that sense as well.

0

I've thought about that, buttttt, like I said in the original post, the while true do in the round function (or pretty much in any other function mentioned in the loop) would make the loop stop, since I tried what you said earlier.... When I added a return the function just kept on going for some reason.

Since I'm adding part of the script to this, I'm putting this as an answer because in comments I can't format the script. I took a bit of superwar55's answer to add "return" but changed a lot more too to make this work. Here it is!

function round()
	local roundTime = 30
	game.Workspace.RoundLength.Value = roundTime
	for i = 1, roundTime do -- in seconds
		wait(1)
		roundTime = roundTime - 1
		message.Value = " "..MinsToSecs(roundTime).."  "
		for i = 0.01, 100 do
			wait(0.01)
				if game.Workspace.GameEnded.Value == true then
		game.Workspace.GameEnded.Value = false
			local spawns = game.Workspace.Lobby.Spawns:GetChildren()
			local r = math.random(#spawns)
			local randomspawn = spawns[r]
	for i, player in ipairs(game.Players:GetChildren()) do
		if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
			player.Character.HumanoidRootPart.CFrame = randomspawn.CFrame+Vector3.new(0,3,0)
			return
		end
	end
	game.Workspace.GameEnded.Value = true
	message.Value = "Nobody won! Round has ended!"
	wait(2)
end end end end 

So what I did is within the each second of the countdown, I squeezed in the if the GameEnded == true statement for each hundredth of a second. Since I used superwar's idea, I'm going to accept his answer. Thanks!

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