Separate tables update as if they were linked

This is a very odd bug, but it forced me to spend 6 hours of debugging to finally realize what the issue was and the fact that it was on Roblox’s end.


I have created two tables:

local t = {first = {enabled = false}
local playerInfo = {}

These tables aren’t linked in the slightest but update together in the following code example:

local joinInfo = {first={enabled=false}}
local playerInfo = {}

game.Players.PlayerAdded:Connect(function(plr)
	playerInfo[plr] = joinInfo
	--
	local myInfo = playerInfo[plr]
	myInfo.first.enabled = true
	playerInfo[plr] = myInfo
	--
	print(t.first.enabled) --> prints true
end)

There is no reason that the joinInfo table should be updating when we change the table in playerInfo.

Also, all the other player’s tables inside the playerInfo will update with that one myInfo table, leading to each client getting the same information inside that script. This can lead to some pretty bad issues.

I don’t know what causes this bug, but the following screenshot may show another side-effect of the bug:


In the screenshot above, it is pretty self explanatory that a,b,c shouldn’t show up when i type in first. because it’s not even a variable for the rest of the script.

This bug happens always, every single time you test it, in-studio and in-game. You can test it yourself by posting the code above into a serverscript and watching it print “true” after you join the game.

For some reason, just removing the joinInfo variable fixes the problem, but it doesn’t explain why this is an issue:

local playerInfo = {}

game.Players.PlayerAdded:Connect(function(plr)
	playerInfo[plr] = {first={enabled=false}}
	--
	local myInfo = playerInfo[plr]
	myInfo.first.enabled = true
	playerInfo[plr] = myInfo -- the other player's tables are no longer updated along with this one, so this solution works
end)

It seems that all tables created from that joinInfo table are interlinked somehow, and any time one is updated it changed all of the rest with it.

This topic was automatically closed after 1 minute. New replies are no longer allowed.