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.
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
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
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
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
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
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)
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)