Round system script mid-match leaving error (can't seem to fix it)

Hello, the code below is code for a basic round system but I’ve run into a bug that I can’t seem to fix. Basically, when the chosen player leaves the game mid-match everyone is supposed to be teleported back to the lobby and the intermission will continue as usual. The bug is that after everyone is teleported back to the lobby, the intermission text gui just says “Intermission: 0” for the duration of the game time (after this the game resumes to normal). So if you know how to fix this, please let me know! Also if you need the code for the text gui’s let me know.

local rp = game:GetService("ReplicatedStorage")

local countdownvalue = rp:WaitForChild("CountdownValue")
local ingamevalue = rp:WaitForChild("InGameValue")
local ingamecountdownvalue = rp:WaitForChild("InGameCountdownValue")

local minplayers = 2

local teleportchosenpart = game.Workspace:WaitForChild("TeleportChosen")
local teleportpart = game.Workspace:WaitForChild("TeleportPart")
local lobbyspawn = game.Workspace:WaitForChild("LobbySpawn")

function mainfunction()
	print("at the start")
	for i = 10,0,-1 do
		countdownvalue.Value = i
		wait(1)
		if #game:GetService("Players"):GetPlayers() < minplayers then
			return
		end
	end
	for _,plr in ipairs(game.Players:GetChildren()) do
		plr.Character.HumanoidRootPart.CFrame = teleportpart.CFrame
	end
	local players = game.Players:GetPlayers()
	local chosenplayer = players[math.random(#players)]
	local chosenplayercharacter = chosenplayer.Character or chosenplayer.CharacterLoaded:Wait()
	chosenplayercharacter.HumanoidRootPart.CFrame = teleportchosenpart.CFrame
	ingamevalue.Value = true
	for i = 15,0,-1 do
		ingamecountdownvalue.Value = i
		wait(1)
		game.Players.PlayerRemoving:Connect(function(player)
			print("in removed")
			if player.Name == chosenplayer.Name then 
				print("in main")
				for _,plr in ipairs(game.Players:GetChildren()) do
					plr.Character.HumanoidRootPart.CFrame = lobbyspawn.CFrame
				end
				ingamevalue.Value = false
				return 
			end
		end)
	end
	ingamevalue.Value = false
	for _,plr in ipairs(game.Players:GetChildren()) do
		plr.Character.HumanoidRootPart.CFrame = lobbyspawn.CFrame
	end
end

while true do
	wait()
		if #game:GetService("Players"):GetPlayers() >= minplayers then
			mainfunction()
		else
			countdownvalue.Value = "Waiting for players..."
	end
end

1 Like

Bruh,why many people us Getplayers()

I have a post regarding about this

CHARACTER MUST USE GET CHILDREN NOT GET PLAYERS

Local players = game.Players:GetPlayers()

Change that line to

Local players = gamr.Players:GetChildren()

IF THERE IS CHARACYER INVOVLED

This should work very well

1 Like

So now you have explained the problem, what is your goal here? I don’t understand what you are trying to do.

Please reread your post which you have made NOT LONG AGO before you even post that reply. That is complete misinformation which has nothing to do with the OP’s goal.

2 Likes

Hi, the goal is to remove the bug of it saying “Intermission: 0” for the duration of the game time, instead it should instantly start the intermission without waiting for the duration of the game time to end.

1 Like

In that case, you need to make a variable where it detects when a specific player leaves the game. From your code it looks like you are connecting the Players.PlayerAdded event everytime the for loop completes one iteration which is unnecessary memory leak and can tank up the server’s performance.

You should only connect the event once, and when a player leaves the game, set a variable to true boolean value, then whenever the for loop is doing one iteration, check if that variable is true, and if it is, that means you need to break the for loop.

1 Like

In my code I’d only want the script to break if the chosen player leaves, but with the explanation above how would I detect if it is the chosen player who left?

1 Like

WIth this line of code? Apply this line of code.

1 Like

Ok, and should I also change the return below that to break?

1 Like

No. The return statement is used when you use a function and the function is expected to give back a value. The break statement is used when you want to stop a for or while loop from continuing its iterations.

1 Like

In this case I was using return to restart the code from the beginning of the code/function (either one works), would that work using return?

1 Like

His one got character,brother pls read the post

1 Like

His one got character,you go and see the code, only the line on get players is changed,the rest can keep the same

I already told u and i will say my error fixing will 100 percent work one

1 Like

If no character then yes you are right,but he has the character

GET PLAYERS() IS ONLY FOR THE PLAYER(CANNOT GET THE CHARACTER ONE)

GETchildren() CAN GET CHARACTER AND THE PLAYER STATS,I HAVE THAT PROBLEM TOO,JUST CHANGE THAT CODE TO GET CHILDREN WILL WORK AS NORMAL

1 Like

My mistake,but my one is not misinformation, you dont anyhow say,i literally created that post not long time ago,

1 Like

Go and see this ,this is the correct one

Dont read the wrong thing sent by people,go and read the things,there is also one post about cannot teleport to that location due to the mixups of getplayers and get children

Dont read long time ago posts,they are not so accurate anymore

It is still the main cause of why the intermission is stuck at zero as a print output,cause it cant identify the character again.

1 Like

No. Thé return statement in your current code does not restart the whole thing. This return statement is used to stop the anonymous function inside the event.

1 Like

You can just iterate the table returns by this function, and for each player just refer to the Character property.

2 Likes

game.Players:GetPlayers() will look for CHILDREN that are players. gameplayers:GetChildren() will look for CHILDREN that can be anything, not just players.

get it yet?

1 Like

Hi your script is currently creating a new PlayerRemoving connection each time you loop over it, which is 15 times for each round. Keep any connection outside of the round script (if possible) or at least disconnect the connection at the end of the loop to prevent duplicates.

Currently after 5 rounds you’re having 75 duplicate connections of the same event.

Good practice is to create connections outside any loops if possible, since there is no need to disconnect or create connections on new, that you use very frequently. Create it once, use it often, disconnect when not used in the near future.

Also use GetPlayers over GetChildren when fetching players, do not listen to hellothere.

Show me that you can get character via getPlayers() i bet u cannot