Help, how can I add more data like "Event Money" to a user's data when the user's data is already created?

How can I add more data like “Event Money” once the data table is created? The problem is that the data is created when the player connects for the first time, so I can’t add more data like “Event Money” because the player already has “PlayerData”.

What I want to achieve with this is to have a method to be able to add data to a player who already has saved data.

I also got “DataStore Editor”, but it turns out that this plugin does not have the function of modifying, adding or removing data from ALL keys (players) at once.

if success then
		print("Connection success for", player.Name)
		if not playerData then
			print("New player, giving default data")
			playerData = {
				["Money"] = 0,
				["Items"] = 0,
				["ItemVariants"] = {"Classic"},
				["Stars"] = 0,
			}
		end
		data[player.UserId] = playerData
		
		--and next I probably need a method to add new data (example: Event Money) to a player without losing what he has already created and saved.

Sorry if I don’t know how to explain it very well, I started writing scripts a little over a month ago and I don’t understand this very well, I would appreciate the help!

1 Like

You can just check players when they connect to see if they have all the entries they need. If they are missing one, it should be added with a default value.

1 Like

How do i check if they have all the entries?

If an entry is nil it is not there. You can keep a “template” player data table that you compare against, which can also hold the default values. A brand new player just an exact copy of this template, whereas a player returning only gets the entries which they don’t have yet.

1 Like

Ok, I’ll see what I can do, although i don’t understand it very well, thanks!

Hello, I don’t understand how to do what you mention, nor how to make the “template”, nor how to make the game detect when the player is missing a “statistic” and only give him the missing statistic, Do you have a line of code you can show me for reference please?

You can simply add a new key-value pair to the playerData table. Here’s how you can do it:

if success then
		print("Connection success for", player.Name)
		if not playerData then
			print("New player, giving default data")
			playerData = {
				["Money"] = 0,
				["Items"] = 0,
				["ItemVariants"] = {"Classic"},
				["Stars"] = 0,
			}
		else
			-- Check if EventMoney already exists, if not, add it.
			if not playerData["EventMoney"] then
				playerData["EventMoney"] = 0
			end
		end
		data[player.UserId] = playerData

In the above code, I added a check to see if “EventMoney” already exists in the playerData. If it doesn’t, then I add it and set its initial value to 0.

This way, if a player has already connected before and has a playerData, we won’t overwrite their existing data but just add the new “EventMoney” data. If the player is new, we create the playerData as before.

1 Like

Thanks bro, this worked for me, i don’t know why I hadn’t thought of using “Else” lol, one thing, does that mean I’ll have to use the “if not playerData[”(dataName)"] method for every new future data I’m going to add? , that is, something like this:

if success then
		print("Connection success for", player.Name)
		if not playerData then
			--BetaData
			print("New player, giving default data")
			playerData = {
				["Money"] = 0,
				["Items"] = 0,
				["ItemVariants"] = {"Classic"},
				["Stars"] = 0,
			}
			--BetaData
		else
			--PostBetaData
			if not playerData["EventMoney"] then
				print("New data [EventMoney] detected for:" .. player.Name .. ", adding the new data")
				playerData["EventMoney"] = 0
			end
			
			if not playerData["Wins"] then
				print("New data [Wins] detected for:" .. player.Name .. ", adding the new data")
				playerData["Wins"] = 0
			end
			
			if not playerData["SpecialItems"] then
				print("New data [SpecialItems] detected for:" .. player.Name .. ", adding the new data")
				playerData["SpecialItems"] = 0
				playerData["SpecialItemsVariants"] = {"Classic"}
			end
			--PostBetaData
		end
		data[player.UserId] = playerData

Anyway this works for me, thanks, i’ve been looking for it for weeks.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.