Menu
CoddyTech

Claim the Factory

Lesson 6 of 18 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

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.

Later the factory can be claimed again after its owner leaves. So that no dropper ever runs two loops, startDropper marks a running dropper with an attribute and does nothing if it is set.

A sign over the factory says whose it is: a SurfaceGui on tycoon.Sign, as on any board.

challenge icon

Challenge

Medium

Make the factory claimable:

  1. Get CollectionService, and add local owner = nil near the top.
  2. Put a sign on tycoon.Sign: a SurfaceGui with a TextLabel named Status filling it, white scaled text on a dark background, reading Step on the green pad to claim this factory, and a setSign(text) function.
  3. In startDropper, return at once if the dropper's Running attribute is set; otherwise set it, and clear it when the loop ends.
  4. Remove the line that starts tycoon.Dropper. When a player steps on tycoon.ClaimPad and there is no owner: make them the owner, grey the pad (Color3.fromRGB(120, 120, 120), SmoothPlastic), set the sign to Player1's Factory (with their Name), and start every tagged dropper in the workspace.
  5. Pay only the owner at the collector (if there is one).

Press Play and claim your factory, 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)
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