Hello! I’ve recently been assigned to script a tycoon from nothing to a fully functional tycoon. I haven’t quite started yet as I want to know how to resolve an issue that I know will happen in the future. I’m not quite sure how to save tycoon progress so that if a player leaves and rejoins the game their progress would be saved.
To specify more about how the tycoon would work: there is a ‘building’ with all other items in the tycoon that is prebuilt. Transparency, Can Collide, and the item’s functionality will be toggled when the item is bought.
I already know the basics DataStoreService and how to store everything from singular values to tables, but I’m unsure how I would store an entire building with some parts ‘toggled off’ and others that are active.
How can I save large amounts of data like this and how can I set up the tycoon in a way that will make it easier to do so? Any response is appreciated.
For me based off of my understanding of the problem statement I would probably give each thing you can buy either a unique name or ID. When a player buys something it would be processed by my script by what the ID they bought was which would then use that information to construct that part of the building by using that ID as the input. (for example, saving all of the parts I need to change under a folder that has that unique name so I can just access that folder or whatever by ID. Or storing a function as that ID name and calling it by ID). Then I would simply add that ID to a list that gets saved in the data store. To load it, I would then simply need to pull that list of bought items and iterate through it triggering the build function for each ID. Of course this method would mean you need to be careful in the future about naming or setting IDs to make sure that you don’t end up breaking old saves.
you can give all the items a special name and set up a table storing all the items the player bought
for eg. if you want to save a wall in your game
we can name the wall as ‘wall’
local inventory = {}
once player bought the item ‘wall’
we can add it to the table:
table.insert(inventory, ‘wall’)
local inventory = {wall}
you can then store this table in datastore when the player left etc.
when player rejoins, you get the table back
local inventory = DS:GetAsync() or {}
then you scroll through the table to check if a player has the item
if table.find(inventory, “wall”) then
workspace.Wall.CanCollide = false
workspace.Wall.Transparency = 0
you can experiment with it and find the way you are most comfortable with, this is just one of the methods doing it
To add to the above, you could make a completely empty table. Loop through the building and have each part’s properties saved with the key being its unique ID which would be whatever you named each part. Then when you call your function to grab the datastore. You would do something like
for i, v in pairs(datastore) do if i then for j, k in pairs(datastore[i]) if k ~= nil then local part = game.Workspace:FindFirstChild(i) part.j = k --[[I believe this could be used to modify properties like part.Transparency = true without actually hardcoding in which properties specifically. (never tried it but I would assume by how it is structured.)]]--
This code above is not guaranteed to work, it is merely pseudo and it could be wrong with how it is formatted but it gives you an idea of how you would call your saved properties and apply them to the part.