Every player being given the same data table

I literally mean move that piece of code onto the place of “or data

But the piece of code is

return {
	CurrentBackpack = 'Starter',
	CurrentBread = 'Brown',
	CurrentDucks = {
		{Tier = 1, Rarity = 'Common'},
	},
		
	Cash = 0,
	Gems = 0,
	Happiness = 0,
	MaxHappiness = 25,
	Rebirths = 0,
	
	Inventory = {
		Backpacks = {
			'Starter'
		},
		Breads = {
			'Brown'
		},
		Ducks = {
			{Tier = 1, Rarity = 'Common'},
		},
	},
	
	Codes = {
		
	},
	
	Levels = {
		['Frantic Farm'] = false,
		['Walters Waterpark'] = false
	},
}

Would take up too much space in one line

local loadData = playerDataStore:GetAsync(player.UserId) or {
	CurrentBackpack = 'Starter',
	CurrentBread = 'Brown',
	CurrentDucks = {
		{Tier = 1, Rarity = 'Common'},
	},
		
	Cash = 0,
	Gems = 0,
	Happiness = 0,
	MaxHappiness = 25,
	Rebirths = 0,
	
	Inventory = {
		Backpacks = {
			'Starter'
		},
		Breads = {
			'Brown'
		},
		Ducks = {
			{Tier = 1, Rarity = 'Common'},
		},
	},
	
	Codes = {
		
	},
	
	Levels = {
		['Frantic Farm'] = false,
		['Walters Waterpark'] = false
	},
}
1 Like

Wow :confused: That actually worked! Can you explain why using a variable name does not work?? Cause I’ve had it that way for several months and never ran into this problem ever before

You’re saving a reference to the table and not creating a new table, so it seems like you’re editing the data of the player but what you’re actually doing is editing what your variable is referring to, and that is the data table at the top of your example script.

4 Likes

Take a look at this thread it is the exact same problem you are having with the solution.

You have to copy the table, it would look something like this:

function CloneTable(t)
  local nt = {}
  for i,v in pairs(t) do
    if type(v) ~= 'table' then
      nt[i]=v
    else
      nt[i] = CloneTable(v)
    end
  end
  return nt
end
2 Likes

You need to return nt

@NinjoOnline this is the explanation and it’s been said several times now. Lua is not deep copying the table for you, it’s just referencing it.

1 Like

How would I intergrate that into my code then?

My bad, typing inside forum can be weird.
Fixed it so it returns nt, thanks for letting me know.

You just copy a table whenever you assign it to new user.

newData = CloneTable(defaultData)
newData = CloneTable(defaultData)
local loadData = PlayerDataStore:GetAsync(Player.UserId) or newData

PlayersData[Player.UserId] = updateData(loadData, Data)

like so?

local loadData = PlayerDataStore:GetAsync(Player.UserId) or CloneTable(defaultData)

PlayersData[Player.UserId] = updateData(loadData, loadData)

Should handle scenarios where GetAsync returns error, but yeah, this is about how you do it.

1 Like

An alternative to the above would be to format your “template” module like so:

--Template.lua
return function()
 local template = { ... }
 return template;
end
--DataManager.lua
local template = require(path_to);
::GetAsync() or Template()

You wouldn’t have to deep copy anything in this case.

[EDIT: meant to reply to op]

The reason why your “VariableName” didn’t work was because that is a shallow copy of the table.

Therefore, some or all of the items inside the table are not copies, but references to the original data point.

To solve this, as @Legoracer has done, you would create a function to recursively iterate through the table and copy each branch into a new one, creating what is called a deep copy.

This error has plagued me before.

1 Like

Did something change with relation to this in the past 2-3 months? As Ive never had this issue before

Nope, it’s probably just because it’s your first time having tables within tables and referencing them.

I’ve noticed you seem to never mark anything as a solution even though your problems have been solved.

Please start doing it as it is common courtesy to do so.

If a New Member has a lot of solutions, it shows that they’re active and being helpful which in turn increases their chance of being promoted to Member.

5 Likes

I do mark something as a solution if it 100% works on my end. I just woke up and haven’t been able to test the new answers yet. When I know that my question has been 100% fully answered and works for me then I will mark it as a solution