How to break out of a loop via a connection?

  • I have a game with rounds.

  • I want to make it so as soon as the selected player leaves, the game would end.

  • it would not be very ideal if I use if statements to constantly check for the player leaving.

  • I thought of using a connection event like this:

players.PlayerLeaving:Connect(function(leavingPlayer)
    
     if leavingPlayer == selectedPlayer then
           break -- attempting to break the rounds *for loop*
     end
end) 
  • And looks like I cant break a loop inside a function/connection event.

  • Any help would be greatly appreciated! :+1:

Use a variable so it send a variable true and make the look run only when the variable is false eg ‘while variable == false do…’ or you can create a new local connection = spawn(function() ‘round stuff’ end) and then check if the connection is running in ‘players.PlayerLeaving:Connect’ like this ‘if connection then’ connection:Disconnect(), i would have to see the other script to help you better

1 Like

Well I don’t think the connection:Disconnect will work actually for this isntacne sorry just use the variable bool thing

I want to end the loop as soon as the selected player leaves.
I think the while “variable == false do” will only check the statment every irretateion

  • dont understand htis part

This is what he is trying to say:

local shouldBreak = false

players.PlayerLeaving:Connect(function(leavingPlayer)

	if leavingPlayer == selectedPlayer then
		shouldBreak = true
	end
end) 

while true do
	
	if shouldBreak == true then
		break
	end
	
end
2 Likes

No.
Sorry for the confusion, let me clear it up.

My code is like this:

for gameRound = maxGameRounds, 0, -1 do

     <game logic>
end

You could still implement the same line of code, and it should work.

1 Like

yeah but,
I would have to check that if statement all the time.

There are 70 lines, so i’d basically need 70 of them.

Send me your whole code and let me check it out. I won’t steal it I promise lol

oh sure.
sorry for the spaghetti though

for gameRound = maxGameRounds, 0, -1 do
      local x = gameRound
	updatePlayersAlive()
	task.wait(1)
	announce("The Questionare is making the question!")
	questionUI = giveQuestionUI(selectedPlayer)
	local hasDoneEvent = false
	local newQuestionText
		
			
	local event = eventsFolder.QuestionCalc.OnServerEvent:Once(function(player, questionText)
		if player == selectedPlayer then
			questionUI:Destroy()
			hasDoneEvent = true
			newQuestionText = questionText
		end
	end)
			
	delay(questionMakeTIme, function()
				
	        if not hasDoneEvent then
		        event:Disconnect()
	        	announce("The Questionare has failed to make the question!", 1)
					
	          end	
	end)
		
	repeat task.wait(.1) until event.Connected == false
	if hasDoneEvent then
	       local answer = calculateString(newQuestionText)
		EditTiles(newQuestionText)
		countDown("What is " ..  newQuestionText, answerTime)
		fallTiles(newQuestionText)
		announce("The correct answer was " .. tostring(answer) .."!",5 )
		EditTiles("25*2")
	end
			
	if questionUI then questionUI:Destroy() end
	updatePlayersAlive()
			
	f seePlayers(selectedPlayer) then -- returns true if the selected player isnt in the game
		break
	end
			
	if gameRound == 0 then
		for i,v in players:GetPlayers() do
			if v.Team == game:GetService('Teams').Player then
				v.leaderstats.Wins.Value += 1
			end
		end
			i = 1
			for i,v in players:GetPlayers() do
				v.Team = game:GetService('Teams').Spectator
				v.Character.PrimaryPart.Position = teleportsFolder["Lobby"..tostring(i)].Position
				if i == 1 then i = 2 elseif i== 2 then i = 3 else i =1 end -- yes.
				end
				announce("Game ended! The Answerers won!",4)

			end
		end-- The game round loop end

you haven’t included the part where you tried to include your connection sharing is caring XD

1 Like
players.PlayerLeaving:Connect(function(leavingPlayer)
    
     if leavingPlayer == selectedPlayer then
           break -- attempting to break the rounds *for loop*
     end
end) 

But break gives an error so i cut it out

Edit: I put it at the start of the loop

for gameRound = maxGameRounds, 0, -1 do 

     players.PlayerLeaving:Connect(function(leavingPlayer)
    
          if leavingPlayer == selectedPlayer then
                break -- attempting to break the rounds *for loop*
          end
     end) 
 
     <other stuff>
end
local stupidVariables = false
     players.PlayerLeaving:Connect(function(leavingPlayer)
    
          if leavingPlayer == selectedPlayer then
               stupidVariables = true
		-- break -- attempting to break the rounds *for loop*
          end
     end) 


for gameRound = maxGameRounds, 0, -1 do 


	if stupidVariables == true then
break
end	
 
end

it would only check to break the loop each irretation…

i want to make the game end as soon as the player leaves.

ive also implemented my version of that

if seePlayers(selectedPlayer) then -- returns true if the selected player isnt in the game
	break
end

As i said. I would probabbly have to used that if statment for all the lines?
So thats why im trying to break it from a connection event :slight_smile:

local stupidVariables = false
     players.PlayerLeaving:Connect(function(leavingPlayer)
    
          if leavingPlayer == selectedPlayer then
               stupidVariables = true
		-- break -- attempting to break the rounds *for loop*
          end
     end) 


for gameRound = maxGameRounds, 0, -1 do 


	if stupidVariables == true then
break
end	
 
end


-- game proggress functions ()

if stupid variable == true -> break 

end)

sorry for the confusion but

isnt that the exact thing as before?

:face_with_raised_eyebrow:

Look up coroutines, something like this is probably what you need.

local rounds = coroutine.wrap(function()
    --code for game round
end)()
Players.playerleaving:connect(function()
--leave logic if true then
coroutine.cancel(rounds)
end)
1 Like

sorry for a late response but

i searched it up on the docs.
looks like I couldnt find “coroutine.cancel()”

Error when i used it
image

Code:


local myWrap = coroutine.create(function()
	while true do
		task.wait(.1)
	end
end)

coroutine.resume(myWrap)


task.wait(10)

coroutine.cancel(myWrap)

EDIT:
using coroutine.wrap
image

Hello!

A better idea would be using task!

here’s an example:

local mySpawn = task.spawn(function()
	while true do
		print("hi")
		task.wait(1)
	end
end)

task.spawn(mySpawn)

task.wait(10)

task.cancel(mySpawn)

P.S if this was a solution, please mark it as one! Help other people out by making them learn!

1 Like