This is a datastore2 where there is a table as the storage object. I use table.find to check if the table already has values inside it. If the table is not empty, the player already has data, and so this loop overwrites data which is bad.
if not table.find(cpDatastore:Get(),Instance) then
print("new to the game")
for index,checkpoint in pairs(checkpointsFolder) do
table.insert(defaultTable,defaultSplit)
end
cpDatastore:Set(defaultTable)
end
Below is a print of the function. I suppose nil is not equal to true, but why is it nil then? Am I using table.find wrong?
Instead of using table.find(), if the table is an array, you could use #, to get length and check if there’s is anything there, if it is a dictionary, you could create a loop to get the length and do the same thing.
Also, it’s returning nil because it couldn’t find anything about the Instance, which I assume isn’t a actual instance, but a value or a string.
yes the table contains NumberValues. Is each value in a table not considered an instance though? I think of it like table.insert returns true if there is an instance in the table and false if there’s no instance in it.
Here is the fixed version based on your #length solution. Not related to my question, but it’s good to get a solution into code for anyone reading.
if #cpDatastore:Get() == 0 then
print("new to the game")
for index,checkpoint in pairs(checkpointsFolder) do
table.insert(defaultTable,defaultSplit)
end
cpDatastore:Set(defaultTable)
end
Even better, if it is a proper array (no nil values stored anywhere), you can simply just check for the existence of the first element in the array. This is usually better because getting the length of an array when it’s not cached requires it to do the counting which can be expensive