I am working on an inventory system for storing player data when in game, however the issue I am running into is when I try to write to a modulescript with a new value, it overwrites all of the other player’s data with that same key name, even though the data I am indexing with that key is at a lower level than the other players data.
I am confused as when I index the table containing the player data with the index of the player. then try to index with the value I want to change in their inventory, it ends up changing all players data for the second key as if the first one is ignored.
I have tried both the . and : methods for calling the functions and it results in the same behavior.
The code used in the modulescript for accessing and storing data
local Data = {}
local PlayerData = {}
function Data.AddPlayer(Player,data)
PlayerData[Player] = data
end
function Data.ReturnData(Player)
return PlayerData[Player]
end
function Data.UpdateData(Player,name,Value)
print("Player "..Player.Name)
print("name "..name)
print("Value "..Value)
print(PlayerData)-- old data from the player
if name == "" then -- update all data for a player
PlayerData[Player] = Value
else -- update a specific value for a player
PlayerData[Player][name] = Value -- issue here
end
print(PlayerData) -- new data where all other player's data is overwritten for the [name] value
end
function Data.RemovePlayer(Player)
PlayerData[Player] = nil
end
An example from a script where I try to update a value in the player’s inventory
local DataModule = require(game.ServerScriptService.DataModule)
DataModule.UpdateData(Player,"PreviousCar","RD-01)
Here is an example of the template data I am providing the player with if their data doesn’t exist in a datastore (this is accessed in a separate script, which isn’t the issue).
local DataTemplate ={
["Currency"] = 0,
["PreviousCar"] = "Chicane",
["ExitRealm"] = false,
["UnlockedCars"] = {},
["Records"]={--[[Best Time,Car,{times}]]}
}
Here is the output after updating the value, as you can see both players “PreviousCar” value were updated to the same value, but only one of the player’s data should have been updated.
the first 3 printed lines show the values that were passed to the function
Thanks, jmsbd07