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