Im working on my first tycoon and wanted to know how would I save a tycoon?
I have a working script but is this really the way?
And if not, how would you do it?
Heres my script:
(Module script)
local Tycoon = {}
local DataStoreService = game:GetService("DataStoreService")
local TycoonStore = DataStoreService:GetDataStore("TycoonStore")
local Players = game.Players
function Tycoon:Load(player)
--player would be the players name
local success, result = pcall(function()
return TycoonStore:GetAsync(Players:GetUserIdFromNameAsync(player))
end)
if result then
for i, v in pairs(result) do
game.ReplicatedStorage.TycoonSample[v]:Clone().Parent = workspace.Tycoon
end
return true
else
warn("Player does not have a saved tycoon")
return false
end
end
function Tycoon.new()
game.ReplicatedStorage.TycoonSample.ATM:Clone().Parent = workspace.Tycoon
game.ReplicatedStorage.TycoonSample.WallButton:Clone().Parent = workspace.Tycoon
game.ReplicatedStorage.TycoonSample.LaserDoorButton:Clone().Parent = workspace.Tycoon
end
function Tycoon:Save(player, ty)
--player would be the players name
--ty would be the path to the tycoon folder
local id = Players:GetUserIdFromNameAsync(player)
local dta = {}
for i, v in pairs(ty:GetChildren()) do
table.insert(dta, v.Name)
TycoonStore:SetAsync(id, dta)
wait()
end
end
return Tycoon
Small note at the side:
This is not the finished script and works only with ONE player and ONE Tycoon folder
Serialization is a method commonly used to save tycoons(you can find more about it here). Although you should optimize the amount of data you save and that optimization heavily depends on your game and its mechanics. Pretty much you should determine which properties remain constant and which not, then based on that only save the data that can’t be predicted on join(for example if you can’t change a tycoon part size, don’t save its size but predict it upon joining since it will be the same for everyone).
PS: Also when it comes to serialization you can round the numbers(to for example 3 decimal points) to greatly decrease the final string size(since the difference will be almost non or non-visible)
Thanks for recommending my article! Although for this case, it’s better to do something simpler.
Flags can be saved, these are basically keys inside of a table that keep track of the progress the player has performed. If he unlocked the tier 1 dropper, have a key inside a table with an intuitive name (e.g. Tier1DropperUnlocked), that is set to true once said action has happened.
This table is saved, when loading data, it’s looped through, and all keys set to true will unlock their respective dropper/item/etc. It might be a good idea to map each key to the function that performs the unlocking, when loading it’s a matter of indexing this table and calling the function for the keys inside of the saved table.
local unlocksTable = {
Tier1DropperUnlocked = function()
-- unlock
end),
... -- more
}