A Buy Pad
Lesson 7 of 18 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
endEvery 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
MediumMake the first pad work:
- Get
ServerStorage, and keeplocal items = ServerStorage.TycoonItemsnear the top. - When a player touches
tycoon.Buttons.Dropper2Pad: do nothing unless they are the owner and the pad is still intycoon.Buttons. If their Cash is at least the pad'sPriceattribute, take the price, move the pad to ServerStorage, moveitems.Dropper2intotycoon.Built, and start it.
Press Play, earn 30 Cash and buy it, 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
-- A dropper runs one loop, however often it is started
local function startDropper(dropper)
if dropper:GetAttribute("Running") then
return
end
dropper:SetAttribute("Running", true)
task.spawn(function()
while dropper:IsDescendantOf(workspace) do
dropOre(dropper)
task.wait(Settings.DropInterval)
end
dropper:SetAttribute("Running", nil)
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)
-- 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)
This lesson includes a short quiz. Start the lesson to answer it and track your progress.