Kalananti - Candy Tycoon Game Source Code

Source Code

MainScript
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)
PurchaseMachineScript
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")

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

local Purchased = false

PurchaseButton.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 >= 0 and Purchased == false then
			Purchased = true
			Money.Value -= 0

			local NewMachine = Machine:Clone()
			NewMachine.Parent = game.Workspace

			PurchaseModel:Destroy()
		end
	end
end)
PurchaseBuildingScript
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")

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

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 = CandyFactoryBuilding: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)
SpawnScript
local ServerStorage = game:GetService("ServerStorage")
local MaterialFolder = ServerStorage.Materials

while wait(3) do
	local RawCandy = MaterialFolder:FindFirstChild("RawCandy")
	
	if RawCandy then
		local NewRawCandy = MaterialFolder.RawCandy:Clone()
		NewRawCandy.Parent = game.Workspace
	end
end
MoldingScript
local ServerStorage = game:GetService("ServerStorage")

local MoldingPart = script.Parent
local CandyFolder = ServerStorage.Candies

MoldingPart.Touched:Connect(function(RawCandy)
	local Randomize = math.random(1, 3)

	if RawCandy.Name == "RawCandy" and Randomize ~= 1 then
		RawCandy:Destroy()

		local GetAllCandies = CandyFolder:GetChildren()
		local GetRandomCandy = GetAllCandies[math.random(1, #GetAllCandies)]

		local Candy = GetRandomCandy:Clone()
		Candy.Parent = game.Workspace
	elseif RawCandy.Name == "RawCandy" and Randomize == 1 then
		RawCandy.Name = "BrokenCandy"
		RawCandy.BrickColor = BrickColor.Random()
	end
end)
QCScript
local ServerStorage = game:GetService("ServerStorage")

local QCPart = script.Parent
local MaterialFolder = ServerStorage.Materials

QCPart.Touched:Connect(function(Candy)
	if Candy.Name == "Candy" then
		Candy:Destroy()
		
		local Box = MaterialFolder.Box:Clone()
		Box.Parent = game.Workspace
	elseif Candy.Name == "BrokenCandy" then
		Candy:Destroy()
	end
end)
DestroyScript
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")

local DestroyingPart = script.Parent

DestroyingPart.Touched:Connect(function(Box)
	if Box.Name == "Box" then
		for _, Player in pairs(Players:GetPlayers()) do
			local Leaderstats = Player:WaitForChild("leaderstats")
			local Money = Leaderstats:WaitForChild("Money")
			
			Box:Destroy()
			Money.Value += 100
		end
	end
end)

Starter Project
Starter Project - Candy Factory Tycoon.rbxl (135.2 KB)