Chiếm nhà máy
Bài học 6 trên 22 trong khóa học Game Roblox: nhà máy tycoon của Coddy.
Một tycoon thuộc về một người chơi. Bệ màu xanh lá bên lối vào xác định chủ sở hữu: người chơi đầu tiên bước lên đó sẽ trở thành chủ sở hữu, nhà máy bắt đầu hoạt động và chỉ chủ sở hữu nhận được tiền. Chủ sở hữu là một biến ở đầu script, có giá trị nil cho đến khi có người nhận quyền sở hữu:
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)Khi có người nhận quyền sở hữu nhà máy, mọi máy thả đồ trong đó sẽ bắt đầu hoạt động. Các máy thả đồ có thẻ tag là Dropper, nên CollectionService:GetTagged("Dropper") sẽ tìm thấy tất cả, dù chúng ở đâu. Các máy thả đồ đã mua sẽ chờ trong ServerStorage, nên chỉ những máy nằm trong workspace mới bắt đầu hoạt động.
Thử thách
Trung bìnhHãy biến nhà máy thành của bạn khi bước lên tấm đệm màu xanh lá.
- Xóa dòng
startDropper(tycoon.Dropper). Gần đầu đoạn code, lấyCollectionServicevà thêmlocal owner = nil. - Khi người chơi chạm vào
tycoon.ClaimPadvà chưa có chủ sở hữu: đặt họ làm chủ sở hữu và khởi chạy mọi máy thả trongCollectionService:GetTagged("Dropper")nằm trong không gian làm việc. - Tại bộ phận thu gom, chỉ trả tiền cho
owner.
Nhấn Chơi: sẽ không có gì rơi xuống cho đến khi bạn bước lên tấm đệm màu xanh lá. Sau đó nhấn Kiểm tra.
Tự làm thử
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")
-- Mỗi người chơi có Cash trên bảng xếp hạng
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: một khối nhỏ có giá trị Cash, rơi xuống từ máy thả quặng
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)
-- Băng chuyền vận chuyển mọi thứ nằm trên nó
tycoon.Conveyor.AssemblyLinearVelocity = Vector3.new(Settings.BeltSpeed, 0, 0)
-- Bộ thu gom chuyển quặng thành 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)
-- Bài học 6: Phần nhận nhà máy sẽ được thêm vào đây
Tất cả bài học trong Game Roblox: nhà máy tycoon
Tự luyện tập: Playground Roblox