I am currently writing a piece of datastore code that, before saving, checks the player’s current save and compares it to the data that is being saved… here is what I have so far…
local ShouldSave = false
local OldData = Datastore:GetAsync(Player.UserId^135) or {}
for Value, DV in pairs(DefaultValues) do
if type(DV) == "table" then -- Is the data a table?
for I, Val in pairs(OldData[Value]) do
if Val ~= Data[Player.UserId][Value][table.find(Val, OldData[Value])] then
ShouldSave = true
end
end
else
if OldData[Value] ~= Data[Player.UserId][Value] then
ShouldSave = true
end
end
end
The thing that I am having issues with is the comparing of the tables. In the PlayerAdded script, I am changing the values that are table to something that is not saved so that I can test the script… and the comparison code is simply not running through the arrays as far as I am aware.
I’ve tested the values that are not tables, and they seem to be functioning perfectly.
Does anyone have a fix for the above code?
Thanks!
for Value, DV in pairs(DefaultValues) do -- This loops through all the default datastore values... AKA what a player's datastore would look like if they joined the game for the first time.
if type(DV) == "table" then -- Is the data element a table? If it is, move on to the code for handling tables. If it isn't, directly compare the 2 values.
for I, Val in pairs(OldData[Value]) do -- Looping through the players current save of the datastore element that we are presently comparing.
if Val ~= Data[Player.UserId][Value][table.find(Val, OldData[Value])] then -- If the old value is different to what is in the player's data presently, we should save the data.
ShouldSave = true
end
end
else
if OldData[Value] ~= Data[Player.UserId][Value] then
ShouldSave = true
end
end
end
if Val ~= Data[Player.UserId][Value][table.find(Val, OldData[Value])] then
with
if Val ~= Data[Player.UserId][Value][I] then
It would also work the same way if you just swapped the arguments in the table.find call, because they are in incorrect order, but that’s unnecessary because you can just use the index variable given by pairs.
How is it supposed to add safety? Table.find will just return the index that is also the value of I, if you give it the table that you are going through and the current value. It does not add safety. And indexing a table with a key that doesn’t have a value in that table does not cause an error.
This post I just made for another topic might help you. I made the function so that it can compare two tables and check if there are any changes between them.