How to remove a player from array after they leave?

I am making a game and I have a lobby system. The lobby system works but, when someone in the lobby leaves or once the players are teleported to the sub-place it doesn’t remove them. This causes the script to not realize that there are no players in the lobby and not reset the timer and teleport. I can’t test in Roblox Studio due to the reserved server but I can test in an actual server.

This is my current code:

local entered = {}
local gui = game.Workspace.Exit.gui.BillboardGui.Frame
local RemoteEvent = game.ReplicatedStorage.Lobby
local timer = Instance.new("NumberValue")
timer.Value = 10
local vtimer = gui.timer
local teleporting = Instance.new("BoolValue")
teleporting.Value = false
local vteleporting = gui.status
local TeleportService = game:GetService("TeleportService")
local placeid = 8964283433
local teleportoptions = Instance.new("TeleportOptions")
teleportoptions.ShouldReserveServer = true
local ReservedServerCode = TeleportService:ReserveServer(placeid)

vteleporting.Text = " "

timer:GetPropertyChangedSignal("Value"):Connect(function()
	if timer.Value < 1 then
		teleporting.Value = true
		vteleporting.Text = "Teleporting..."
	else
		teleporting.Value = false
		vteleporting.Text = " "
	end
end)

teleporting:GetPropertyChangedSignal("Value"):Connect(function()
	if teleporting.Value == true then
		TeleportService:TeleportToPrivateServer(placeid, ReservedServerCode, entered)
	end
end)	

RemoteEvent.OnServerEvent:Connect(function(player, request)
	if request == "Enter" then
		player.Character.HumanoidRootPart.CFrame = game.Workspace.Exit.gui.CFrame
		table.insert(entered, player)
	else
		player.Character.HumanoidRootPart.CFrame = game.Workspace.leavepart.CFrame
		for index = #entered, 1, -1 do
			if entered[index] == player then
				table.remove(entered, index)
			end
		end
		
	end
	gui.players.Text = "Players: "..#entered
end)

while true do
	task.wait()
	if #entered > 0 then
		while timer.Value > 0 and #entered > 0 do
			task.wait(1)
			timer.Value -= 1
			vtimer.Text = "Timer: " .. timer.Value
		end
	else
		timer.Value = 10
		vtimer.Text = "Timer: " .. timer.Value
	end
end

I don’t know how to detect if a player in the array left and how to remove them if so.

(If possible can someone also tell me how to disable the proximity prompts I use when it is teleporting?)

You could make use of the game.Players.PlayerRemoving event which you can get the name of the player that is leaving from and simply remove that name from the array

1 Like

Do I need to detect if they are in the array or not? If so how would I do that?

1 Like

you could use a for loop to search each value of the array and compare the value to the players name

2 Likes

table.remove(entered, table.find(entered, player))

1 Like

Neither of those worked. Is there a way I can make the script not error when I run it in studio other than removing the teleport?

if not game:GetService("RunService"):IsStudio() then

end

if you embed the teleport in this then it shouldnt run if the game is running through roblox studio

Thank you, I had to put the variable that gets the reserved server code inside it too but it works in studio now.

Boosting so more people see this.

Boosting once again because I still need a solution.

So I changed some code and know in studio if someone leaves it works but no one else can enter the lobby. And if someone gets teleported in a normal server it just does the same thing.

This is due to a problem in the the iteration. If there are for example 15 players in entered table, and the player is in the 13th position, it will continue on until the table gets to 1. You can remove this unnecessary iteration by doing:

for index = #entered, 1, -1 do
    if entered[index] == player then
        table.remove(entered, index)
        break -- breaks the loop once the player has been founded.
    end
end

Also here, it is suggested that you do here that you keep the table new again for the next sets of players:

teleporting:GetPropertyChangedSignal("Value"):Connect(function()
    if teleporting.Value == true then
        TeleportService:TeleportToPrivateServer(placeid, ReservedServerCode, entered)
        entered = {}
    end
end)

That works but afterwards the player doesn’t get teleported into the lobby waiting room if they enter for some reason.

I still need help with this, please help.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.