while wait() do contestants = {} for _, player in pairs(game.Players:GetPlayers()) do if player and player.Character then local humanoid = player.Character:WaitForChild("Humanoid") if humanoid and humanoid.Health > 0 then table.insert(contestants, player) end end end if #contestants >= 2 then break else statustag.Value = "Waiting for enough players" timertag.Value = -1 end end for _, player in pairs(contestants) do local duelplayers = {} local randomplayers = contestants[math.random(2, #contestants)] table.insert(duelplayers, randomplayers) statustag.Value = duelplayers[1].Name .. " vs. " .. duelplayers[2].Name -- ERROR HERE (163) Instance.new("StringValue", script.Dueling).Name = duelplayers[1].Name Instance.new("StringValue", script.Dueling).Name = duelplayers[2].Name duelplayers[1]:MoveTo(arena:WaitForChild("Spawn1").Position) duelplayers[2]:MoveTo(arena:WaitForChild("Spawn2").Position) end
20:42:38.388 - ServerScriptService.MainScript:163: attempt to index field '?' (a nil value)
I'm trying to get 2 random players out of the 'contestants' table and then I want to create a new Instance with the Name of the 'StringValue' to the name of the first player and then another of the second player, then teleport the player to spawn1 and spawn2. I don't know what the error is, but I think it has something to do with how I am making setting the tables out. Thanks.
You need to make two separate variables for the two players.
The way you're doing is getting a random player from the second player to the last player in the table, so the first player in the table is totally ignored. Since you're adding only one player, the script errors because duelplayers[2] is nil.
You should get something like this when you're done:
local player1,player2 --Creates the player1 and player2 variables for use in the script. while wait() do contestants = {} for _, player in pairs(game.Players:GetPlayers()) do if player and player.Character then local humanoid = player.Character:WaitForChild("Humanoid") if humanoid and humanoid.Health > 0 then table.insert(contestants, player) end end end if #contestants >= 2 then break else statustag.Value = "Waiting for enough players" timertag.Value = -1 end end local duelplayers = {} player1,player2 = contestants[math.random(#contestants)],contestants[math.random(#contestants)] --Multiple assignment to assign two variables on one line. if player1 == player2 then repeat player2 = contestants[math.random(#contestants)] until player1 ~= player2 end --If the first player is the second player, then it finds another random player until player1 is not player2. table.insert(duelplayers, player1) --Add player1 to the duelplayers table. table.insert(duelplayers, player2) --Add player2 to the duelplayers table. statustag.Value = duelplayers[1].Name .. " vs. " .. duelplayers[2].Name --This line should not error because duelplayers[1] and duelplayers[2] should exist. Instance.new("StringValue", script.Dueling).Name = duelplayers[1].Name Instance.new("StringValue", script.Dueling).Name = duelplayers[2].Name duelplayers[1].Character:MoveTo(arena:WaitForChild("Spawn1").Position) duelplayers[2].Character:MoveTo(arena:WaitForChild("Spawn2").Position)
I hope my answer helped you. If it did, be sure to accept it.