The last couple of days I've been trying to make a script that teleports multiple people that are standing on a Part to a private server. And currently, I'm left with a script that partly works, but sometimes comes up with an error:
[ TeleportToPrivateServer must be passed an array of players ]
not sure what could cause the error because it doesn't address at which line it happen, but im pretty sure it's the function at line 37
-- Player and Place Variables --
local Players = game:GetService("Players")
local PlayersTouching = {}
local NeededPlayers = 1
local MaxPlayers = 4
local PlaceId = 0000000000
-- Services --
local RunService = game:GetService("RunService")
local TeleportService = game:GetService("TeleportService")
-- SurfaceGUI's for text --
local PlayerDisplay = script.Parent.Parent.PlayerDisplay.SurfaceGui:WaitForChild("TextLabel")
local StatusDisplay = script.Parent.Parent.StatusDisplay.SurfaceGui:WaitForChild("TextLabel")
local ServerDisplay = script.Parent.Parent.ServerDisplay.SurfaceGui:WaitForChild("TextLabel")
ServerDisplay.Text = "Server Size: " ..MaxPlayers
-- Other --
local Toucher = script.Parent.Parent:WaitForChild("MainPart")
local debounce = true
--/--
function IsPlayer(obj)
if obj.Parent:IsA("Model") and obj.Parent:findFirstChild("Humanoid") then
if Players:GetPlayerFromCharacter(obj.Parent) ~= nil then
return true
end
end
return false
end
-- function that teleports all the people to a private server --
function TeleportPlayers()
local PrivateServerId = TeleportService:ReserveServer(PlaceId)
TeleportService:TeleportToPrivateServer(PlaceId, PrivateServerId, PlayersTouching)
end
-- functions that adds or removes players from the table --
Toucher.Touched:connect(function(obj)
if IsPlayer(obj) then
local CurChar = obj.Parent
local CurPlayer = Players:GetPlayerFromCharacter(CurChar)
local index
for i,v in pairs(PlayersTouching) do
if v == CurPlayer then
index = i
end
end
if not index then
table.insert(PlayersTouching, CurPlayer)
end
return CurPlayer
end
end)
Toucher.TouchEnded:connect(function(obj)
if IsPlayer(obj) then
local CurChar = obj.Parent
local CurPlayer = Players:GetPlayerFromCharacter(CurChar)
local index
for i,v in pairs(PlayersTouching) do
if v == CurPlayer then
index = i
end
end
if index then
table.remove(PlayersTouching, index)
end
end
end)
-- Main function that monitors everything --
RunService.Stepped:connect(function()
if debounce then
-- This just changes the text of GUI's
if #PlayersTouching >= NeededPlayers then
PlayerDisplay.Text = "Players: " ..#PlayersTouching.. "/ "..MaxPlayers
else
PlayerDisplay.Text = "Players: " ..#PlayersTouching.. "/ "..MaxPlayers
end
if #PlayersTouching >= MaxPlayers then
debounce = false
StatusDisplay.Text = "Status: Teleporting..."
TeleportPlayers() -- runs the function ( line 38 )
wait(8)
-- This loop will clear the table so next array of players can use the teleporter
for k in pairs (PlayersTouching) do
PlayersTouching [k] = nil
end
wait(1)
StatusDisplay.Text = "Status: Waiting For Players"
PlayerDisplay.Text = "Players: " ..#PlayersTouching.. "/ "..MaxPlayers
debounce = true
end
end
end)
Thank you for your attention
