How to Apply Datastore to Saving Tycoon Progress?

The Issue

For the past week or so I’ve been trying to make myself a tycoon template of sorts. The tycoon system itself was based off of GamerM8’s tutorials, but one aspect he never went over was how to make a tycoon save automatically, or save at all. I’m at the point where I can make some basic scripts and use output to fix some issues. However one issue I’ve been putting off when making this tycoon system was giving it the ability to save what someone has bought.

How I THINK I Should Approach the Issue:

I’d imagine I could add a number value or some other more appropriate value type to each button to signal when they have been bought. When the MainScript buys an item, would I add a line of code (around, say line 107 and 125 where it says Items[v.Item.Value].Parent = BoughtItems") to change the value present in the button? If so, which value type should I use, or does it really matter, and how would I get this communicated to a datastore script? Another issue is how would I call for the data of a player to be loaded back in once they claim a tycoon?

Tycoon Info/Scripts

Here is what the explorer looks like for some of the tycoon.explorer

Here is the script used to basically run the tycoon, though I’d imagine the only relevant parts of the script are below the “buying functions” section.

---Variables---

local TycoonModel = script.Parent.Parent
local Items = {}

local BoughtItems = TycoonModel:FindFirstChild("BoughtItems")
local Buttons = TycoonModel:FindFirstChild("Buttons")
local DropperParts = TycoonModel:FindFirstChild("DropperParts")
local MainItems = TycoonModel:FindFirstChild("MainItems")
local Scripts = TycoonModel:FindFirstChild("Scripts")
local Values = TycoonModel:FindFirstChild("Values")

local Debounce = false
local TweenService = game:GetService("TweenService")

---Build Animation---

local function TweenVisible(Model, Time)
	local VisibleInfo = TweenInfo.new(Time, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0)
	local Tweens = {}
	local Materials = {}
	for i, v in pairs(Model:GetDescendants()) do
		if v:IsA("BasePart") then

			v:SetAttribute("OriginalColor", v.BrickColor)
			Materials[i] = v.Material
			v.Material = Enum.Material.ForceField
			v.BrickColor = BrickColor.new("Bright red")

			v.Transparency = 1
			local Tween = TweenService:Create(v, VisibleInfo, {Transparency = 0})
			Tweens[v] = Tween
			Tween:Play()
		end
	end

	wait(2.25)

	for i, v in pairs(Model:GetDescendants()) do
		if v:IsA("BasePart") then
			v.Material = Materials[i]
			v.BrickColor = v:GetAttribute("OriginalColor")
		end
	end
	return(Tweens)
end

---Tycoon Clone---

local TycoonClone = TycoonModel:Clone()
TycoonClone.Parent = game.ReplicatedStorage


---Owner Functions---

MainItems.OwnerDoor.MainDoor.Touched:Connect(function(Hit)
	if Hit.Parent:FindFirstChild("Humanoid") and Values.OwnerValue.Value == nil then
		local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
		
		if Player:FindFirstChild("OwnsTycoon").Value == false then
			Values.OwnerValue.Value = Player
			Player:FindFirstChild("OwnsTycoon").Value = true
			
			while wait() do
				MainItems.OwnerDoor.MainDoor.SurfaceGui.TextLabel.Text = tostring(Values.OwnerValue.Value).. "'s Tycoon"
			end
		end
	end
end)


---Buying Functions---

for i, v in pairs(Buttons:GetChildren()) do
	local NewItem = BoughtItems:FindFirstChild(v.Item.Value)
	if NewItem ~= nil then
		Items[NewItem.Name] = NewItem:Clone()
		NewItem:Destroy()
	else
		v.ButtonPart.Transparency = 1
		v.ButtonPart.CanCollide = false
		v.ButtonPart.BillboardGui.Frame.Visible = false
	end

	if v:FindFirstChild("Dependency") then
		coroutine.resume(coroutine.create(function()
			v.ButtonPart.Transparency = 1
			v.ButtonPart.CanCollide = false
			v.ButtonPart.BillboardGui.Frame.Visible = false
			if BoughtItems:WaitForChild(v.Dependency.Value, 100000) then
				v.ButtonPart.Transparency = 0
				v.ButtonPart.CanCollide = true
				v.ButtonPart.BillboardGui.Frame.Visible = true
			end
		end))
	end

	v.ButtonPart.Touched:Connect(function(Hit)
		if Hit.Parent:FindFirstChild("Humanoid") then
			local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
			if Values.OwnerValue.Value == Player then
				if v.ButtonPart.CanCollide == true and v.ButtonPart.Transparency == 0 then
					if v:FindFirstChild("Price") and Player:WaitForChild("leaderstats").Cash.Value >= v.Price.Value then
						if not Debounce then
							Debounce = true
							Player.leaderstats.Cash.Value -= v.Price.Value
							Items[v.Item.Value].Parent = BoughtItems
							v.ButtonPart.Building:Play()
							local time = 2.25 --Time for tween to take
							local ButtonTween = TweenService:Create(v.ButtonPart, TweenInfo.new(time), {Transparency = 1})
							local Tweens = TweenVisible(BoughtItems[v.Item.Value], time)


							ButtonTween:Play()
							wait(2.25)

							v:Destroy()
							Debounce = false
						end
					end
					if v:FindFirstChild("KillPrice") and Player:WaitForChild("leaderstats").Kills.Value >= v.KillPrice.Value then
						if not Debounce then
							Debounce = true
							Player.leaderstats.Kills.Value = v.KillPrice.Value
							Items[v.Item.Value].Parent = BoughtItems
							v.ButtonPart.Building:Play()
							local time = 2.25 --Time for tween to take
							local ButtonTween = TweenService:Create(v.ButtonPart, TweenInfo.new(time), {Transparency = 1})
							local Tweens = TweenVisible(BoughtItems[v.Item.Value], time)


							ButtonTween:Play()
							wait(2.25)

							v:Destroy()
							Debounce = false
						end
					end
				end
			end
		end
	end)
end

---Cash Functions---

local Debounce = false

MainItems.CashButton.ButtonPart.Touched:Connect(function(Hit)
	if Hit.Parent:FindFirstChild("Humanoid") then
		local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
		if Values.OwnerValue.Value == Player then
			if Debounce == false then
				Debounce = true
				
				if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(Player.UserId, 20688106) then
					Player:WaitForChild("leaderstats").Cash.Value += Values.CashValue.Value * 1.5
					if Player.MembershipType == Enum.MembershipType.Premium then
						Player:WaitForChild("leaderstats").Cash.Value += Values.CashValue.Value * 0.2
					end
					wait()
					Values.CashValue.Value = 0
					MainItems.CashButton.ButtonPart.CashNoise:Play()
					wait(1)
				else
					Player:WaitForChild("leaderstats").Cash.Value += Values.CashValue.Value
					if Player.MembershipType == Enum.MembershipType.Premium then
						Player:WaitForChild("leaderstats").Cash.Value += Values.CashValue.Value * 0.2
					end
					wait()
					Values.CashValue.Value = 0
					MainItems.CashButton.ButtonPart.CashNoise:Play()
					wait(1)
				end
				Debounce = false
			end
		end
	end
end)

while wait() do
	MainItems.CashButton.ScreenPart.SurfaceGui.TextLabel.Text = Values.CashValue.Value
end

Conclusion

Basically, I’m curious if what I have planned out is an appropriate way to handle auto saving to a datastore. If I am right, how would I do said things I asked about? If I am WAY off and have no idea what I’m talking about, what would be the best course of action? Lastly, I’d appreciate any pointers in the right direction. I don’t expect anyone to simply be like “lol just use this script replies with a script that works perfectly somehow with only like 15 lines of code” considering how complex datastores seem to be.

This may help you or not help you in any way.

Go on YouTube and watch “bricey Roblox” totorial on how to make a tycoon. It’s super amazing and is just much less time consuming and can be changed very drastically. Also his tycoon making method is very different from others. He also has a tycoon data saving video too. I personally use his method. Check it out when you have the time

2 Likes

Thank you, I believe I found the video. I will take a look into it and see how I can apply what was done in that video to the scripts I’m using.

You could use the idea of serializing stuff. Serializationnis basically thenidea of converting the part to pure data, like saving it’s position, Size etc. Btw, sorry if this is off the topic, but Gamer M8 is a she, not a he.

He only have a player data not the tycoon self from what i see

Would Binary be of use here? Or is it impractical?