I am creating a big tycoon game and I need a saver could anyone help?
I have looked on the internet for some answers but I could not find any
I have tried to use a module script and server script to save the Tycoon, but it does not load properly
here are the scripts could anyone help?
Server script:
local ds = game:GetService(“DataStoreService”):GetDataStore(“CodesOtakuTutorial”)
local sl = require(game.ServerScriptService.SL)
local target = game.Workspace[“Factory”]
wait(5)
ds:SetAsync(0, sl.ModelToArray(target))
target:Destroy()
wait(5)
local loadedArray = ds:GetAsync(0)
sl.ArrayToModel(loadedArray, game.Workspace)
Module script:
local module = {}
function module.PartToArray(part)
local result = {}
result[#result+1] = part.BrickColor.Name
result[#result+1] = part.Material.Value
result[#result+1] = part.Reflectance
result[#result+1] = part.Transparency
result[#result+1] = part.Name
result[#result+1] = part.Orientation.X
result[#result+1] = part.Orientation.Y
result[#result+1] = part.Orientation.Z
result[#result+1] = part.Position.X
result[#result+1] = part.Position.Y
result[#result+1] = part.Position.Z
result[#result+1] = part.Anchored
result[#result+1] = part.CanCollide
result[#result+1] = part.Size.X
result[#result+1] = part.Size.Y
result[#result+1] = part.Size.Z
return result
end
function module.ArrayToPart(array, parent)
local part = Instance.new(“Part”)
part.BrickColor = BrickColor.new(array[1])
print(array[1])
part.Material = array[2]
part.Reflectance = array[3]
part.Transparency = array[4]
part.Name = array[5]
part.Orientation = Vector3.new(array[6],array[7],array[8])
part.Position = Vector3.new(array[9],array[10],array[11])
part.Anchored = array[12]
part.CanCollide = array[13]
part.Size = Vector3.new(array[14],array[15],array[16])
if parent then
part.Parent = parent
end
return part
end
function module.ModelToArray(model)
local result = {model.Name}
for count,child in pairs(model:GetChildren()) do
if child:IsA(“BasePart”) then
result[#result+1] = module.PartToArray(child)
end
end
return result
end
function module.ArrayToModel(array, parent)
local model = Instance.new(“Model”)
model.Name = array[1]
for count,element in pairs(array) do
if count > 1 then
module.ArrayToPart(element, model)
end
end
if parent then
model.Parent = parent
end
return model
end
Just use datastores and come up with a data system that works for you and when a player joins it’ll read the data and load the tycoon pieces they should own.
That method is a bit faulty as that method is tailored towards game’s like Miners Haven. I assume OP is referring to a more traditional tycoon with “buttons”.
You can still use the same base for it… Just with different aspects of it…
So lets say each button has a model in ServerStorage that it brings on once you buy it…
local Models = game:GetService("ServerStorage"):WaitForChild("Models") -- Replace with correct path
local datastore = game:GetService("DataStoreService"):GetDataStore("TycoonSave")
game.Players.PlayerAdded:Connect(function(plr)
local good, info = pcall(function()
return datastore:GetAsync(plr.UserId)
end)
if good then
for i,v in pairs(info) do
local model = Models[v]
--Load Model
end
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local Tycoon = --Path to their tycoon
local InfoToSave = {}
for i,v in pairs(Tycoon.Models:GetChildren()) do
table.insert(InfoToSave, v.Name)
end
datastore:SetAsync(plr.UserId, InfoToSave)
end)
This is off the top of my head so their may be bugs…
This is mostly based off of assumptions… This will work if there is a model in the Tycoon called Models that contains each separate component…
If you have 0 scripting experience, I highly down you’ll be able to take this code and get it working… But after all, this category is not for free scripts… It should give you a place to start…
It’s way simpler than the original datastore and manages saving without overflowing your request there as never been a data loss with it, I suggest you check this to learn further I really recommend using it: How to use DataStore2 - Data Store caching and data loss prevention