How Do You Create A Tycoon Saving System?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Right now I am making a big tycoon game that I want to be saved when players leave.
  2. What is the issue? Include screenshots / videos if possible!
    I don’t know how to save objects to data stores.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I looked on the forum and I found out that I should save and make ids for things that players purchase. The only problem is, I don’t know how to do that.

Here is my code for purchases!

objects = {}
teamcolor = BrickColor.new(script.Parent.Name)
wait(1)

script.Parent.Essentials.Spawn.TeamColor = teamcolor
script.Parent.Essentials.Spawn.BrickColor = teamcolor

script.Parent.Essentials.BookCollector.CollectorPart.Touched:Connect(function(hit)
	if hit:FindFirstChild("Cash") then
		script.Parent.Cash.Value = script.Parent.Cash.Value + hit.Cash.Value
		Instance.new("Sparkles", hit).Color = Color3.new(math.random(1, 255)/255, math.random(1, 255)/255)
		game.Debris:AddItem(hit, 0.1)
	end
end)

script.Parent.Essentials.Giver.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player ~= nil then
		if script.Parent.Owner.Value == player then
			if hit.Parent:FindFirstChild("Humanoid") then
				if hit.Parent.Humanoid.Health > 0 then
					local cashmoney = game.ServerStorage.MoneyStorage:FindFirstChild(player.Name)
					if cashmoney ~= nil then
						cashmoney.Value = cashmoney.Value + script.Parent.Cash.Value
						player.leaderstats.Cash.Value = cashmoney.Value
						script.Parent.Cash.Value = 0
						script.Parent.Essentials.Giver.BrickColor = BrickColor.new("Bright red")
					end
				end
			end
		end
	end
end)

script.Parent.Essentials.Giver.TouchEnded:Connect(function()
	script.Parent.Essentials.Giver.BrickColor = BrickColor.new("Bright green")
end)

script.Parent:WaitForChild("Buttons")

for i,v in pairs(script.Parent.Buttons:GetChildren()) do
	if v:FindFirstChild("Head") then
		
		local object = script.Parent.Purchases:FindFirstChild(v.Object.Value)
		if object ~= nil then
			objects[object.Name] = object:Clone()
			object:Destroy()
		else
			print("Button: "..v.Name.." is missing its object and has been removed!")
			v.Head.CanCollide = false
			v.Head.Transparency = 1
		end
		
		if v:FindFirstChild("Dependency") then
			v.Head.CanCollide = false
			v.Head.Transparency = 1
			coroutine.resume(coroutine.create(function()
				if script.Parent.PurchasedObjects:WaitForChild(v.Dependency.Value) then
					v.Head.CanCollide = true
					v.Head.Transparency = 0
				end
			end))
		end
		
		v.Head.Touched:Connect(function(hit)
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			if v.Head.CanCollide == true then
				if player ~= nil then
					if script.Parent.Owner.Value == player then
						if hit.Parent:FindFirstChild("Humanoid") then
							if hit.Parent.Humanoid.Health > 0 then
								local cashmoney = game.ServerStorage.MoneyStorage:FindFirstChild(player.Name)
								if cashmoney ~= nil then
									if cashmoney.Value >= v.Price.Value then
										cashmoney.Value = cashmoney.Value - v.Price.Value
										player.leaderstats.Cash.Value = cashmoney.Value
										objects[v.Object.Value].Parent = script.Parent.PurchasedObjects
										v.Head.CanCollide = false
										v.Head.Transparency = 1
									end
								end
							end
						end
					end
				end
			end
		end)
		
	end
end
	
2 Likes

Oops how do you format the code again?

Edit: There we go!

2 Likes

You can save player’s data with DataStoreService when they leave the game.

2 Likes

Yes I know, but the issue is that I don’t know how to save and make an id for each button a player purchases.

1 Like

Do you mean a purchase prompt?

1 Like

No. Maybe I didn’t explain properly. So, the tycoon game I am creating is a button and dropper tycoon. I want it so that for every button that the player purchases, the item that they bought saves to a datastore. This way when they join back they will have their purchased items in the tycoon. The only problem is I don’t know how to do this, so I am looking for help.

1 Like

You could keep a Module Script or Folder holding info on every button, probably just name and model. Then, when you buy an item add it to a table or Folder inside the player. You can then save that table. When loading in, simply check the Module Script or Folder that holds info on items, and load all the models of buttons the player has saved.

Kind of a basic explanation, but I don’t want to just give an entire script.

4 Likes

Could you maybe elaborate a bit more?