Gold Ore
Lesson 5 of 18 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
endGold ore glows (Neon) and is worth Settings.OreValue * 5. This lesson's settings make gold common, so you see plenty.
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
EasyIn dropOre:
- If
math.random() < Settings.GoldChance, make the ore gold:Color3.fromRGB(255, 200, 40), Neon, and a Value ofSettings.OreValue * 5. - After parenting the ore, have the
Debrisservice remove it after 20 seconds.
Press Play and watch for gold, 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)
-- 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)
This lesson includes a short quiz. Start the lesson to answer it and track your progress.