Why does this not work?

Why does this script not work?

local playerTable = game:GetService(“Players”):GetPlayers()

while true do
wait(5)
if (#playerTable < 2) then
print(“not enouhg players”)
else
print(“enough players”)
local randomPlayer1 = playerTable[math.random(1, #playerTable)]

  table.remove(playerTable, table.find(playerTable, randomPlayer1))

  local randomPlayer2 = playerTable[math.random(1, #playerTable)]

  print(randomPlayer1.Name ..   randomPlayer2.Name)

end
end

What is it meant to do? What do you mean by not working?

It’s suppose to find two random players from players and print their names but if there is not enough players in the table it prints not enough players.

also it just keeps on printing not enough players

even though there is 3 players in the game

You never update playerTable so it’s always empty. The proper code would be:

local playerTable = game:GetService(“Players”):GetPlayers()

while true do
    wait(5)
    if (#playerTable < 2) then
        print(“not enouhg players”)
    else
        print(“enough players”)
        local randomPlayer1 = playerTable[math.random(1, #playerTable)]

        table.remove(playerTable, table.find(playerTable, randomPlayer1))

        local randomPlayer2 = playerTable[math.random(1, #playerTable)]

        print(randomPlayer1.Name ..   randomPlayer2.Name)

    end
    playerTable = game:GetService("Players"):GetPlayers()
end

ok ill see if this works thanks for the help.

It works thanks. Can I ask you one more thing?

No need to ask for permission, what is the question?

Oops, I already did that didnt i.

Ok well thanks for the help! Bye

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.