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")) --> 10The 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
EasyMake the dropper drop ore:
- Near the top:
local tycoon = workspace.Tycoon. - Write
dropOre(dropper): a 1 by 1 by 1 Part namedOre, grey (Color3.fromRGB(150, 150, 160)), 2.5 studs below the dropper, with an attributeValueset toSettings.OreValue, parented to the workspace. - Write
startDropper(dropper): in atask.spawn, while the dropper is in the workspace, drop an ore and waitSettings.DropIntervalseconds. - 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)
This lesson includes a short quiz. Start the lesson to answer it and track your progress.