Menu
CoddyTech

升级器

Coddy Roblox 游戏:工厂大亨 课程的第 11 课,共 22 课。

传送带上方的紫色闸门是升级器。它不会挡住矿石(它的 CanCollide 为 false),但每块经过它的矿石价值都会翻倍。

同一块矿石可能会多次触发 Touched,例如它停下后又重新启动时。翻倍两次会产生一个玩家喜欢、拥有者讨厌的 bug。因此,升级器会给每块已翻倍的矿石添加一个属性,并跳过带有该属性的矿石:

if hit.Name == "Ore" and not hit:GetAttribute("Upgraded") then
	hit:SetAttribute("Upgraded", true)
	hit:SetAttribute("Value", hit:GetAttribute("Value") * 2)
end

升级器会在购买前一直放在 ServerStorage 中,但可以在开始时连接它的 Touched 事件:无论部件在哪里,连接都会一直跟着它;只有当它进入游戏世界后,Touched 才会触发。

challenge icon

挑战

中等

让紫色升级器将每块经过它的矿石价值翻倍。

  1. 连接 items.Upgrader.Touched,只处理名为 Ore 且没有 Upgraded 属性的部件。
  2. 将 Upgraded 设置为 true,将它的 Value 属性翻倍,并将颜色设置为 Color3.fromRGB(155, 89, 182)。

按“开始”并购买升级器:矿石经过时会变成紫色,并支付 20。然后按“检查”。

亲手试试

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

local tycoon = workspace.Tycoon
local items = ServerStorage.TycoonItems
local owner = nil

print("Ore is worth " .. Settings.OreValue .. " Cash")

-- 每个玩家的排行榜上都有 Cash
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)

-- 工厂上方的标牌
local gui = Instance.new("SurfaceGui")
gui.Parent = tycoon.Sign

local signLabel = Instance.new("TextLabel")
signLabel.Name = "Status"
signLabel.Size = UDim2.fromScale(1, 1)
signLabel.BackgroundColor3 = Color3.fromRGB(25, 25, 35)
signLabel.TextColor3 = Color3.fromRGB(255, 255, 255)
signLabel.TextScaled = true
signLabel.Text = "Step on the green pad to claim this factory"
signLabel.Parent = gui

local function setSign(text)
	signLabel.Text = text
end

-- Ore:从 dropper 下方掉落的小零件,可换取 Cash
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

-- 传送带会运走放在上面的物品
tycoon.Conveyor.AssemblyLinearVelocity = Vector3.new(Settings.BeltSpeed, 0, 0)

-- 收集器将矿石转换成 Cash
tycoon.Collector.Touched:Connect(function(hit)
	if hit.Name == "Ore" then
		local value = hit:GetAttribute("Value")
		hit:Destroy()
		if owner then
			owner.leaderstats.Cash.Value += value
		end
	end
end)

-- 第 11 课:Upgrader 放在这里

-- 将购买的物品放进工厂
local function build(item)
	item.Parent = tycoon.Built
	if item:HasTag("Dropper") then
		startDropper(item)
	end
end

-- 购买后:踏板消失,对应的物品建成
local function buy(pad, player)
	if player ~= owner or pad.Parent ~= tycoon.Buttons then
		return
	end
	local cash = player.leaderstats.Cash
	local price = pad:GetAttribute("Price")
	if cash.Value < price then
		return
	end
	cash.Value -= price
	pad.Parent = ServerStorage
	build(items:FindFirstChild(pad:GetAttribute("Item")))
end

for _, pad in tycoon.Buttons:GetChildren() do
	pad.Touched:Connect(function(hit)
		local player = Players:GetPlayerFromCharacter(hit.Parent)
		if player then
			buy(pad, player)
		end
	end)
end

-- 每个踏板上方都悬浮着价格标签
for _, pad in tycoon.Buttons:GetChildren() do
	local tag = Instance.new("BillboardGui")
	tag.Name = "PriceTag"
	tag.Size = UDim2.fromOffset(160, 40)
	tag.StudsOffset = Vector3.new(0, 3, 0)
	tag.Parent = pad

	local label = Instance.new("TextLabel")
	label.Size = UDim2.fromScale(1, 1)
	label.BackgroundColor3 = Color3.fromRGB(25, 25, 35)
	label.BackgroundTransparency = 0.2
	label.TextColor3 = Color3.fromRGB(255, 255, 255)
	label.TextScaled = true
	label.Text = pad:GetAttribute("Label") .. ": $" .. pad:GetAttribute("Price")
	label.Parent = tag
end

-- 认领:第一个踏上踏板的玩家拥有这座工厂
local claimPad = tycoon.ClaimPad
claimPad.Touched:Connect(function(hit)
	local player = Players:GetPlayerFromCharacter(hit.Parent)
	if player and not owner then
		owner = player
		claimPad.Color = Color3.fromRGB(120, 120, 120)
		claimPad.Material = Enum.Material.SmoothPlastic
		setSign(player.Name .. "'s Factory")
		for _, dropper in CollectionService:GetTagged("Dropper") do
			if dropper:IsDescendantOf(workspace) then
				startDropper(dropper)
			end
		end
	end
end)

Roblox 游戏:工厂大亨中的所有课

自己练习: Roblox 在线编辑器