Menu
CoddyTech

Gold Ore

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

A little luck keeps players watching the belt: some ore is gold, worth five times as much. math.random() with no arguments returns a number from 0 up to (not including) 1, so it is below 0.1 one time in ten:

if math.random() < Settings.GoldChance then
	-- this one is gold
end

Gold ore glows (Neon) and is worth Settings.OreValue * 5.

Some ore will fall off the belt over the game's life. Debris:AddItem(ore, 20) removes each ore after 20 seconds, wherever it ended up, so the world never fills with stray parts.

challenge icon

Challenge

Easy

Make one ore in ten gold, and clean up stray ore.

  1. In dropOre, if math.random() < Settings.GoldChance: colour the ore Color3.fromRGB(255, 200, 40), make it Neon, and set its Value to Settings.OreValue * 5.
  2. After parenting the ore, call Debris:AddItem(ore, 20).

Press Play: now and then a glowing gold ore rides the belt and pays 50. 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)
	-- Lesson 5: Gold Ore goes here
	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)

-- The collector turns ore into Cash
tycoon.Collector.Touched:Connect(function(hit)
	if hit.Name == "Ore" then
		local value = hit:GetAttribute("Value")
		hit:Destroy()
		for _, player in Players:GetPlayers() do
			player.leaderstats.Cash.Value += value
		end
	end
end)

All lessons in Roblox Game: Factory Tycoon

Practice on your own: Roblox playground