Update async aint updating my datastore

leaderboards:UpdateAsync("leaderboard", function(totalList)
					local ties = totalList[pdata.InfiniteWave]

					if not ties then
						ties = {player.UserId}
					elseif not table.find(ties, player.UserId) then
						table.insert(ties, player.UserId)
					end

					for i, v in pairs(totalList) do
						if i ~= pdata.InfiniteWave and table.find(v, player.UserId) then
							table.remove(v, table.find(v, player.UserId))
						end			
					end


					totalList[pdata.InfiniteWave] = ties

					return totalList
				end)

nothing is changed after this runs

am i just stupid why
please help

pdata.InfiniteWave is a number
i use that as an key to a dictionary
the dictionary has tables of users who hold the same wave

example

{
 [1] = {520525; 12040242;}
 [26] = {259259; 251034023}
}

it saves sometimes but not all the time
very weird

Hello!

It’s hard to figure out what your trying to do with your code without any context, so I can’t exactly give you best answer, but I’ll try my best. So here are some things I found that may be causing the problem:

First, in your for loop, you are changing the v variable, but I’m pretty sure the way tables work with variables, v is only a reference to your main table (totalList), so changing it doesn’t change your main table. To fix this, you would have to change the data directly, not through the v variable. Sort of like how changing ties doesn’t actually change totalList.
Here’s a modified version of the for loop that should fix this problem, if it is the problem:

for i, v in pairs(totalList) do
	if i ~= pdata.InfiniteWave and table.find(v, player.UserId) then
		-- Changes totalList directly, instead of through v
		table.remove(totalList[i], table.find(v, player.UserId))
	end			
end

I’m not 100% sure if this is the problem, but it might be.

Second, all of your pieces of code that change your main table are under if statements, so if none of the if statements come out to true, then your table will not change. To check if this is the problem, I would print the table at the start and the end of the function, and if both print statements print the same thing, then this may be the problem. (It could also be caused by the last problem, since the last problem I went over may have stopped you from changing the main table, so try that one first)

That’s all I can really think of that could be causing this, but also I’ve never used UpdateAsync, so it could be something simple that I wouldn’t know. I hope this helped solve your problem!

Just to make sure, are the values of totalList arrays or dictionaries? table.find and table.remove only work on array-like tables.

The opposite is true; Because v is a reference to the main table, changing v does affect it. You don’t need to index it again later.

If v were somehow a clone of the table instead (i.e. “pass by value”), that’s when changing one would not affect the other. But when it’s a reference, it acts like the original object.

1 Like

table.remove shifts elements, so its likely breaking your iteration

That was my first intuition too, but upon closer inspection, you’ll see the table being iterated over (totalList) is different from the table from which the value is removed (v, sub-tables inside totalList).

reference is still the same, try cloning totalList and using the clone, so reference is different

What type of datastore is it? Ordered or normal one?

normal datastore
i’ll add more context to post