Tables Acting as If They're Linked

So recently, I encountered an odd issue which was breaking my game (Specifically my GCC game) - players were getting unusual amounts of potions, incorrect amount of candies, and more bugs were occurring.

After doing extensive research (8 hours of debugging and a case of Mountain Dew later), I found that tables now have an odd behavior that may/may not be intentional. If you set a table within a table from a variable that is set as a table, it acts as a pointer instead of a table.

Example:

[spoiler][code]template_playerdata = {
candy=0,
potions=0,
kills=0,
items={a=1,b=2,c=3}

playerdata = {}

game.Players.PlayerAdded:connnect(function(plr)
playerdata[tostring(plr.userId)] = template_playerdata
end)[/code][/spoiler]

Now, the previous behavior was that when you ran this code, each player would get a copy of the template player data that was a copy of the original. Now, the new behavior is that it acts as a pointer directly to the template player data instead of its own separate table. That means that if player 1 gets 10 candies, and player 2 joins after that event occurs, then player 2 will receive the player data of player 1.

I’m not sure if this is intentional or not, but it sure as heck is annoying (I had to write a function to make a copy of the table so it wouldn’t write to the original) - some clarification as to if this is the intended behavior or not would be greatly appreciated.

Thanks!

1 Like

I agree that this should be documented. I always use a function called ‘DeepyCopy()’ to access my data in that kind of cases.

That’s how Lua works.

Strings and numbers pass around the content of themselves, and everything else passes around pointers.

I’m pretty sure it is documented, give me a sec.

“A table is always anonymous. There is no fixed relationship between a variable which holds a table and the table itself.”

[quote] That’s how Lua works.

Strings and numbers pass around the content of themselves, and everything else passes around pointers.

I’m pretty sure it is documented, give me a sec. [/quote]

This used to work a while back, but a recent update caused this behavior.

[quote] That’s how Lua works.

Strings and numbers pass around the content of themselves, and everything else passes around pointers.

I’m pretty sure it is documented, give me a sec. [/quote]

This used to work a while back, but a recent update caused this behavior.[/quote]

That’s weird. Maybe you were doing something different?

It’s always acted this way, I don’t know why you’re seeing something different now, regardless. I think that you should just be setting the table from within your function.

Tables are memory locations. You need to set the contents of the table to a new memory location to get a new table.

A simple way to make a new copy:

function copyTable(tab)
	return {unpack(tab)}
end
1 Like

[quote] A simple way to make a new copy:

function copyTable(tab) return {unpack(tab)} end [/quote]

This only works with numerical indices.
It should be noted that any members of the table that are tables themselves will still be “linked”.

[quote] A simple way to make a new copy:

function copyTable(tab) return {unpack(tab)} end [/quote]

This only works with numerical indices.
It should be noted that any members of the table that are tables themselves will still be “linked”.[/quote]

function duplicateTable(t)
local duplicate = {}
for key,value in pairs(t) do
duplicate[key] = value
end
return duplicate
end