Does setting a table to another table overnight the original table?

for _, v in pairs(Players:GetPlayers()) do
	table.insert(Settings.ALL_PLAYERS, v)
end
print(#Settings.ALL_PLAYERS) -- prints 2
Settings.ACTIVE_PLAYERS = Settings.ALL_PLAYERS

-- When a player dies, remove them from the ACTIVE_PLAYERS table, but they STAY in the ACTIVE_PLAYERS TABLE
character.Humanoid.Died:Connect(function()
	for i, v in pairs(Settings.ACTIVE_PLAYERS) do
		if v == player then
			table.remove(Settings.ACTIVE_PLAYERS, i)
		end
	end
end)

-- At end of round check for all players who played that round
print(#Settings.ALL_PLAYERS) -- only prints 1
for _, player in pairs(Settings.ALL_PLAYERS) do
    print(player) -- only prints 1, prints the active player
end

So what I am doing is starting a round and killing one player, so that 1 player gets removed from the ACTIVE_PLAYERS table, however when I do a loop at the end of ALL_PLAYERS it only shows the active player. The only other time I set data inside the ALL_PLAYERS table is at the very end with

ALL_PLAYERS = {}

which runs a couple seconds after this loop (so the loop for all players runs before the all players table is reset to empty)

So my question is, because I’m setting ACTIVE_PLAYERS to == ALL_PLAYERS does that overide ALL_PLAYERS??

Yes, it does overwrite. I think you should use table.insert

because it doesn’t overwrite anything

It does overwrite, but it overwrites active_players

I believe it does indeed overwrite, are you trying to “clone” the array?

Why does it do that tho?

If I do

local a = 1
local b = a

b = b - 1

print(a)

It should print 1 right? As I’m not setting a to b, I’m setting b to a

Basically, ACTIVE_PLAYERS should equal ALL_PLAYERS at the start. ALL_PLAYERS should remain constant throughout the game. ACTIVE_PLAYERS is the one that gets values removed, as it only contians the active players in game

Yes, it would, but here’s a quick example:

local a = 1
local b = 100
print(b)-- prints 100
b = a
print(b)-- prints 1

oh and I found something:

maybe because there is only one player in the game? Are you testing this in multiplayer?

local Array1 = {"John","Sarah","Ann"}
local Array2 = {"Nick","Carl","Dre"}

Array2 = Array1

table.remove(Array2, 2) -- shoould remove "sarah"

for k,v in pairs(Array1) do
print(k,v)
end

for k,v in pairs(Array2) do
print(k,v)
end

This removes Sarah from BOTH arrays.

Testing multiplayer, and both players are in game.

Yes i believe it does override the ALL_PLAYERS tables, (as mentioned above) in fact i believe it just sets the tables to the same table so when you were to remove a value from All_Players, ACTIVE_PLAYERSthe value will also get removed from that tables aswell

i use this sometimes for cloning tables

function NewTable(...)
	local NewTable = {}
	local Args = ...
	if typeof(Args) ~= "table" then
		Args = {...}
	end
	for _, val in pairs(Args) do
		table.insert(NewTable, val)
	end
 return NewTable
end
local Table = {5,3}
local Table2 = {2,1}

Table = NewTable(Table2)

table.insert(Table,9)
 
for i,v in pairs(Table2) do
 print(v) --- would print 2 and 1
end

Here’s my theory, you joined at different times, and unless you added a characteradded/playeradded event, there will theoretically only be 1 player in it at all times

Not joining at different times.

for _, v in pairs(Players:GetPlayers()) do
	table.insert(Settings.ALL_PLAYERS, v)
end
print(#Settings.ALL_PLAYERS) -- prints 2
Settings.ACTIVE_PLAYERS = Settings.ALL_PLAYERS

-- When a player dies, remove them from the ACTIVE_PLAYERS table, but they STAY in the ACTIVE_PLAYERS TABLE
character.Humanoid.Died:Connect(function()
	for i, v in pairs(Settings.ACTIVE_PLAYERS) do
		if v == player then
			table.remove(Settings.ACTIVE_PLAYERS, i)
		end
	end
end)

-- At end of round check for all players who played that round
print(#Settings.ALL_PLAYERS) -- only prints 1
for _, player in pairs(Settings.ALL_PLAYERS) do
    print(player) -- only prints 1, prints the active player
end

As mentioned, the ALL_PLAYERS print 2, so there are 2 players in it

Instead of like this,
Settings.ACTIVE_PLAYERS = Settings.ALL_PLAYERS
and by doing so editing both arrays (as seen in the sarah example above),

try to simply loop through the ALL_PLAYERS and table.insert() each value into ACTIVE_PLAYERS.

2 Likes