Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Help with random players?

Asked by 8 years ago
    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.

1 answer

Log in to vote
1
Answered by 8 years ago

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.

0
"Be sure to accept it" HungryJaffer 1246 — 8y
0
It works, but theres an error saying MoveTo is not part of player NinjoOnline 1146 — 8y
0
MoveTo is a property of the player's character, so just .Character after duelplayers[1] and duelplayers[2] on lines 29 and 30. I've also fixed this myself. Spongocardo 1991 — 8y
0
I have discovered a bug. It creates 4 string values inside Duel, 2 for one player and the other 2 for the other. Why is it creating 4, when it should only create 2? NinjoOnline 1146 — 8y
0
Get rid of the for loop on line 19, it's not needed. I'll also edit my answer. Spongocardo 1991 — 8y
Ad

Answer this question