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)