How To Fix Round Ending Problems

Ending the round if the player leave the server while the round is on and if the players touched a part

Alright, so let me explain.

  • Sometimes, when the player leave the server while the round is on-going then the other players have to wait until the time to ran out to start another round
  • If finished the obby round and teleported back to the lobby (I wanted to put a part inside the lobby to disconnect the player from the round) and let other who’s still alive to do the obby

This is the part of the script where it ends the round if all player die. (I assumed that we don’t need the whole script since it’s too long and this is the part we need)

for i,player in pairs(game.Players:GetPlayers()) do
			local character = player.Character or player.CharacterAdded:Wait() --The character of the player.
			character:WaitForChild("HumanoidRootPart").CFrame = CurrentMap.SpawnPoint.CFrame --Every map needs to contain a part called SpawnPoint
			table.insert(alive, player)
			local conn
			conn = character:WaitForChild("Humanoid").Died:Connect(function() --When the player died
				local pos = table.find(alive, player) --Find if he existed in the table
				if(pos) then
					table.remove(alive, pos)
					conn:Disconnect() --Stop following the player's death
				end
			end)
		end
3 Likes

So do you mean that you want to end the round when a player leaves the game (Server) and the other players die?

((Just to check if I understood correctly))

Here’s the senario
Scenario 1
There’s a full server with 20 players then a round started. One player decided leaved (19 player are still in the server and playing that round). Then all of that 19 player died in that round and there’s still a lot of time left in the system. BUT if no one died, the round will still run

What I wanted

I just want to disconnect or remove that player from round since (s)he was counted as a player who will be teleported in the round before (s)he leave

Scenario 2
Same as scenario 1 but this time when a player touched a part, he will be disconnected as a player on that round. Other player who haven’t still finished that round, it’ll still run

*Sorry if it’s not clear, it is hard to explain :persevere:

1 Like

Yes yes I understood, just wait I will try to find out a solution. :+1:

2 Likes

I’ve made a quick amendment to your script as you don’t need to use connections as it automatically disconnects when the player dies; this is because the player has a new character upon respawn:

for i,player in pairs(game.Players:GetPlayers()) do
	local character = player.Character or player.CharacterAdded:Wait()
	character:WaitForChild("HumanoidRootPart").CFrame = CurrentMap.SpawnPoint.CFrame
	table.insert(alive, player.Name)
	
	character:WaitForChild("Humanoid").Died:Connect(function()
		local pos = table.find(alive, player.Name)
		
		if pos then
			table.remove(alive, pos)
		end
	end)
end

If you’re looking for a way to remove the player from the table when they disconnect, you can use the PlayerRemoving event in the same script:

game.Players.PlayerRemoving:Connect(function(player)
	local pos = table.find(alive, player.Name)
	
	if pos then
		table.remove(alive, pos)
	end
end)

And finally, you’re going to require the Touched event for when the player touches a part to teleport the player back to the lobby, alongside the use of a debounce so it doesn’t fire multiple times:

local endPart = workspace.EndPart -- reference the part you want the player to touch here
local debounce = {}

endPart.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and table.find(alive, hit.Parent.Name) and not debounce[hit.Parent.Name] then
		debounce[hit.Parent.Name] = true
		
		-- add a line to teleport the player to the lobby
		
		wait(1)
		
		debounce[hit.Parent.Name] = false
	end
end)
3 Likes

Ok, sorry for being late.
I have got an idea but I don’t think that it is what you want (Just forgive me, I am noob in scripting :confused: )

Anyway, you first put 2 teams in the teams file
image

And then you will put a script that puts the player in the losers team, I used a killbick script just for a sample

> local Brick = script.Parent
> 
> 
> local function PlayerTouched(Part)
> 	local Parent = Part.Parent
> 	if game.Players:GetPlayerFromCharacter(Parent) then
> 		local plr = game.Players:GetPlayerFromCharacter(Parent)	
> 		Parent.Humanoid.Health = 0
> 		plr.Team = game.Teams.Losers
> 
> 	end
> end
> 
> Brick.Touched:connect(PlayerTouched)

And then you will detect if there are players still in the “On Going” Team. If there is not one then the round will end

> RoundOn = true
> local team = game.Teams["On Going"]
> 
> while RoundOn do
> 	
> 	wait(0.5)
> 	
> 	for i,players in pairs(game.Players:GetPlayers()) do
> 		
> 		if #team:GetPlayers() > 0 then
> 			print("Round is on going")
> 		else
> 			
> 			print("Round finished")
> 			RoundOn = false
> 		end
> 	end
> end

I hope this helps you, if not then sorry, I tried :pensive:

1 Like

Thanks for trying. If it doesn’t work, sorry for wasting your time but hey at least you tried! I’m also noob at scripting so I need someone’s guide

1 Like

You can make a table of the active players and make a function which checks it.
For example when the round starts, you can put all usernames of the active players to the table. And then if someone left or died then you just remove his username from the table. Use the function table.getn() which returns the number of elements in the table which you passed. And when you have the number of the players then you can just script whatever you want (when everyone left or other situations).

I just gave an idea, but if you need any help with the code then ask me, I am ready to help you.

2 Likes

Can you please help me with the code? I’m not that good at scripting, sorry

Sure. This code is also just a simple explanation of the situation.

local playersInRound = {} -- table of the players in the round.

-- Intermission

for i,player in pairs(game.Players:GetChildren()) do -- for all players in the general table.
	table.insert(playersInRound,player.Name) -- add all players who was in the lobby to other table (Players in round).
end

-- Teleporting Players.
-- Beginning of the round and its progress.

game.Players.PlayerRemoving:Connect(function(player) -- fires every time when the player leaves.
	if table.find(playersInRound,player.Name) then -- if the player was in the round then it removes his name from the PlayersInRound table.
		table.remove(playersInRound,table.find(playersInRound,player.Name))
		local numberOfThePlayersInRound = table.getn(playersInRound) -- Returns the number of the elements in the passed table. (Our case is the number of players in the round).
		if numberOfThePlayersInRound == 0 then -- if number == 0 then Stop the round system.
			StopRoundProgress() -- This function is just mine example (You need to pass your own).
		end
	end	
end)

If you need More help with this situation then feel free to ask me in D.M and I will help you with your specific situation :wink:

1 Like

Maybe you could do something like this:

local function disconnect()
table.remove(player)
end
game.Players.PlayerRemoving:Connect(function(player)
local playerWasinRound = table.find(player)
if playerWasinRound then
disconnect()
end)

Yes, this method is also good but he also needs to have the Count of players in the round. Your function works but this system doesnt know how much players are in the round. You need also have a count of active players to stop the round if everyone left (who was in the round).

1 Like

Well, I’ll implement that in soon and repost it.

Maybe you could do something like this:

local function disconnect()
table.remove(player)
end
function disconnectallplayers()
char.HumanoidRootPart.Position = workspace.Lobby.Position
text.Text = "Round Ended"
end

game.Players.PlayerRemoving:Connect(function(player, char)
local playersranout = table.getn(player)
local playerWasinRound = table.find(player)
if playerWasinRound then
disconnect()
elseif playersranout == 0 then
disconnectallplayers()
end
end)
1 Like

This doesn’t work I realised. You will come up with an error if you try this. Fixing the error and resposting.