How to make game select 2-4 players instead of set number

is there anyway to make it so the game selects between 2-4 players depending on the current player count?
this is the code rn

local PickedPlayers = {playerService:GetPlayers()[math.random(4, #playerService:GetPlayers())]}
			local Player1 = PickedPlayers[1]
			local Player2 = PickedPlayers[2]
			print(Player1.. " and".. Player2.. " have joined!")

someone please help!!! (im sorry for using the dev fourm so often)

the problem is that you’re not really selecting multiple unique players, you’re just picking one random player index between 4 and the player count, which doesn’t make sense when the goal is to pick 2–4 random players depending on how many are in the game.

to make the game randomly pick between 2/4 players you should do something like this:

local playerService = game:GetService("Players")
local allPlayers = playerService:GetPlayers()

-- you 100% need more than 2 players
if #allPlayers < 2 then
	warn("need more players!!")
	return
end

-- pick how many 2/4
local amountToPick = math.clamp(math.random(2, 4), 2, #allPlayers)

-- shuffle logic
local shuffled = {}
for _, player in ipairs(allPlayers) do
	table.insert(shuffled, player)
end
table.sort(shuffled, function() return math.random() < 0.5 end)

-- Pick the first
local pickedPlayers = {}
for i = 1, amountToPick do
	table.insert(pickedPlayers, shuffled[i])
end

--test output
local names = {}
for _, p in ipairs(pickedPlayers) do
	table.insert(names, p.Name)
end
print(table.concat(names, ", ") .. "joined")

oh my this is giving me a headache, i have no idea how to integrate this with my current code

What do you mean by “instead of a set number”

Are you trying to select 4 random players?

local Players = game:GetService("Players")

local Plrs = Players:GetPlayers()
local Needle:number = math.random(1,#Plrs)
local Plr1 = table.remove(Plrs,Needle)::Player
Needle = math.random(1,#Plrs)
local Plr2 = table.remove(Plrs,Needle)::Player?
Needle = math.random(1,#Plrs)
local Plr3 = table.remove(Plrs,Needle)::Player?
Needle = math.random(1,#Plrs)
local Plr4 = table.remove(Plrs,Needle)::Player?
local PickedPlayers = {playerService:GetPlayers()[math.random(4, #playerService:GetPlayers())]}
local Player1 = PickedPlayers[1]
local Player2 = PickedPlayers[2]
print(Player1.. " and".. Player2.. " have joined!")

It’s good to talk about the problems with the code snippet you provided.

  • Always use the Random object in favor of math.random
  • The call to math.random is going to pick a random Player from the 4th index to the nth index (where n is the number of Players). This line of code is problematic for multiple reasons
  • The above is being stored into a table, so the PickedPlayers table will have exactly one entry
  • As a consequence of the above statements, the assignments to Player2 will always fail as PickedPlayers will always have one item and Player1 will always be the sole item contained in PickedPlayers

To fix the code, make the following changes (untested code, here be dragons):

local Rnd = Random.new() -- using the Random object
local Players = game:GetService("Players") -- store the Players service as a variable

-- Note that the GetPlayers() method always returns a table of players
-- We will store the selected players in a separate table
local CurrentPlayers = Players:GetPlayers()
local SelectedPlayers = {}

-- Now, decide whether to pick anywhere from 2 to 4 players
-- We will base the number of players to pick based on the current Player count
-- We can use the floor division operator, which divides a number and throws away the remainder; for example, 9 players // 2 = 4 chosen players
-- Then, make sure we always choose at least 2 but never more than 4 players using math.clamp
for i = 1, math.clamp(#CurrentPlayers // 2, 2, 4) do
    local SelectedIndex = Rnd:NextInteger(1, #CurrentPlayers) -- Randomly select an index from 1 to #CurrentPlayers
    local SelectedPlayer = CurrentPlayers[SelectedIndex] -- Use SelectedIndex to index the corresponding player
    table.remove(CurrentPlayers, SelectedIndex) -- Remove the chosen player from CurrentPlayers
end