每个购买台
Coddy Roblox 游戏:工厂大亨 课程的第 10 课,共 22 课。
还剩 4 个垫子。要是把购买代码复制 4 遍,就意味着每次修复错误都要修复 4 次。一个函数就能处理所有垫子:每个垫子的 Item 属性会指定它购买的物品,而 items:FindFirstChild(name) 会找到该物品。
local function buy(pad, player)
-- 检查和价格部分与之前一样
build(items:FindFirstChild(pad:GetAttribute("Item")))
end第二个函数 build(item) 会将物品放入工厂,并在物品是投放器时启动它(它带有 Dropper 标签)。墙壁、雕像和升级器也用同样的方式建造,只有投放器会启动。
挑战
中等让每个按钮都通过同一个函数运行。
- 编写
build(item):将物品移入tycoon.Built,如果item:HasTag("Dropper"),就启动它。 - 编写
buy(pad, player):像之前一样进行检查并处理价格,然后将按钮移到 ServerStorage,并创建其Item属性所命名的物品。 - 不要使用 Dropper2Pad 代码,而是将
tycoon.Buttons中每个按钮的 Touched 事件连接到buy。
按“开始”:每个你买得起的按钮都会创建对应的物品。然后按“检查”。
亲手试试
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)
-- 购买第二个 dropper 的踏板
local pad = tycoon.Buttons.Dropper2Pad
pad.Touched:Connect(function(hit)
local player = Players:GetPlayerFromCharacter(hit.Parent)
if not player or 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
cash.Value -= price
pad.Parent = ServerStorage
local dropper = items.Dropper2
dropper.Parent = tycoon.Built
startDropper(dropper)
end
-- 第 10 课:所有 Pad 都放在这里
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 在线编辑器