Module datastore help

i did a module datastore script that worked perfectly, i tried making it soo i could add new values without breaking the datastore completely, because my game will always be updated with new content everyday, and with my old datastore scripts i had to wipe data every single time i updated, and now it ins’t saving, soo what i need is help to fix the scripts and make soo i can add new values whenever i want without having to data wipe, 100% of the roblox games dont need to data wipe when adding new content to the datastore, but when i search how to do that, i either cant find anything or people tell me to use datastore 2 (which is super messy), also someone said using datastore 2 doesn’t changes anything soo yeah

problems: broken save script, need to wipe data everytime i add new values

module script:

local dsinfo = {
	folder_name = "DATA";
	ds_name = "WDYF_TEST0333";
	key = "WDYF-";
	
	data = {
		coins = {"IntValue", 0};
	};
};

return dsinfo

server script (inside the module, however i dont think thats the problem)

local PS = game:GetService("Players")
local RUN = game:GetService("RunService")
local DSS = game:GetService("DataStoreService")
local DSM = require(script.Parent)
local DS = DSS:GetDataStore(DSM.ds_name)

function save(p)
	local key = DSM.key .. p.userId
	local tosave = {}
	
	local folder = p:FindFirstChild(DSM.folder_name)
	if not folder then return end
	
	for _, stat in pairs(folder:GetChildren()) do
		tosave[stat.Name] = stat.Value
	end
	
	local s, e = pcall(function()
		DS:SetAsync(key, tosave)
	end)
	
	if not s then
		warn(p.Name .. " Data Error: " .. e)
	end
end


PS.PlayerRemoving:Connect(save)
PS.PlayerAdded:Connect(function(p)
	local key = DSM.key .. p.userId
	local currentsave = DS:GetAsync(key)
	
	local folder = Instance.new("Folder",p)
	folder.Name = DSM.folder_name
	
	for name, stat in pairs(DSM.data) do
		local newval = Instance.new(stat[1])
		newval.Name = name
		newval.Value = stat[2]
		newval.Parent = folder
	end
	
	if currentsave then -- LOAD
		for l, stat in pairs(folder:GetChildren()) do
			if currentsave[stat.Name] then
				stat.Value = currentsave[stat.Name]
			else
				local missing = DSM.data[stat.Name]
				stat.Value = missing
				currentsave[stat.Name] = missing
			end
		end
		
		save(p)
		
		for l,s in pairs(currentsave) do
			print(l, s)
		end
	else
		save(p)
	end
end)

-- # studio bugfix # -- (not the problem btw)
game:BindToClose(function()
	wait(5)
end)

You should iterate through the module’s dsinfo table to check if everything is there.

local currentsave = DS:GetAsync(key) or {}
for i,v in pairs(module.data) do
    if currentSave[i] == nil then -- if the current index isn't inside of currentSave (we have to explicitly check if it isn't nil as not false is true, not nil is also true) then create it.
        currentSave[i] = v
    end
end
-- then iterate through the data and create the new objects as needed
for i,v in pairs(currentSave) do
    local currentObject = Instance.new(i[1])
    currentObject.Parent = folder
    currentObject.Name = i
    currentObject.Value = v[2]
end

where should i put that line of code

It was just some quick pseudocode that has to be adjusted to your circumstance but probably inside of your load func.

ok thx imma test it here if it works imma mark it as solution

1 Like

its working thx, im soo happy that i would spam caps rn but the forum wont let me xD