--[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?

3 comments
I'm not sure if that is really the solution since I see return only used for returning variables, function arguments, and etc.
Return can also be used to end a function, but you're correct in that sense as well.
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.