Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to save tycoon buildings?

Asked by 7 years ago

As seen in the title, I am trying to make a late game kind of tycoon. In order for that late game content to be possible, the tycoon needs to be saved, specifically the buildings, please tell me how?

0
I am bad with DataStores but, I know that you can't save objects. FiredDusk 1466 — 7y
0
You should save every thing that you bought as a string, and save the ammount of cash, then load in everything with those strings. RubenKan 3615 — 7y

1 answer

Log in to vote
3
Answered by 7 years ago
Edited 7 years ago

Nobody is going to give you an answer unless you post some already-existing code. However, I will provide some links and examples to guide you in the correct direction.

Depending on what tycoon kit you're using, you can grab all of the items that is in the Purchased folder (or what ever its called) with :GetChildren. For example, if my purchases were located in game.Workspace.Tycoon.Purchases, I can insert all of the children into a table.

local itemTable = {}

for i,v in pairs(game.Workspace.Tycoon.Purchases:GetChildren()) do
    table.insert(itemTable, v)
end

This code effectively puts all children into a table format.

Now, DataStore, the service you should use to save, has limitations, and you cannot directly save tables. So, try inserting the name of the object instead of the object itself, as shown here:

local itemTable = {}

for i,v in pairs(game.Workspace.Tycoon.Purchases:GetChildren()) do
    table.insert(itemTable, v.Name)
end

Now that we have a table of item names, we can convert this table into a string format for saving. HttpService has a built in feature to do this, also known as JSONEncode.

local itemTable = {}

for i,v in pairs(game.Workspace.Tycoon.Purchases:GetChildren()) do
    table.insert(itemTable, v.Name)
end

local condensedData = game:GetService("HttpService"):JSONEncode(itemTable)

Now that you have a string format of the purchases, save it with a unique key and DataStore.

local dataStore = game:GetService("DataStoreService"):GetDataStore("ItemSave")
local itemTable = {}

for i,v in pairs(game.Workspace.Tycoon.Purchases:GetChildren()) do
    table.insert(itemTable, v.Name)
end

local condensedData = game:GetService("HttpService"):JSONEncode(itemTable)
local playerObject = game.Players.Player1

dataStore:UpdateAsync(playerObject.UserId, function()
    return condensedData
end)

You have successfully saved the player's purchases into your game! Make sure you understand that this code will not work in-game, as the playerObject points to a specific player. Grab players and save them one by one if needed to get around this problem. Use examples I have shown you above to help you.

Now that you've saved, you can use the following code to get data:

local playerObject = game.Players.Player1
game:GetService("DataStoreService"):GetDataStore("ItemSave"):GetAsync(playerObject.UserId)

Here are some other links that will help you along the way: http://wiki.roblox.com/index.php?title=API:Class/DataStoreService http://wiki.roblox.com/index.php?title=API:Class/Instance/GetChildren http://wiki.roblox.com/index.php?title=API:Class/HttpService http://wiki.roblox.com/index.php?title=Global_namespace/Table_manipulation

Hopefully this helped. If it did, give it an upvote! :) (edited to make links clickable)

0
As for this code, where would you add the parts or droppers into the code? SynnDC 8 — 3y
Ad

Answer this question