How to copy table from module script?

Hi,
my problem is simple, I have modul script and I need to detect if there is some change, so I compare new table with old table, but problem is that when I update it using oldTable=module.info, the var oldTable will update automaticly every time the module script table change, so the script dont detect the change, is there some way to set oldTable just to value of table in module?

1 Like

What if you compare it before you update it?

That is what i am doing, but the problem is, that it update without my activity

local tab = {}
game:GetService("RunService").Stepped:Connect(function ()
players={}
if tab ~= module.Players then
print("change")
tab = module.Players

it doesnt print change, but when i put there break, it have got the new table inside it

You will have to use a in pairs function. For example:

for _, Pls in pairs(module.Players) do
    for i, v in pairs(tab) do
        if Pls ~= v then
        print("change")
        end
    end
end
1 Like

ok, but the problem is, that the var is shortcut, so it will change its value when i change it in module, so how to make, that it will stay the same value

Create another var and set it as before updated table and later you call this table. The table is not going to change if you are not changing it in the script.

pls, look on the script i already posted, and in this script, the values of table tab are changing with changing players, its does the same as when i have int value and i put it into var and read its Value properity

I mean so you have problems with changing the tables?

i have problem with copiing tables from module, bec when i do local table2=module.table1, it save table2 as shortcut to table1

You can’t directly compare table like this, as they are treated as instances - you would have to check each individual value and/or length as the other poster has suggested. However, in your case it seems like you’re having problems with variable referencing. To fix this you need to do a shallow or deep copy of your table.

Also, IMO, I don’t think it’s the best idea to be running a loop like that. Assuming you’re using this to keep track of players you’ve put into a lobby/game etc:

Module:

local PlayerModule = { }

function PlayerModule.new()
	local this = { }
	this.Players = { }
	this.connections = { }
	this.privateEvent = Instance.new("BindableEvent")
	this.Changed = this.privateEvent.Event

	--> Push players adds a player to the list and calls the Changed event
	function this:PushPlayer(Player)
		this.Players[#this.Players + 1] = Player
		this.Changed:Fire(this.Players, "added")
	end
	--> Push players removes a player (if indexed) from the list and calls the Changed event
	function this:RemovePlayer(Player)
		local old = this.Players
		for i, v in next, old do
			if v == Player then
				table.remove(this.Players, i)
			end
		end
		this.Changed:Fire(this.Players, "removed")
	end

	return this
end

return PlayerModule

Example use:

local PlayerModule = require(game:GetService("ReplicatedStorage").PlayerModule)
local PlayerManager = PlayerModule.new()

local currentPlayerList = { }

PlayerManager.Changed:connect(function (newListOfPlayers, Action)
	--> 'Action' tells you what type of change was made to the player list
	--> 'newListOfPlayers' is the updated list of players
	print("We ", Action, " a player!")
	table.foreach(newListOfPlayers, print) --> Will print the new list of players
	
	--> Now lets update our local list of players to the new list
	currentPlayerList = newListOfPlayers
end)

--> Now we'll add some player for some reason
PlayerManager:PushPlayer(game.Players.Player1)

In your case, if you choose not to adopt the above, you can fix it by doing this (I’m assuming your table is 1 dimensional):

local oldTable = {1, 2, 3}
local newTable = { }

newTable = {unpack(oldTable)}

-- to prove this works:
table.insert(oldTable, 4)

table.foreach(newTable, print) --> Should only print 1, 2, 3
table.foreach(oldTable, print) --> Should print 1, 2, 3, 4

PLEASE NOTE: The last snippet will not work in all cases, for other cases you will need to do what’s called a “deep copy”. The above is an example of a “shallow copy”.

Then you should use table.insert() and table.remove(). Make sure to edit it for yourself because I don’t really know which table you are changing. For example:

for i, Pls in pairs(module.Players) do
    for i, v in pairs(tab) do
        if Pls[i] ~= v[i] then
          print("change")
          if v[i] == nil then
            table.remove(module.Players, i)
             else
            table.insert(module.Players, v[i])
          end            
        end
    end
end

I hope this helps. :herb: