Menu
CoddyTech

A Buy Pad

Lesson 8 of 22 in Coddy's Roblox Game: Factory Tycoon course.

Cash is for spending. The blue pads in front of the belt each buy something; the first buys a second dropper. A pad knows its price from an attribute, Price, set when the world was built.

The item it buys waits in ServerStorage.TycoonItems, already in the right place. Buying moves it into the factory's Built folder, which puts it in the world, and moves the pad out to ServerStorage, where nobody can step on it. Moved, not destroyed: a later lesson brings pads back.

if cash.Value >= price then
	cash.Value -= price
	pad.Parent = ServerStorage
	items.Dropper2.Parent = tycoon.Built
end

Every limb that touches the pad fires Touched, a few times in the same moment. After the first one buys, the pad is no longer in tycoon.Buttons, so checking that first stops a double purchase.

challenge icon

Challenge

Medium

Make the first blue pad buy a second dropper.

  1. Near the top: local items = ServerStorage.TycoonItems.
  2. On tycoon.Buttons.Dropper2Pad's Touched, return unless the player is owner and the pad is in tycoon.Buttons.
  3. If their Cash is at least its Price attribute: take it, move the pad to ServerStorage, move items.Dropper2 into tycoon.Built and start it.

Press Play, earn 30 Cash, step on the pad: a second dropper appears. Then Check.

Try it yourself

local Players = game:GetService("Players")
local CollectionService = game:GetService("CollectionService")
local Debris = game:GetService("Debris")
local Settings = require(game.ReplicatedStorage.Settings)

local tycoon = workspace.Tycoon
local owner = nil

print("Ore is worth " .. Settings.OreValue .. " Cash")

-- Every player gets Cash on the leaderboard
Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local cash = Instance.new("IntValue")
	cash.Name = "Cash"
	cash.Parent = leaderstats
end)

-- The sign above the factory
local gui = Instance.new("SurfaceGui")
gui.Parent = tycoon.Sign

local signLabel = Instance.new("TextLabel")
signLabel.Name = "Status"
signLabel.Size = UDim2.fromScale(1, 1)
signLabel.BackgroundColor3 = Color3.fromRGB(25, 25, 35)
signLabel.TextColor3 = Color3.fromRGB(255, 255, 255)
signLabel.TextScaled = true
signLabel.Text = "Step on the green pad to claim this factory"
signLabel.Parent = gui

local function setSign(text)
	signLabel.Text = text
end

-- Ore: a small part worth Cash, dropped under a dropper
local function dropOre(dropper)
	local ore = Instance.new("Part")
	ore.Name = "Ore"
	ore.Size = Vector3.new(1, 1, 1)
	ore.Color = Color3.fromRGB(150, 150, 160)
	ore.Position = dropper.Position - Vector3.new(0, 2.5, 0)
	ore:SetAttribute("Value", Settings.OreValue)
	if math.random() < Settings.GoldChance then
		ore.Color = Color3.fromRGB(255, 200, 40)
		ore.Material = Enum.Material.Neon
		ore:SetAttribute("Value", Settings.OreValue * 5)
	end
	ore.Parent = workspace
	Debris:AddItem(ore, 20)
end

local function startDropper(dropper)
	task.spawn(function()
		while dropper:IsDescendantOf(workspace) do
			dropOre(dropper)
			task.wait(Settings.DropInterval)
		end
	end)
end

-- The conveyor carries whatever rests on it
tycoon.Conveyor.AssemblyLinearVelocity = Vector3.new(Settings.BeltSpeed, 0, 0)

-- The collector turns ore into Cash
tycoon.Collector.Touched:Connect(function(hit)
	if hit.Name == "Ore" then
		local value = hit:GetAttribute("Value")
		hit:Destroy()
		if owner then
			owner.leaderstats.Cash.Value += value
		end
	end
end)

-- Lesson 8: A Buy Pad goes here

-- Claiming: the first player to step on the pad owns the factory
local claimPad = tycoon.ClaimPad
claimPad.Touched:Connect(function(hit)
	local player = Players:GetPlayerFromCharacter(hit.Parent)
	if player and not owner then
		owner = player
		claimPad.Color = Color3.fromRGB(120, 120, 120)
		claimPad.Material = Enum.Material.SmoothPlastic
		setSign(player.Name .. "'s Factory")
		for _, dropper in CollectionService:GetTagged("Dropper") do
			if dropper:IsDescendantOf(workspace) then
				startDropper(dropper)
			end
		end
	end
end)

All lessons in Roblox Game: Factory Tycoon

Practice on your own: Roblox playground