I don't know how to fill missing names in my table

Hello. I’m trying to fill in missing template names if I ever were to update my data template to prevent existing data from not loading. But now I’m stuck and can’t find it out.

local dataTemplate = {
	["Boosts"] = {
		["Red"] = 0;
		["Blue"] = 0;
		["Green"] = 0;
		["Pink"] = 0;
		["Purple"] = 0;
		["Golden"] = 0;
	};

	["CharacterPositions"] = {
		["X"] = 0;
		["Y"] = 0;
		["Z"] = 0;
	};

	["Currency"] = {
		["Candy"] = 0;
		["Redeemed"] = 0;
		["Time"] = 0;
	};

	["GamePasses"] = {
		["X2Time"] = false;
		["X5Time"] = false;
		["X10Time"] = false;
		["Vip"] = false;
	};

	["Misc"] = {
		["Halloween2024"] = false;
		["TransferredData"] = false;
		["ReceivedVipPerks"] = false;
	};

	["CodesRedeemed"] = {};

	["UGCsPurchased"] = {};
}
local playerData: any

	local getSuccess, errorMessage = pcall(function()
		playerData = dataStore:GetAsync("Player_"..player.UserId)
	end)

	if not playerData then
		playerData = dataTemplate
	end

	for dataType, _ in dataTemplate do
		if not playerData[dataType] then
			playerData[dataType] = dataTemplate[dataType]
			warn("Added type: "..dataType)
		end
		
		for dataName, _ in dataTemplate[dataType] do
			-- I want to add every missing data name to every data type aswell, but I don't know how to do this.
		end
	end

Are you going to use the standard DataStore? Why not use Profile Service? It’s easier for you to load or save data. It has template/table that you can store the way you want.

I’m trying to do it my own way, not having to require the ProfileService module a lot of times. I only need to load player’s data once and set their attributes.

       if playerData[dataType][dataName] == nil then
            playerData[dataType][dataName] = dataTemplate[dataType][dataName]
        end

like this?

I’ve tried this and it results in all data that have set booleans (instead of numbers) in the template to get added. I have only removed my ‘‘Time’’ from my data for testing, so it should only add time.
image

for dataName, v in dataTemplate[dataType] do
	if table.find(playerData, dataName) then continue end
	playerData[dataName] = v
end

This might work

This makes it add everything
image

for dataName, v in dataTemplate[dataType] do
	if playerData[dataName] then continue end
	playerData[dataName] = v
end

Oh i think i made a mistake before srry

1 Like

It’s still showing the same result
image

This is the code I used before testing (as you can see only time is missing):

From what I understand, you’re adding a new item to the table, so you want the new item to be saved?

If player’s data have been saved prior, and I decide to add, let’s say a new currency called gems. I want the script to set the player’s data ‘‘gems’’ to 0 by copying the template.

I think this is what you’re looking for

local playerData: any

	local getSuccess, errorMessage = pcall(function()
		playerData = dataStore:GetAsync("Player_"..player.UserId)
	end)

	if not playerData then
		playerData = {}
	end

	for dataType, data in pairs(dataTemplate) do
		if not playerData[dataType] then
			playerData[dataType] = data
			warn("Added type: "..dataType)
		end

               for key, value in pairs(data) do
                       if not playerData[dataType][key] then
                               playerData[dataType][key] = value
                       end
               end
	end

Wouldn’t this only add the types like ‘‘Boosts’’ and ‘‘Currency’’? I don’t think the script will notice if I were to update the game and add gems inside of the template’s ‘‘Currency’’ table, because currency already exists for the player.

		if playerData[dataType][dataName] == nil then
			print(dataName)
		end

whats the output?

1 Like
for dataName, v in dataTemplate[dataType] do
	if playerData[dataType][dataName] then continue end
	playerData[dataType][dataName] = v
end

Does this work?

image

thats what u looking for right?

Then just do another loop inside of that loop , edited my message and did it for you though.

Yeah, it works now. Thanks!

image
image

bruh

hi heyo wow what is this what is this

1 Like