Is this an Table Bug?, What am I doing wrong? [Solved]

I’ve been fiddling with my scripts thinking it had bugs that I wasn’t aware of, then after hours of doing everything the issue wasn’t my script logic, I found this odd behavior that overwrites an Table Key, even tho the keys doesn’t match. I wanna post at bug report but I don’t have the perms.

Main Script

local Module = require(game.ServerStorage:WaitForChild("ModuleScript"))

game.Players.PlayerAdded:Connect(function(Player)
	Module:Start(Player)
end)

task.wait(15)

Module:Get()

ModuleScript

local module = {
	Status = {}
}

local DefaultValues = {
	Level = 300,
	Coins = 500
}

function module:Start(Player)
	if not self.Status[Player] then
		self.Status[Player] = DefaultValues
		self.Status[Player]["Name"] = Player.Name
		print(Player, self.Status[Player])
	end
end

function module:Get()
	print(self)
	return self
end

return module

Place if you want to test it out for yourself:
Table-Dictionary BUG REPORT.rbxl (32.8 KB)
PS: Play 2 or more players in Studio.

Could you explain what do you believe is not working properly?

It doesn’t seem to be a bug, but rather a logic error. When you do self.Status[Player] = DefaultValues you are making all players point to the same table. To avoid this you should create a copy of DefaultValues.

self.Status[Player] = copy(DefaultValues)
2 Likes

That melted my brain, shouldn’t it technically be like

self.Status[Player] = {
	Level = 300,
	Coins = 500
}

but yea this enlightened me, it just clicked to my brain just now. Thanks!

The { } operator is considered a constructor and always returns the reference to a new table. What is stored in the variables are references to tables that have already been created, so they will always return the same reference.

Yea I just realized I’m pointing out an local variable table to be the values of players which will overwrite each other as they’re using the same only variable, It wasn’t clicking to me due to the way I constructed my Datastore using modulescripts to get the default values.