I just recently started trying to learn how to use TeleportService to reserve a private server for two players to teleport to. I have a system made here:
--Services
local teleService = game:GetService("TeleportService")
local Players = game:GetService("Players")
--UI properties
local UI = script.Parent
local Name1 = UI.Name1
local In1 = Name1.In
local ID1 = Name1.userID
local Join1 = Name1.Join1
local Name2 = UI.Name2
local In2 = Name2.In
local ID2 = Name2.userID
local Join2 = Name2.Join2
local Output = UI.Output
local Timer = UI.Timer
--Clickdetectors
local SurfaceUI = script.Parent.Parent
local Blue = SurfaceUI.Blue.BlueClick
local Red = SurfaceUI.Red.RedClick
--Place
local Place = 7047486844
Blue.MouseClick:Connect(function(Player)
if In2.Value == true and Player.UserId ~= ID2.Value then return end
if In1.Value == true and Player.UserId == ID1.Value then return end
if In2.Value == false then
In2.Value = true
Name2.Text = Player.Name
Join2.Text = "Leave"
ID2.Value = Player.UserId
else
In2.Value = false
Name2.Text = "Nobody"
Join2.Text = "Join"
ID2.Value = -1
end
end)
Red.MouseClick:Connect(function(Player)
if In1.Value == true and Player.UserId ~= ID1.Value then return end
if In2.Value == true and Player.UserId == ID2.Value then return end
if In1.Value == false then
In1.Value = true
Name1.Text = Player.Name
Join1.Text = "Leave"
ID1.Value = Player.UserId
else
In1.Value = false
Name1.Text = "Nobody"
Join1.Text = "Join"
ID1.Value = -1
end
end)
while wait(1.1) do
Timer.Text = Timer.Text - 1
if Timer.Text == "0" then
local code = teleService:ReserveServer(Place)
Output.Text = "Finding players..."
local User1
local User2
for i, plyr in pairs(Players:GetChildren()) do
if plyr.UserId == ID1.Value then
User1 = plyr
elseif plyr.UserId == ID2.Value then
User2 = plyr
end
end
if User1 and User2 then
repeat wait() until code
teleService:TeleportToPrivateServer(Place, code, User1, User2)
Output.Text = "Teleporting!"
repeat wait() until User1 == nil and User2 == nil
ID1.Value = -1
ID2.Value = -1
Name1.Text = "Nobody"
Name2.Text = "Nobody"
Join1.Text = "Join"
Join2.Text = "Join"
In1.Value = false
In2.Value = false
Output.Text = "Counting down..."
Timer.Text = "40"
elseif User1 ~= nil and User2 == nil or User1 == nil and User2 ~= nil then
Timer.Text = "40"
Output.Text = "Counting down..."
elseif User1 == nil and User2 == nil then
Timer.Text = "40"
Output.Text = "Counting down..."
end
end
end
Essentially if a player is in the queue an IntValue is stored that has the player’s UserId. It then runs a for loop once the timer hits 0 that checks all player’s user IDs and sees if it matches the one provided in the IntValues. Then I have two empty variables assigned to each player, and I try teleporting them.
The code works, the only error that is produced is the actual command that tells them to teleport to the private server. The error says that it cannot cast a value to Object:
The third parameter of TeleportToPrivateServer requires a table of players to teleport, if you reference it like that you’re creating a new separate parameter for the function to detect (Or User2, which would actually be the fourth parameter)
Try this perhaps?
--Services
local teleService = game:GetService("TeleportService")
local Players = game:GetService("Players")
--UI properties
local UI = script.Parent
local Name1 = UI.Name1
local In1 = Name1.In
local ID1 = Name1.userID
local Join1 = Name1.Join1
local Name2 = UI.Name2
local In2 = Name2.In
local ID2 = Name2.userID
local Join2 = Name2.Join2
local Output = UI.Output
local Timer = UI.Timer
--Clickdetectors
local SurfaceUI = script.Parent.Parent
local Blue = SurfaceUI.Blue.BlueClick
local Red = SurfaceUI.Red.RedClick
--Place
local Place = 7047486844
local PlayersToTeleport = {}
Blue.MouseClick:Connect(function(Player)
if In2.Value == true and Player.UserId ~= ID2.Value then return end
if In1.Value == true and Player.UserId == ID1.Value then return end
if In2.Value == false then
In2.Value = true
Name2.Text = Player.Name
Join2.Text = "Leave"
ID2.Value = Player.UserId
else
In2.Value = false
Name2.Text = "Nobody"
Join2.Text = "Join"
ID2.Value = -1
end
end)
Red.MouseClick:Connect(function(Player)
if In1.Value == true and Player.UserId ~= ID1.Value then return end
if In2.Value == true and Player.UserId == ID2.Value then return end
if In1.Value == false then
In1.Value = true
Name1.Text = Player.Name
Join1.Text = "Leave"
ID1.Value = Player.UserId
else
In1.Value = false
Name1.Text = "Nobody"
Join1.Text = "Join"
ID1.Value = -1
end
end)
while wait(1.1) do
Timer.Text = Timer.Text - 1
if Timer.Text == "0" then
local code = teleService:ReserveServer(Place)
Output.Text = "Finding players..."
local User1
local User2
for i, plyr in pairs(Players:GetChildren()) do
if plyr.UserId == ID1.Value then
User1 = plyr
elseif plyr.UserId == ID2.Value then
User2 = plyr
end
end
if User1 and User2 then
--Since User1 & User2 are Player UserIds, we have to convert them into Player objects for it to work
local Player1 = game.Players:GetPlayerByUserId(User1)
local Player2 = game.Players:GetPlayerByUserId(User2)
table.insert(PlayersToTeleport, Player1)
table.insert(PlayersToTeleport, Player2)
repeat wait() until code
teleService:TeleportToPrivateServer(Place, code, PlayersToTeleport)
Output.Text = "Teleporting!"
repeat wait() until User1 == nil and User2 == nil
ID1.Value = -1
ID2.Value = -1
Name1.Text = "Nobody"
Name2.Text = "Nobody"
Join1.Text = "Join"
Join2.Text = "Join"
In1.Value = false
In2.Value = false
Output.Text = "Counting down..."
Timer.Text = "40"
elseif User1 ~= nil and User2 == nil or User1 == nil and User2 ~= nil then
Timer.Text = "40"
Output.Text = "Counting down..."
elseif User1 == nil and User2 == nil then
Timer.Text = "40"
Output.Text = "Counting down..."
end
end
end
This is server-sided. Here is a portion of the code:
while wait(1.1) do
Timer.Text = Timer.Text - 1
if Timer.Text == "0" then
local code = teleService:ReserveServer(Place)
Output.Text = "Finding players..."
local User1
local User2
for i, plyr in pairs(Players:GetChildren()) do
if plyr.UserId == ID1.Value then
User1 = plyr
elseif plyr.UserId == ID2.Value then
User2 = plyr
end
end
if User1 and User2 then
local Plyr1 = game.Players:GetPlayerByUserId(User1)
local Plyr2 = game.Players:GetPlayerByUserId(User2)
table.insert(PlyrsToTeleport, Plyr1)
table.insert(PlyrsToTeleport, Plyr2)
repeat wait() until code
teleService:TeleportToPrivateServer(Place, code, PlyrsToTeleport)
Output.Text = "Teleporting!"
(The table is made outside of this called local PlyrsToTeleport = {})
I just noticed that you used this function:
local Plyr1 = game.Players:GetPlayerByUserId(User1)
local Plyr2 = game.Players:GetPlayerByUserId(User2)
User1 and User2 are not PlayerIDs, they are assigned as the actual player. I think that might be what is producing the error.