I want to make a lobby system by myself. I have the problem with TeleportService:TeleportAsync()
Here’s the serverscript inside a part :
-- change settings here
local maxplayers = 2
local maxtimer = 20
local placeID = 17267036049
-- other variables
local list = {}
local playerExistsInLobby = false
local teleportProcessing = false
local timer
local frame = script.Parent.SurfaceGui.Frame
local enterpart = script.Parent.Parent.waitingpoint
local cablecar_basepart = script.Parent.Parent.CableCar.Base
local replicatedstorage = game:GetService("ReplicatedStorage")
local teleportservice = game:GetService("TeleportService")
local server = teleportservice:ReserveServer(placeID)
local teleportOptions = Instance.new("TeleportOptions")
teleportOptions.ShouldReserveServer = true
teleportOptions.ReservedServerAccessCode = server
local function updateplayer ()
frame.PlayerNum.Text = #list .. "/" .. maxplayers
end
-- start values
frame.PlayerNum.Text = #list .. "/" .. maxplayers
-- running values
replicatedstorage.LeaveGuiEvent.OnServerEvent:Connect(function (player)
for i = 1, #list do
if list[i] == player.Character.Name then
player.Character.Humanoid.Jump = true
task.wait(.1)
player.Character.Humanoid.Jump = true
task.wait(.1)
player.Character.Humanoid.Jump = true
player.Character.HumanoidRootPart.CFrame = script.Parent.Parent.ExitLobbyPart.CFrame
table.remove(list, i)
if #list < 0 then
playerExistsInLobby = false
teleportProcessing = false
end
task.wait(.1)
updateplayer()
end
end
end)
-- when touches a wall
script.Parent.Touched:Connect(function (hit)
if hit.Parent:FindFirstChild("Humanoid") then
if teleportProcessing == false then
local char = hit.Parent
local players = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
local playerenter = false
-- do things here and check the teleport process or max players
for i = 1, #list do
if list[i] == char.Name then
playerenter = true
end
end
if playerenter == false then
if #list < maxplayers then
table.insert(list, char.Name)
char:FindFirstChild("HumanoidRootPart").CFrame = enterpart.CFrame
updateplayer()
print(list)
replicatedstorage.LeaveGuiEvent:FireClient(players, true)
for i = 1, #list do
if #list >= 1 then
if list[i] == char.Name then
playerExistsInLobby = true
end
else
playerExistsInLobby = false
end
end
end
-- check if player left then remove character name from the table
game:GetService("Players").PlayerRemoving:Connect(function (plrWhoGetsRemovedOrLeft)
if plrWhoGetsRemovedOrLeft.Name == char.Name then
for i = 1, #list do
if list[i] == plrWhoGetsRemovedOrLeft.Name then
table.remove(list, i)
updateplayer()
print(#list , list[1] , list[2])
if teleportProcessing == true then
if #list <= 0 then
teleportProcessing = false
end
end
end
end
end
end)
-- check if player die or removed then remove character name from the table
players.CharacterRemoving:Connect(function (charWhoGetsRemovedOrDied)
if charWhoGetsRemovedOrDied.Name == char.Name then
for i = 1, #list do
if list[i] == charWhoGetsRemovedOrDied.Name then
table.remove(list, i)
updateplayer()
print(#list , list[1] , list[2])
if teleportProcessing == true then
if #list <= 0 then
teleportProcessing = false
end
end
end
end
end
end)
end
end
end
end)
while task.wait() do
for i = 1, maxtimer do
timer = maxtimer - i
frame.Timer.Text = timer
task.wait(1)
end
if #list > 0 then
if playerExistsInLobby == true then
teleportProcessing = true
frame.Status.Text = "Teleporting"
for i = 1, #list do
if list[i] then
local player = game:GetService("Players"):FindFirstChild(list[i])
-- runs when player is exist and being teleported
replicatedstorage.LeaveGuiEvent:FireClient(player, false)
task.wait(5)
teleportservice:TeleportAsync(placeID, player, teleportOptions)
task.wait(5)
teleportProcessing = false
end
end
repeat task.wait() until not teleportProcessing
frame.Status.Text = "Ready"
playerExistsInLobby = false
for i = 1, #list do
if list[i] then
table.remove(list, i)
updateplayer()
end
end
end
end
end
Here’s the short line of codes :
local placeID = 17267036049
local teleportservice = game:GetService("TeleportService")
local server = teleportservice:ReserveServer(placeID)
local teleportOptions = Instance.new("TeleportOptions")
teleportOptions.ShouldReserveServer = true
teleportOptions.ReservedServerAccessCode = server
if #list > 0 then
if playerExistsInLobby == true then
teleportProcessing = true
frame.Status.Text = "Teleporting"
for i = 1, #list do
if list[i] then
local player = game:GetService("Players"):FindFirstChild(list[i])
-- runs when player is exist and being teleported
replicatedstorage.LeaveGuiEvent:FireClient(player, false)
task.wait(5)
teleportservice:TeleportAsync(placeID, player, teleportOptions)
task.wait(5)
teleportProcessing = false
end
end
repeat task.wait() until not teleportProcessing
frame.Status.Text = "Ready"
playerExistsInLobby = false
for i = 1, #list do
if list[i] then
table.remove(list, i)
updateplayer()
end
end
end
end
I tried printing the “player” and it gives player name. And tried printing the player classname and it gives “Player” class. I’m stuck with this issue for days. Also the Reserve Server is in teleportOptions. There was an error HTTP 403 Forbidden, but It was an error from roblox studio so that’s not the issue.