Menu
CoddyTech

Claim the Factory

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

A tycoon belongs to someone. The green pad by the entrance claims it: the first player to step on it becomes the owner, the factory starts, and only the owner is paid. The owner is one variable at the top of the script, nil until someone claims:

local owner = nil

tycoon.ClaimPad.Touched:Connect(function(hit)
	local player = Players:GetPlayerFromCharacter(hit.Parent)
	if player and not owner then
		owner = player
	end
end)

When the factory is claimed, every dropper in it starts. The droppers carry a tag, Dropper, so CollectionService:GetTagged("Dropper") finds them all, wherever they are. Bought droppers will wait in ServerStorage, so only the ones in the workspace start.

challenge icon

Challenge

Medium

Make the factory yours when you step on the green pad.

  1. Delete the line startDropper(tycoon.Dropper). Near the top, get CollectionService and add local owner = nil.
  2. When a player touches tycoon.ClaimPad and there is no owner: make them the owner and start every dropper in CollectionService:GetTagged("Dropper") that is in the workspace.
  3. At the collector, pay only owner.

Press Play: nothing drops until you step on the green pad. Then Check.

Try it yourself

local Players = game:GetService("Players")
local Debris = game:GetService("Debris")
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)
	if math.random() < Settings.GoldChance then
		ore.Color = Color3.fromRGB(255, 200, 40)
		ore.Material = Enum.Material.Neon
		ore:SetAttribute("Value", Settings.OreValue * 5)
	end
	ore.Parent = workspace
	Debris:AddItem(ore, 20)
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)

-- Lesson 6: Claim the Factory goes here

All lessons in Roblox Game: Factory Tycoon

Practice on your own: Roblox playground