I am making a data store using oop. To specify i created a class called “playerClass” here is the code.
function playerClass.new(player, self)
if self then return setmetatable(self, playerClass) end
local self = setmetatable({}, playerClass)
self.Money = 0
self.Rank = 0
self.Exp = 0
self.Tools = {}
self.Abilities = {}
self.Perks = {}
self.Skins = {}
--self.Apples = 0
return self
end
So lets say I make an update to the game and i want to add new currency or variable for the class lets say for this example apples an integer variable. If I add it the old playerclass is saved and the apple variable isnt added. Is this because of the first line?
For adding new data values to a DataStore table, I suggest having a default table, then looping over it to see if the player’s data has the key. If not, then set the key to the default value. (Make sure you use table.clone
if the default value is a table, otherwise your default table will be changed if you modify the table in the player’s data!)
Here’s an example:
local defaultData = {
Money = 0,
Rank = 0,
Exp = 0,
Tools = {},
Abilities = {},
Perks = {},
Skins = {},
Apples = 0
}
function playerClass.new(player,self)
self = setmetatable(self or {}, playerClass)
for i,v in pairs(defaultData) do
if self[i] == nil then
self[i] = (type(v) == "table" and table.clone(v)) or v
end
end
return self
end
Hope this helps!
1 Like
I don’t know about normal Roblox Datastores but I do know that ProfileService by loleris can have new values added and still keep the old ones.
Oh I see that makes sense I couldnt think myself such a simple solution. Thank you very much!