I’ve got a bit of an issue with my player stat reporting. All seems good on the surface and the code works, but often it will return and save data which isn’t even mathematically possible. I have no idea what is causing this, the code looks totally fine (InitPlayer).
I reviewed the code and I do not see anything remotely wrong with it.
The code is not on GitHub but you can see it in the package here:
My only other idea is OnBuild is getting called more than once but that should not be possible, as least not from the same game server.
Thank you to anybody who is able to look at the code.
I may have found the issue. When a completely new player joins the game and doesn’t have any existing save data, it passes the DefaultData table. This table is never cloned/copied therefore every new player that joins will modify the same table of data.
local function InitPlayer(Player: Player, API)
repeat task.wait(.1) until Player.UserId
local PlayerInfo = PlayerStore:GetAsync(Player.UserId) or DefaultData -- Here's the issue
When it should be something like this:
-- The function used for deep copying a table
local function deepCopy(original)
-- Define the new table for the copy
local copy = {}
-- Loop through the original table to clone
for key, value in original do
-- If the type of the value is a table, deep copy it to the key (index)
-- Else (or) the type isn't a table, assign the default value to the index instead
copy[key] = type(value) == "table" and deepCopy(value) or value
end
-- Return the finalized copy of the deep cloned table
return copy
end
local function InitPlayer(Player: Player, API)
repeat task.wait(.1) until Player.UserId
local PlayerInfo = PlayerStore:GetAsync(Player.UserId) or deepCopy(DefaultData) -- No more shared table reference