This function currently selects any random amount of players from a table I made that gets all players in the game, the issue is that it removes the player from my table playersingame.
How can I modify this function so that it doesn’t remove anything from my table, and just returns the players randomly selected?
Any help is appreciated, thank you!
function ChooseRandomPlayers(count)
local result = {}
local players = playersingame
if count > #players then
return players
end
local rand = Random.new()
for i = 1, count do
table.insert(result, table.remove(players, rand:NextInteger(1, #players)))
end
return result
end
Remember to clone the table using a function like below. Currently it’s the same table due to how tables are passed by reference.
Sorry, I’m not quite sure how I’m supposed to go about this.
When I print out the playersingame table, I only see 1 value in it since the other one got taken out from it, but I need both values to stay and just a random result returned. Am I supposed to clone the original table? Or…
I tried using the unpack method for cloning but resulted in this error:

function ChooseRandomPlayers(count)
local result = {}
local clone = (unpack(playersingame))
if count > #clone then
return clone
end
local rand = Random.new()
for i = 1, count do
table.insert(result, table.remove(clone, rand:NextInteger(1, #clone)))
end
return result
end
Okay I tried the shallowcopy function and it worked thank you so much for this, I never knew about cloning tables so it’s something new I learned 