The Collector
Lesson 4 of 18 in Coddy's Roblox Game: Factory Tycoon course.
At the end of the belt, the glowing green block is the collector. When an ore touches it, the ore is turned into Cash: the collector reads the ore's Value, destroys the ore and pays.
collector.Touched:Connect(function(hit)
if hit.Name == "Ore" then
local value = hit:GetAttribute("Value")
hit:Destroy()
-- pay value
end
end)Check the name first: players and anything else can touch the collector too. Read the value before destroying the ore. For now the collector pays every player in the game; in the next chapter only the factory's owner gets paid.
Challenge
EasyConnect tycoon.Collector.Touched: when the part that touched it is named Ore, read its Value attribute, destroy it, and add the value to every player's Cash.
Press Play and watch your Cash climb, then Check.
Try it yourself
local Players = game:GetService("Players")
local Settings = require(game.ReplicatedStorage.Settings)
local tycoon = workspace.Tycoon
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)
-- 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)
ore.Parent = workspace
end
local function startDropper(dropper)
task.spawn(function()
while dropper:IsDescendantOf(workspace) do
dropOre(dropper)
task.wait(Settings.DropInterval)
end
end)
end
startDropper(tycoon.Dropper)
-- The conveyor carries whatever rests on it
tycoon.Conveyor.AssemblyLinearVelocity = Vector3.new(Settings.BeltSpeed, 0, 0)
This lesson includes a short quiz. Start the lesson to answer it and track your progress.