Attempt to compare number and table - Two random players

if Players:GetChildren() >= 2 then
print(#Participants)
local Player1 = table.remove(Participants, math.random(#Participants))
print(Player1) end

I am receiving this error - ServerScriptService.main:23: attempt to compare number and table
I’m sure its a simple mistake, but I’ve been looking around and nothing pertaining to this on various other forums either that or I’m just blind.
I was using the :GetPlayer() method, but still had the same error - I’m just trying to retrieve a random player from the table of participants, it will end up being two players, but I’m trying to test it in studio first. Thanks in advance.

what do u define on Participants?

Participants = Players:GetPlayers() i presume? Add a hash before Players:GetChildren part

if #Players:GetChildren() >= 2 then

local Participants = {}
– Once they spawn, add them to participants
local function onCharacterAdded(character)
table.insert(Participants, character.Name)
print(character.Name … " has been added to participants.")
end

1 Like

You use math.random and only give it one value, you give it the number of players. Math.random requires a numbers, the minimum number and the maximum number.

You are comparing a table with a number, you need to get the number of things In the array/table

if #Players:GetChildren() >= 2 then -- missing hash

I can’t get how this would randomly remove a value from the Participants table. Try this:

local RandomPlayer = table.remove(Participants, math.random(1, #Participants))

--[[
This would only get one random value of it.
If the table is not numerical, there might be some issues with doing this.
]]

Edit: Fix.

1 Like

Heres the thing, I recieve no error from this, but I’m not getting anything in the output. After I hit test play, of course.
if #Players:GetPlayers() >= 1 then
print(#Participants)
local Player1 = table.remove(Participants, math.random(1, #Participants))
print(Player1)
end

edit : nvm I had it. lol

still no output, though???!

1 Like

Well you say:

local Player1 = table.remove... --more 

You are setting a variable equal to that table.remove statement, not to the player that it is removing.
Try this instead:

if #Players:GetChildren() >= 2 then
     print(#Participants)
     local randomnumber1 = math.Random(1,#Participants)
     local player1 = Participants[randomnumber1]
    table.remove(Participants, randomnumber1)
    print(player1)
  
    --- if you want to find the second player then do this also:
     local randomnumber2 = math.Random(1,#Participants)
     local player2 = Participants[randomnumber2]
     print(player2)
end
1 Like

When using brackets to index something via numbers, if that index exists, it will get it’s value. And table.remove receives a number as #2 index, not a value.

Yeah, I’m still receiving no output?
I’ve even changed if #Players:GetChildren() >= 2 then to >= 1
I’m still getting no output.

How much players are in the game?

@0b4u, your if statement checks if the amount of players is equal to, or higher than 2, and it’s not. And so, it’ll never print.

Try removing the Selected Player outta the Participant List

Maybe before the first line print the number of players in the game.

print(#Players:GetChildren())

Well, its in studio, just me. 30 words are in this message

Well the code requires at least 2 people to be in the game for it to work, therefore it never runs because it’s only you.

I got 0 players in the game, how would I make it wait for me to join, onCharacterAdded I guess, but that seems like I would have to wait on - there has to be a better way…

0 - Server - main:23

Then Run a test server with 4 players?

Usually for this type of thing it is implemented in a round system, where after the intermission it chooses 2 players. How are you using it in your game?

Sometimes, I wonder how I didn’t get to that conclusion myself if I had taken the time to look around.

1 Like

The Players class has a built-in event for checking whenever a player joins. This event is called PlayerAdded. ( Players.PlayerAdded (roblox.com))