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.
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
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.
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.
]]
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
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
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.
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…
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?