Menu
CoddyTech

The Collector

Lesson 4 of 22 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 icon

Challenge

Easy

Turn ore into Cash at the green collector.

  1. Connect tycoon.Collector.Touched; act only when the part is named Ore.
  2. Read its Value attribute, then destroy it.
  3. Add the value to every player's Cash.

Press Play: each ore vanishes into the collector and your Cash climbs by 10. 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)

-- Lesson 4: The Collector goes here

All lessons in Roblox Game: Factory Tycoon

Practice on your own: Roblox playground