Can you compare entire dictionaries just by its name?

So I’m trying to make a datastore for a global donation leaderboard and I’m scripting it so that periodically it saves the dictionary that contains all of the leaderboard holders as long as it doesn’t look like the default dictionary or an ‘empty’ one.
Here’s what my dictionary looks like:

local defaultGDT = {
["slot1"] = {
	UserId = "";
	AmountDonated = "";
};
["slot2"] = {
	UserId = "";
	AmountDonated = "";
};
["slot3"] = {
	UserId = "";
	AmountDonated = "";
};
["slot4"] = {
	UserId = "";
	AmountDonated = "";
};
["slot5"] = {
	UserId = "";
	AmountDonated = "";
};
["slot6"] = {
	UserId = "";
	AmountDonated = "";
};
["slot7"] = {
	UserId = "";
	AmountDonated = "";
};
["slot8"] = {
	UserId = "";
	AmountDonated = "";
};
}

I currently also have a identical dictionary called activeGDT which is what will actively store the leaderboards placeholder’s data. I want to save the activeGDT to a datastore as long as it doesn’t look identical to defaultGDT. I’m planning to do this with a statement like this:

if activeGDT ~= defaultGDT then
	-- dataStore save stuff
end

Is something like this possible? Logically it would seem possible but I wouldn’t be surprised if something like this didn’t work. Does comparing entire dictionaries like this work or do I need to compare every key individually?

You could loop through activeGDT and look for a nil value for UserId or AmountDonated. If there is a something that is not a nil value, it’s not the same as the default table. I would recommend replacing the

UserId = ""

with nil, looks better in my opinion.

Or just do defaultGDT[player.UserId] instead of manually making a full list of new slots

I wrote this up for myself a bit ago in my core library.
You want the function isSameTable() but you must have all of this to make it work.

When the function is finished you’ll get a return of true or false

function module.tableCopy(myTable :table, keepMeta :bool)
	local tempTable = {}
	for k,v in pairs(myTable) do
		tempTable[k] = v
	end
	if keepMeta then
		setmetatable(tempTable, getmetatable(myTable))
	end
	return tempTable
end

function module.tableCount(myTable)
	local i = 0
	for k,v in pairs(myTable) do
		i+=1
	end
	return i
end

function module.isSameTable(tab1,tab2)
	if type(tab1) ~= "table" or type(tab2)~="table" then return false end
	if tab1==tab2 then return true end
	local temp1, temp2 = module.tableCopy(tab1), module.tableCopy(tab2)
	for k,v in pairs(temp1) do
		if temp2[k] == v or module.isSameTable(v,temp2[k]) then
			temp2[k] = nil
			temp1[k] = nil
		end
	end
	for k,v in pairs(temp2) do
		if temp1[k] == v or module.isSameTable(v,temp1[k]) then
			temp1[k] = nil
			temp2[k] = nil
		end
	end
	return module.tableCount(temp1)==0 and module.tableCount(temp2)==0
end
1 Like