I am attempting for the first time to create a matchmaking system, that teleports 2 random players in game to a set place where they will “battle it out.”
This is the code in the local script:
while true do
wait(5)
local myTableName = {}
if #myTableName < 2 then
local players = game.Players:GetChildren()
local randomplayer1 = players[math.random(1, #players)]
local randomplayer2 = players[math.random(1, #players)]
table.insert(myTableName, randomplayer2)
table.insert(myTableName, randomplayer1)
wait()
if randomplayer1 == randomplayer2 then
print("SamePlayer")
continue
end
print(""..randomplayer1.Name.."")
print(""..randomplayer2.Name.."")
local playerIndex = myTableName[math.random(1,#myTableName)]
local nextplayerIndex = myTableName[math.random(1,#myTableName)]
repeat wait()
if nextplayerIndex == playerIndex then
nextplayerIndex = myTableName[math.random(1,#myTableName)]
end
until nextplayerIndex ~= playerIndex
print(""..randomplayer1.Name.." is matched with"..randomplayer2.Name.."!")
local playersList = {randomplayer1, randomplayer2}
--- Teleport players
local playert1,playert2 = unpack(playersList)
script.PartyTP:FireServer(6333006474, playert1,playert2)
--game:GetService("TeleportService"):TeleportPartyAsync(6333006474, TOTPLAYERS)
for k in pairs (myTableName) do
myTableName [k] = nil
end
continue
end
end
And this is the code in the Server Script:
script.Parent.OnServerEvent:Connect(function(ID, playert1,playert2)
game:GetService("TeleportService"):TeleportPartyAsync(6333006474, playert1,playert2)
end)
I keep getting the error “unable to cast value to object”, which is LINE 2 in the Server Script. Can someone help? Why is this happening?
