Menu
CoddyTech

购买台

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

现金就是用来花的。传送带前面的蓝色垫子各自可以购买一样东西;第一个可以购买第二个投放器。垫子的价格来自一个属性 Price,这个属性是在构建世界时设置的。

要购买的物品存放在 ServerStorage.TycoonItems 中,位置已经放对了。购买时,物品会移到工厂的 Built 文件夹中,这样它就会出现在世界里;垫子则会移到 ServerStorage 中,这样就没人能踩到它了。它只是被移动了,并没有被销毁:后面的课会让垫子回来。

if cash.Value >= price then
	cash.Value -= price
	pad.Parent = ServerStorage
	items.Dropper2.Parent = tycoon.Built
end

每个碰到垫子的肢体都会触发 Touched,而且同一瞬间会触发好几次。第一次购买后,垫子就不在 tycoon.Buttons 中了,所以先检查这一点可以防止重复购买。

challenge icon

挑战

中等

让第一个蓝色垫子可以买下第二个掉落器。

  1. 在靠近顶部的位置:local items = ServerStorage.TycoonItems。
  2. 在 tycoon.Buttons.Dropper2Pad 的 Touched 事件中,除非玩家是 owner 且垫子位于 tycoon.Buttons 中,否则就返回。
  3. 如果玩家的 Cash 至少达到 Price 属性的值:扣除这笔 Cash,将垫子移到 ServerStorage,将 items.Dropper2 移入 tycoon.Built 并启动它。

按下“开始”,赚取 30 Cash,然后踩上垫子:第二个掉落器就会出现。然后按“检查”。

亲手试试

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

local tycoon = workspace.Tycoon
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

-- 矿石:从投放器下方掉落的小零件,可换取 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)

-- 第 8 课:在这里放置购买踏板

-- 认领:第一个踩上踏板的玩家将拥有工厂
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 在线编辑器