Every single table value being overwritten when inserting data

This is my first post on scripting support as I cannot find anything online regarding my issue. Please tell me if I have done something incorrect!

  1. What do you want to achieve? Keep it simple and clear!
    I would like DataList to not have every index be overwritten (I explain better below)

  2. What is the issue? Include screenshots / videos if possible!
    Everytime I go to insert a value (DataFromDS) into DataList, it returns DataList with every index the value that was just inserted (look at the code to see what I mean, I don’t know how to explain it well). If I assign literally anything else (like unique strings, integers, or even the DataID itself, or just a singular value of DataFromDS) to DataList[DataID] it works completely fine. I’ve even tried using the ‘i’ value instead of DataID and it still did not work.

This shows that the data is unique

Each DataID returns a different timestamp


Iterating through each value in the table after all values have been set

Each index in the table returns the exact same timestamp (this should not be the case)


Assigning a unique value directly to DataList[DataID] works

This is not the behavior I want, as I want DataList[DataID] to be the entire table of data instead of just the one value.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
  • I have verified DataIDsList has unique ids
  • I have verified DataModel:GetDataFromID(DataID) returns the correct data (as in returns different data depending on DataID provided).
  • I have tried inserting values into DataList using different methods like table.insert(), indexing values directly DataList[DataID] (like in the example below)
  • I have tried inserting different values into DataList like the DataID directly and it worked correctly. It’s just when inserting a data in the format I need to into DataList - it sets every value to the table that was just inserted.

I have modified the following code (the code in screenshots was also edited), though the logic executes pretty much the same as the code in-game.

local DataModel = require(game:GetService("ServerScriptService"):WaitForChild("Models"):WaitForChild("ThreadModel"));

local DataList = {};
local DataIDsList = {1,2,3,4,5,6};


-- Loops through DataIDsList and **is SUPPOSED TO** assign DataList[DataID] to the DataID's data

for i, DataID in pairs(DataIDsList) do

	local DataFromDS = DataModel:GetDataFromID(DataID);
	-- This line sets DataFromDS in the format:
	--[[
	
		DataFromDS = {
		
			UserID = 12345;
			SomeValueSetByPlayer = "This is a title",
			...
		
		}
	
	]]

	DataList[DataID] = DataFromDS;

end


print(DataList)

--[[

	Prints:
	
	DataList = {
	
		[1] = {
		
			UserID = 12345;
			SomeValueSetByPlayer = "This is a title 1";
			...
		
		};
		
		[2] = {
		
			UserID = 12345;
			SomeValueSetByPlayer = "This is a title 1";
			...
		
		};
		
		[3] = {
		
			UserID = 12345;
			SomeValueSetByPlayer = "This is a title 1";
			...
		
		};
	
		-- Exact duplicates of the last value that was inserted
	
	}
	
	
	Should print (something like):
	
	DataList = {
	
		[1] = {
		
			UserID = 12345;
			SomeValueSetByPlayer = "This is a title 1";
			...
		
		};
		
		[2] = {
		
			UserID = 2425234;
			SomeValueSetByPlayer = "This is a different title";
			...
		
		};
		
		[3] = {
		
			UserID = 2133624;
			SomeValueSetByPlayer = "This another unique title";
			...
		
		};
	
		-- Unique data for each
	
	}

]]

Why is the table DataList having EVERY value set to DataFromDS when only DataList[DataID] (DataList indexed at DataID) should be set to DataFromDS?

Thanks in advance, I hope it’s something really simple that I completely missed because I’m tired

Hey just want to let anyone know who’s thinking of replying know that I’m going to bed as I’ve been trying to work out this particular bug for quite a while now. If you do decide to post a potential solution, I will not reply until tomorrow.

Again, thank you for your time
~ xPuff_le

Tables have odd behaviors that I’ve seen and based on what youve provided, you’ve seemed to have noticed–as shown when you do the following, it now works

datalist[id] = datads.TimeStamp

am i correct to guess that the un-unique timestamps we get in the 2nd floor loop is == to the final dataid index’s?

if you assign a table with a persistent table this type of stuff occurs and ik my explanation has no substance but you essentially have to do it in the way you’ve inadvertently solved it. instead of writing all the .indexes, you can use for loop and recursion

DataList[DataID] = {}

--for loop, recurse for nested loops

i, v

parenttable[i]=v
1 Like

Well, that’s some ugly syntax but it does work. Thank you. Here’s a somewhat better explanation for anyone in the future since I know reading Data 50 times in a row is confusing

Here’s the final code sample I made:

local DataList = {};

for i, DataIDInfo in pairs(DataIDsList) do

	local DataID = DataIDInfo.value;
	local DataFromDS = ThreadModel:GetDataFromDS(DataID);

	DataList[DataID] = {};

	for DataName, DataValue in pairs (DataFromDS) do
		DataList[DataID][DataName] = DataValue;
	end

	warn("------------")
	warn("DataID#: "..tostring(DataID))
	warn("TimeStamp: "..tostring(DataFromDS.TimeStamp))

end

for DataID, Data in pairs(DataList) do
	print("DataID#: "..tostring(DataID))
	print("Timestamp: "..tostring(Data.TimeStamp));
end

What I was doing before:

  • Setting DataList[DataID] = DataFromDS directly

Did not work :frowning:

What I ended up having to do:

  • Initialize DataList[DataID] = {} – An empty table
  • Loop through every value in DataFromDS and inserting the value into the newly initialized table

Works :slight_smile:

Basically instead of getting DataFromDS and assigning it directly to DataList at the index DataID, I have to set DataList[DataID] to an empty table, then “copy and paste” every value of DataFromDS to the empty table. (I feel like I’m just repeating myself at this point sorry)

Proof that it works now:




Edit: Yes, I know some of the TimeStamp values are duplicated, this is my fault as when I was creating dummy data for the tests I messed up a little lol. I did more tests with new dummy data and it worked flawlessly, so the above solution is working!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.