The Dropper
Lesson 2 of 22 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", 25)
print(ore:GetAttribute("Value")) --> 25The 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): a1, 1, 1Part namedOre, 2.5 studs below the dropper, with aValueattribute ofSettings.OreValue, in the workspace. - Write
startDropper(dropper): intask.spawn, while the dropper is in the workspace, drop ore everySettings.DropInterval. Call it fortycoon.Dropper.
Press Play: grey 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)
-- Lesson 2: The Dropper goes here
All lessons in Roblox Game: Factory Tycoon
Practice on your own: Roblox playground