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.EnemiesThử thách
DễĐưa một Zombie vào điểm bắt đầu của đường đi.
- Ở gần đầu tệp:
local ServerStorage = game:GetService("ServerStorage")vàlocal enemies = workspace.Enemies. - Sắp xếp
workspace.Path.Waypoints:GetChildren()theotonumber(a.Name)và lưu vàowaypoints. - Viết hàm (function)
spawnEnemy(kind): tạo bản sao củaServerStorage.Enemies[kind], dùngPivotTođể đặt nó cao hơnwaypoints[1]5 stud, đặt nó làm đối tượng con củaenemies, rồi trả về nó. GọispawnEnemy("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