Menu
CoddyTech

Tạo zombie

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

Kẻ địch đi theo con đường được đánh dấu bằng các bộ phận vô hình trong workspace.Path.Waypoints, có tên từ 1 đến 6. GetChildren không đảm bảo thứ tự, vì vậy trước tiên hãy sắp xếp chúng theo số trong tên:

local points = workspace.Path.Waypoints:GetChildren()
table.sort(points, function(a, b)
	return tonumber(a.Name) < tonumber(b.Name)
end)

Mỗi kẻ địch là một model trong ServerStorage.Enemies. Hãy tạo bản sao, di chuyển toàn bộ model bằng PivotTo, rồi đặt model làm phần tử con của workspace để nó xuất hiện:

local runner = ServerStorage.Enemies.Runner:Clone()
runner:PivotTo(points[3].CFrame + Vector3.new(0, 4, 0))
runner.Parent = workspace.Enemies
challenge icon

Thử thách

Dễ

Đưa một Zombie vào điểm bắt đầu của đường đi.

  1. Ở gần đầu tệp: local ServerStorage = game:GetService("ServerStorage") và local enemies = workspace.Enemies.
  2. Sắp xếp workspace.Path.Waypoints:GetChildren() theo tonumber(a.Name) và lưu vào waypoints.
  3. Viết hàm (function) spawnEnemy(kind): tạo bản sao của ServerStorage.Enemies[kind], dùng PivotTo để đặt nó cao hơn waypoints[1] 5 stud, đặt nó làm đối tượng con của enemies, rồi trả về nó. Gọi spawnEnemy("Zombie").

Nhấn Chơi: một Zombie xuất hiện ở điểm bắt đầu của đường đi. Sau đó nhấn Kiểm tra.

Tự làm thử

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

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

-- 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)

-- Bài học 2: Thêm phần tạo Zombie tại đây

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

Tự luyện tập: Playground Roblox