Bug with teleportation system

I have made this teleportation system for a game, but once they get teleported to the other place, more people can go into their same place.
Is there any way to prevent this?

local pad = script.Parent
local maxPlayers = 4
local currentPlayers = 0
local standingPlayers = {}
local countdown = 11
local isTimerRunning = false
local destinationPlaceId = 1 --pretend this is my place id

local frame = script.Parent.BillboardGui3.ScrollingFrame

local function teleportPlayers()
	local playersToTeleport = {}
	for index, player in pairs(standingPlayers) do
		if player and player.Parent == game.Players then
			table.insert(playersToTeleport, player)
		end
	end

	if #playersToTeleport > 0 then
		

		local TeleportService = game:GetService("TeleportService")
		TeleportService:TeleportAsync(destinationPlaceId, playersToTeleport)
	else
		
	end
end

local function startCountdown()
	isTimerRunning = true
	local timer = countdown

	while timer > 0 do
		timer -= 1
		script.Parent.BillboardGui.TextLabel.Text = timer.. "   ".. #standingPlayers .. "/" .. maxPlayers.. " PLAYERS"
		if #standingPlayers == 0 then
			script.Parent.BillboardGui.TextLabel.Text = countdown -1 .. "   ".. #standingPlayers .. "/" .. maxPlayers.. " PLAYERS"
			isTimerRunning = false
			break
		end
		task.wait(1)

	end


		task.wait(0)
		
		timer = countdown
	

	teleportPlayers()
	isTimerRunning = false
end

local function onTouch(hit)
	local character = hit.Parent
	local player = game.Players:GetPlayerFromCharacter(character)
local foundplayer = false
for i,v in pairs(standingPlayers) do
	if v == player then
		foundplayer = true
	end
end

	if player and not standingPlayers[player] and currentPlayers < maxPlayers  and foundplayer == false then
		table.insert(standingPlayers,player)
		currentPlayers += 1
		
		if frame:FindFirstChild(player.Name) then
		else
			local hh = script.ListText:Clone()
			hh.Parent = frame
			hh.Name = player.Name
			hh.Text = player.Name
		end

		if not isTimerRunning then
			startCountdown()
		end
	end
end

local function onLeave(hit)
	local character = hit.Parent
	local player = game.Players:GetPlayerFromCharacter(character)
	
	local index
	for i,v in pairs(standingPlayers) do
		if v == player then
			index = i
		end
	end

	if player and index then
		table.remove(standingPlayers,index)
		currentPlayers -= 1
		
		if frame:FindFirstChild(player.Name) then
			frame:FindFirstChild(player.Name):Destroy()
		else
			
		end
	end
end

pad.Touched:Connect(onTouch)
pad.TouchEnded:Connect(onLeave)

Try use a ReservedServer.

TeleportService | Documentation - Roblox Creator Hub

Expanding on what @gravitycoil828 said, Reserved servers are servers that can only be accessed via an access code and can only be joined by having a server send you (Such as in a lobby or matchmaking server).

Here is an example script:

local teleportService = game:GetService("TeleportService")
local placeId = 1234567890 -- Change this to your intended place ID

-- Reserve server
local serverId = teleportService:ReserverServer(placeId)

-- Teleport players
teleportService:TeleportToPrivateServer(placeId, serverId, game.Players:GetChildren()) -- Replace game.Players:GetChildren() with a table containing the players you want to teleport
2 Likes

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