Help needed with setting datastores

I have settings as multiple values divided in different sections with folders. However, while attempting to save the values, Ive come across an issue. I can only save one value at a time. (mind you, im new to data store.)


local plr = game.Players.LocalPlayer

local dataStore = game:GetService("DataStoreService")
local mainStore = dataStore:GetDataStore("Main")

local success,data = pcall(function()
	return mainStore:GetAsync(plr.UserId)
end)
if success then
	for i, v in pairs(script:GetChildren()) do
		for ia, z in pairs(v:GetChildren())  do
			if z:IsA("ValueBase") then
				z.Value = data or z.Value
			end
		end
	end
else 
	print("failed to load data, rip man")
end

game.Players.PlayerRemoving:Connect(function(pla)
	if pla == plr then
		local success,err = pcall(function()
			return mainStore:SetAsync(plr.UserId)
		end)
	end
end)

I stopped writing the code once i encountered this problem

Im not entirely sure how to explain my layout any further, so heres a screenshot to give you an idea.
image

anything else needed will be provided, thank you.

1 Like

You can save tables to DataStores. You can manually (or automatically) construct your data to a table when saving, and/or to ValueBases when loading.

1 Like

so sorry for taking so long to respond, how exactly do you do this?

You can just pack all of your data values into a table: (I don’t know what you’re saving so I’ll use coins/experience as example values)

local DataToSend = {
    -- add any data that you want to save here
    Coins = 100,
    Experience = 2500
}

And then save the data. All you need to do is add DataToSend as the second parameter to your mainStore:SetAsync call

After that, calling mainStore:GetAsync(plr.UserId) will return a table identical to the one you sent:

local success,data = pcall(function()
    return mainStore:GetAsync(plr.UserId)
end)

print(data.Coins)  -- 100
print(data.Experience)  -- 2500