Menu
CoddyTech

The Dropper

Lesson 2 of 18 in Coddy's Roblox Game: Factory Tycoon course.

Money starts with ore. The grey box over the conveyor, workspace.Tycoon.Dropper, drops a small part every second. Each ore carries what it is worth as an attribute, a named value you can store on any instance:

ore:SetAttribute("Value", 10)
print(ore:GetAttribute("Value")) --> 10

The drop happens forever, so it is a loop. A loop that waits would hold up the rest of the script, so the dropper's loop runs alongside it, in task.spawn, and stops if the dropper ever leaves the world:

task.spawn(function()
	while dropper:IsDescendantOf(workspace) do
		dropOre(dropper)
		task.wait(Settings.DropInterval)
	end
end)

A new Part is unanchored unless you anchor it, so the ore falls out of the dropper by itself.

challenge icon

Challenge

Easy

Make the dropper drop ore:

  1. Near the top: local tycoon = workspace.Tycoon.
  2. Write dropOre(dropper): a 1 by 1 by 1 Part named Ore, grey (Color3.fromRGB(150, 150, 160)), 2.5 studs below the dropper, with an attribute Value set to Settings.OreValue, parented to the workspace.
  3. Write startDropper(dropper): in a task.spawn, while the dropper is in the workspace, drop an ore and wait Settings.DropInterval seconds.
  4. Call startDropper(tycoon.Dropper).

Press Play: ore piles up under the dropper. Then Check.

Try it yourself

local Players = game:GetService("Players")
local Settings = require(game.ReplicatedStorage.Settings)

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)
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Roblox Game: Factory Tycoon