Anyway to Improve this System to Update Data?

Hi,

So I’m trying to make a system where when the Player Joins, if Checks if the Data the Player has is Up to Date, and if not, it would either Add to it or Remove from it.
The Code is using OOP, so there is self, This is the Current Code I have at the moment:

function PlayerData:Update()
	task.spawn(function() -- so it doesnt yield, plus runs immediately
		for i,v in StarterData do -- StarterData is the Base Data for new Players
			if not self.Data[i] then -- if Key is false or nil
				self.Data[i] = v -- Adds Value to Key
			end
		end
		
		for i,v in self.Data do -- Checks Player Data
			if not StarterData[i] then -- if Data no longer Exists
				self.Data[i] = nil -- Removes Data
			end
		end
		
		return print(self.Data) -- for Debugging Purposes
	end)
end

I was wondering if there was anyway to improve this, anything wrong, or at least make this slightly work better?
If you Like, I can provide more.

Anything Helps,
Thanks.

1 Like

I strongly suggest changing not self.Data[i] to self.Data[i] == nil (same for StarterData). Maybe a value of their data is false on purpose. For example, the user’s settings for the game could include true/false values.

1 Like

The Data I have is just numbers and tables, booleans are stored within the tables so I think its fine

Wouldn’t that restrict what you’re doing with your datastores then?

maybe, but im kinda just asking how would I improve it, so ¯\_(ツ)_/¯

This is StarterData btw:

StarterData = {
	Level = 1,
	Cash = 500,
	EXP = 0,
	
	Items = {
		Weapons = {},
		Tools = {},
	},
	
	Other = {
		Banned = false,
		Reason = "N/A"
	},
}

This is just nitpicking and semantic reasoning but I think that removing properties should be habdled by another table.

StarterData implies that it contains default values. What if there is a new property that CANNOT have a default value and MUST be set individually for each player. Would it just have a dummy value to be replaced later?

Your solution works but I think it would make more sense to have a RemovedData table which contains keys that are no longer useful.

Furthermore, how often are keys removed? Is it really worth having a feature to remove unused keys? Why not add a feature that allows keys to be renamed.

This is just my opinion you are not required to listen to it.

I would say use Datastore2, but I have a feeling you know that exists already. Am I wrong?

Yes.

No.

How should I go about doing this?

Like this for example:

Say you created a property called “Exhaustion” but later on decided to rename it to “Stamina”

local NewKeyNames = {
    Exhaustion = "Stamina"
}

In the update code:

for old, new in NewKeyNames do
    if self.Data[old] then
        self.Data[new] = self.Data[old]
        self.Data[old] = nil
    end
end

1 Like

Still wondering on Suggestions,or Improvements for this system.