Help needed with datastore

Hello!

I am making a tycoon game and I need to save items that the player has bought to be saved and loaded when they rejoin. I’ve watched lots of tutorials, but they haven’t gone over saving all the children of a folder. What method would I use to go about saving this information?
Here’s an image of what I’m trying to save.

Any help is appreciated! Thanks!

1 Like

You could iterate that folder, save the names of the items into a table, then save that table into the datastore using the player.UserId

local tableToSave = {}
for _, i in pairs(BoughtItems:GetChildren()) do
	table.insert(tableToSave, i.Name)
end

local DSS = game:GetService("DataStoreService")
local Items = DSS:GetDataStore("Items")

local succ, err = pcall(function()
	return Items:SetAsync(player.UserId, tableToSave)
end)

if succ then
	warn("saving done")
else
	warn("saving failed", err)
end
2 Likes

adding to @Dev_Peashie’s solution, in order to load the data, you need to put the folder containing everything that the player can obtain and then check if the folder contains something that the player has obtained, and when it passes that, you could load it in his tycoon.

local dataStoreService= game:GetService("DataStoreService")
local items = DSS:GetDataStore("Items")
local folder = game.ServerStorage:WaitForChild('FolderName')
local data, err = pcall(function()
     return Items:GetAsync(player.UserId)
end)

for i, v in ipairs(folder:GetChildren()) do
     if table.find(data, v.Name) then
            -- stuff
      end
end
3 Likes

What would I parent the script to? ServerScriptService or the folder itself?

I like to keep most of the scripts, specially if controls something like databases in SSS (ServerScriptService)

You only need to reference the variable BoughtItems with your folder.
local BoughtItems = "unknownPath".Tycoons.BoughItems

1 Like

Hello!
My script isn’t working. Sorry I’m new to datastores and don’t know a whole lot about them. I combined yours and @mroaan 's code, but nothing is saving or loading. I can’t really tell as nothing is coming out in the output. It’s probably just a forehead slap how did I not realize that mistake problem but It’s beyond me right now. One thing is that the parts that aren’t bought yet are saved in replicated storage when the game is run as the main handler for the tycoon that I use duplicates it for backup purposes (player leaves, tycoon is reset). I accommodated that but it isn’t called when creating the folder variable for it in @mroaan 's portion. Anyways here’s the current script.
DSS problem 1

Thanks! Sorry for the inconvenience!

1 Like

bump because problem hasn’t been resolved.

1 Like

Honestly the way you’ve combined our code is very weird, your not supposed to do it like that, you’re just saving and loading the data to a non-existing player as soon as the server starts, firstly how are you planning to save/load your data? Is it as soon as the player leaves?

Sorry! I’m new to datastore code so I kind of just winged putting it together even if I knew it wasn’t right. I plan on making it to where it saves on exit (of the game) at the very least for now, since I know it’s not necessary but it’s nice to have a save every now and then in case datastores go down mid-game (again not really a priority right now). Obviously data will be loaded when the player enters.