I’m trying to make a game that is somewhat a tycoon but I don’t know the best way to save player data (Like with the buttons). Does anyone know the best way?
You cannot save world objects to “DataStore” however you can save their data such as position/colour
in terms of the best way I am unsure but hope the little advise above will help when it comes to a solution
Im quite sure they use hash tables storing data. From what I know, that’d be the best solution as it could all fit into one key.
This doesn’t mean put everything in a game in one key as a hash table in general though, that’s bad practice.
I just think that’s the best way for a tycoon to save data.
You could assign a number to all of the buttons, and then save them as true or false, true if it’s bought and false if it’s not. And then when you join, you will know which one is bought and you can place the same parts that it would place when you buy it.
Edit: You can also just save the ones that are true, you don’t need to save the false ones too. You can just check if the data exists, and if not, then its false
The best way to do it, in my opinion, is to save the name of the object you want and have it load up when ever the player claims a tycoon. Basically clone from the rep storage and placing it in the workspace like a tycoon does when you buy things.
Best way I’ve found is to assign each button an ID, you then put that ID into a tycoon table for the player. This table is what you save and load.
Once you load the table, you loop through it, for actually loading the items it depends on some stuff, on a traditional tycoon you could just purchase the buttons but not take away the players money, for a sandbox tycoon you’d keep a model in ReplicatedStorage and clone it into the right position.
Adding an item to the table would be like
--in your buying button function
--PlayerData is your entire datastore table, .Tycoon is the button data
PlayerData.Tycoon[ID] = true -- you could replace 'true' with other info you want to save
Then when you load it’d look something like
for i,v in PlayerData.Tycoon do --you no longer need pairs or ipairs, new update lets you just use a table
PurchaseItem(i, false) --i is the ID, false means we don't spend money
end
You can make a table after player leaving with all the buttons that activated.
local activate_buttons = {button1, button2, button4}
activate_buttons = game:GetService("HttpService"):JSONEncode(acitvate_buttons)
Then JSONEncode it to a string, and save with datastore.
local loaded_data = game:GetService("HttpService"):JSONDecode(data)
After player joined the game again you JSONDecode the data you save and you’re getting the same table as you was saving. Then just make these buttons already activated as you did it at first.
There is no need to JSONEncode data going into a datastore. It encodes it and decodes it behind the scenes already, this’ll use more data and take more time than necessary.