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
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.
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.