Kalananti Game Coding Bootcamp - Factory Tycoon Game (13-15)

Source Code

SpawnProductScript
local ServerStorage = game:GetService("ServerStorage")

local Button = script.Parent
local ClickDetector = Button.ClickDetector
local MachineFolder = ServerStorage.ProductMachine

local SpawnDelay = false

ClickDetector.MouseClick:Connect(function()
	if SpawnDelay == false then
		local Box = MachineFolder:FindFirstChild("Box")

		if Box then
			local NewBox = Box:Clone()
			NewBox.Parent = game.Workspace

			SpawnDelay = true
			wait(3)
			SpawnDelay = false
		end
	end
end)
DestroyProductScript
local ServerStorage = game:GetService("ServerStorage")

local DestroyingPart = script.Parent
local BankMoney = ServerStorage.Bank.Money

DestroyingPart.Touched:Connect(function(Box)
	if Box.Name == "Box" then
		Box:Destroy()
		BankMoney.Value += 100
	end
end)
Leaderstats
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)
	local Leaderstats = Instance.new("Folder", Player)
	Leaderstats.Name = "leaderstats"

	local Money = Instance.new("IntValue", Leaderstats)
	Money.Name = "Money"
	Money.Value = 0
end)
WithdrawScript
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")

local WithdrawButton = script.Parent
local BankMoney = ServerStorage.Bank.Money

WithdrawButton.Touched:Connect(function(HitPart)
	local Player = Players:GetPlayerFromCharacter(HitPart.Parent)

	if Player then
		local Leaderstats = Player:WaitForChild("leaderstats")
		local Money = Leaderstats:WaitForChild("Money")

		Money.Value += BankMoney.Value
		BankMoney.Value = 0
	end
end)
ATMScreenScript
local ServerStorage = game:GetService("ServerStorage")

local BankMoney = ServerStorage.Bank.Money

BankMoney:GetPropertyChangedSignal("Value"):Connect(function()
	script.Parent.Text = BankMoney.Value
end)
UpgradeScript
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")

local UpgradeButton = script.Parent
local UpgradeModel = UpgradeButton.Parent
local AutoMachine = script.Parent.Parent.Parent.AutoMachine

UpgradeButton.Touched:Connect(function(HitPart)
	local Player = Players:GetPlayerFromCharacter(HitPart.Parent)

	if Player then
		local Leaderstats = Player:WaitForChild("leaderstats")
		local Money = Leaderstats:WaitForChild("Money")

		if Money.Value >= 500 then
			Money.Value -= 500

			AutoMachine.Value = true

			UpgradeModel:Destroy()
		end
	end
end)
AutoSpawnProductScript
local ServerStorage = game:GetService("ServerStorage")
local MachineFolder = ServerStorage.ProductMachine

local AutoMachine = script.Parent.AutoMachine

while wait(3) do
	if AutoMachine.Value == true then
		local Box = MachineFolder:FindFirstChild("Box")

		if Box then
			local NewBox = Box:Clone()
			NewBox.Parent = game.Workspace
		end
	end
end
PurchaseScript
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")

local PurchaseButton = script.Parent
local PurchaseModel = PurchaseButton.Parent
local FactoryBuilding = ServerStorage.Building.FactoryBuilding

local Purchased = false

PurchaseButton.Touched:Connect(function(HitPart)
	local Player = Players:GetPlayerFromCharacter(HitPart.Parent)

	if Player then
		local PlayerGui = Player.PlayerGui
		local ScreenGui = PlayerGui.ScreenGui
		local Leaderstats = Player:WaitForChild("leaderstats")
		local Money = Leaderstats:WaitForChild("Money")
		
		if Money.Value >= 5000 and Purchased == false then
			Purchased = true
			Money.Value -= 5000

			local NewFactoryBuilding = FactoryBuilding:Clone()
			NewFactoryBuilding.Parent = game.Workspace

			PurchaseModel:Destroy()
			
			local TextLabel = ScreenGui.TextLabel	

			TextLabel.Position = UDim2.fromScale(0.5, 1.5)

			ScreenGui.Enabled = true

			TextLabel:TweenPosition(
				UDim2.fromScale(0.5, 0.5),
				Enum.EasingDirection.Out,
				Enum.EasingStyle.Back,
				0.5,
				false
			)
		end
	end
end)

Starter Project

Starter Project - Factory Tycoon Game.rbxl (81.8 KB)