I am trying to write a lobby teleport script (similar to DBZ: Final Stand) where the game randomly selects two players and teleports them to a separate match. My code returns as an error ‘attempt to index nil’ value.
Script I wrote:
while true do
wait(5)
local AlreadyInTable = false
local PickedPlayersTable = {}
local players = game.Players:GetChildren()
local randomplayer1 = players[math.random(1, #players)]
local randomplayer2 = players[math.random(1, #players)]
for _,OtherPlayer in next,PickedPlayersTable do
if OtherPlayer == randomplayer2 then
AlreadyInTable = true
print(""..randomplayer2.Name.." is already in the table.")
end
end
if not AlreadyInTable then
table.insert(PickedPlayersTable, randomplayer2)
end
wait(3)
for i, v in pairs(PickedPlayersTable:GetChildren())do
wait()
game:GetService("TeleportService"):Teleport(6333006474, v)
for k in pairs (PickedPlayersTable) do
PickedPlayersTable [k] = nil
end
end
end