How to see the difference between 2 tables

Hello there,

I was creating my datasaving system, but i though, if i wanted to add a new value to the data, it wouldend save, becouse it doesnt exsist.

let me show you

this is the player data of now: (what you start with)

local PlayerDataTableExample = {
	["Data"] = {
		SYA = 0,
		EAEA = 0,
		AFA = 0,
		KAJH = 0,
		LKIS = 0,
	},
	["PlayerInfo"] = {
		TimePlayed = 0,
		TimesJoined = 0,
		Kills = 0,
		Deaths = 0,
	}
}

Than i add in Data : test = 10000,
But i only add this table to the playerdata, when someone joines first time.
So if i had joined, than left, than added this value, than rejoined, the player wouldend have “test = 10000,”.

so have a PlayerData table (that the current data is) and i have a exampletable.
How could i see if the PlayerData doesnt have a value, thats in exampletable.

(i am going to add a lot of values later in the game)

i though about it and i could always check if the player doesn’t have the value and than add it, but i want to learn, and i want that the script will always add new data if i ever add some.

I mean, would something like this work?:

if not LoadedData["Data"]["test"] then

yes but at the end i said:
i though about it and i could always check if the player doesn’t have the value and than add it, but i want to learn, and i want that the script will always add new data if i ever add some. (basicly i am lazy and i want to learn new ways of working with tables)

You would do something like this where you loop through each Table in the PlayerDataTableExample and if a value does not exist in the data you are checking, then you just set it there. You could make it recursive, but this will work as long as you dont add any more tables.

local PlayerDataTableExample = {
	["Data"] = {
		SYA = 0,
		EAEA = 0,
		AFA = 0,
		KAJH = 0,
		LKIS = 0,
		Test = 0
	},
	["PlayerInfo"] = {
		TimePlayed = 0,
		TimesJoined = 0,
		Kills = 0,
		Deaths = 0,
	}
}

local DataToCompareAgainst = {
	["Data"] = {
		SYA = 0,
		EAEA = 0,
		AFA = 0,
		KAJH = 0,
		LKIS = 0,
	},
	["PlayerInfo"] = {
		TimePlayed = 0,
		TimesJoined = 0,
		Kills = 0,
		Deaths = 0,
	}
}

for Folder, Contents in pairs(PlayerDataTableExample) do
	if not DataToCompareAgainst[Folder] then
		DataToCompareAgainst[Folder] = Contents
	end
	
	for Item, Value in pairs(PlayerDataTableExample[Folder]) do
		if not DataToCompareAgainst[Folder][Item] then
			DataToCompareAgainst[Folder][Item] = Value
		end
	end
end
1 Like

Thanks, this is what i was searching for!

1 Like