Menu
CoddyTech

Cửa hàng

Bài học 7 trên 22 trong khóa học Trò chơi Roblox: phòng thủ tháp của Coddy.

Bây giờ là phía người chơi. DefenseClient là một LocalScript bên trong HUD, một ScreenGui mà Roblox sao chép lên màn hình của mỗi người chơi, vì vậy script.Parent là HUD. Khung Shop chứa một TextButton cho mỗi tháp, được đặt tên theo tháp đó.

LocalScript có thể khởi chạy trước khi các đối tượng của trò chơi đến thiết bị người chơi, vì vậy nó tìm chúng bằng WaitForChild. Sự kiện Activated của một nút sẽ kích hoạt khi bạn nhấp hoặc chạm vào nút đó:

local Settings = require(game.ReplicatedStorage:WaitForChild("Settings"))
local button = script.Parent.Shop.Frost
button.Text = "Frost tower"
button.Activated:Connect(function()
	print("Frost picked")
end)
challenge icon

Thử thách

Trung bình

Hiển thị giá trên các nút của cửa hàng trong DefenseClient.

  1. Thêm local Settings = require(game.ReplicatedStorage:WaitForChild("Settings")) và local hud = script.Parent.
  2. Với mỗi TextButton trong hud.Shop, đặt nội dung của nút thành button.Name .. " $" .. Settings.Towers[button.Name].Price.
  3. Khi sự kiện Activated xảy ra, lưu tên của nút vào local selected.

Nhấn nút Chơi: cửa hàng hiển thị Archer $60, Cannon $120 và Frost $90. Sau đó nhấn Kiểm tra.

Tự làm thử

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

local enemies = workspace.Enemies

print("The base can take " .. Settings.BaseHealth .. " hits")

-- Các điểm mốc theo thứ tự (được đặt tên 1, 2, 3...)
local waypoints = workspace.Path.Waypoints:GetChildren()
table.sort(waypoints, function(a, b)
	return tonumber(a.Name) < tonumber(b.Name)
end)

-- Máu của căn cứ là một thuộc tính của workspace: mọi client đều có thể đọc
workspace:SetAttribute("BaseHealth", Settings.BaseHealth)

-- Mỗi người chơi đều có Money 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 money = Instance.new("IntValue")
	money.Name = "Money"
	money.Value = Settings.StartMoney
	money.Parent = leaderstats
end)

-- Một kẻ địch đã đến căn cứ
local function damageBase(amount)
	local health = math.max(0, workspace:GetAttribute("BaseHealth") - amount)
	workspace:SetAttribute("BaseHealth", health)
	print("Base: " .. health)
end

-- Cho kẻ địch đi theo đường đi. MoveTo dừng sau 8 giây và báo lại
-- (MoveToFinished với false), nên kẻ địch đi chậm sẽ nhận lệnh di chuyển lại cho đến khi đến nơi.
local function walkPath(enemy)
	local humanoid = enemy.Humanoid
	for i = 2, #waypoints do
		repeat
			humanoid:MoveTo(waypoints[i].Position)
		until humanoid.MoveToFinished:Wait() or not enemy.Parent
		if not enemy.Parent then
			return
		end
	end
	-- Kẻ địch đã đến căn cứ
	damageBase(Settings.Enemies[enemy.Name].Damage)
	enemy:Destroy()
end

-- Một kẻ địch mới xuất hiện ở cổng và đi theo đường đi
local function spawnEnemy(kind)
	local enemy = ServerStorage.Enemies[kind]:Clone()
	-- Xuất hiện ngay phía trên điểm mốc đầu tiên
	enemy:PivotTo(waypoints[1].CFrame + Vector3.new(0, 5, 0))
	local info = Settings.Enemies[kind]
	enemy.Humanoid.MaxHealth = info.Health
	enemy.Humanoid.Health = info.Health
	enemy.Humanoid.WalkSpeed = info.Speed
	enemy.Parent = enemies
	task.spawn(walkPath, enemy)
	return enemy
end

-- Một đợt: cho từng kẻ địch xuất hiện, rồi chờ đến khi không còn kẻ nào
local function runWave(list)
	for _, kind in list do
		spawnEnemy(kind)
		task.wait(Settings.SpawnGap)
	end
	while #enemies:GetChildren() > 0 do
		task.wait(0.2)
	end
end

-- Các đợt, mỗi đợt bắt đầu sau một khoảng nghỉ
for i, list in Settings.Waves do
	task.wait(Settings.WaveBreak)
	runWave(list)
end

Tất cả bài học trong Trò chơi Roblox: phòng thủ tháp

Tự luyện tập: Playground Roblox