Spawn a Zombie
Lesson 2 of 22 in Coddy's Roblox Game: Tower Defense course.
The enemies walk a path marked by invisible parts in workspace.Path.Waypoints, named 1 to 6. GetChildren makes no promise about order, so sort them by the number in their name first:
local points = workspace.Path.Waypoints:GetChildren()
table.sort(points, function(a, b)
return tonumber(a.Name) < tonumber(b.Name)
end)Each enemy is a model in ServerStorage.Enemies. Clone it, move the whole model with PivotTo, and parent it to the workspace so it appears:
local runner = ServerStorage.Enemies.Runner:Clone()
runner:PivotTo(points[3].CFrame + Vector3.new(0, 4, 0))
runner.Parent = workspace.EnemiesChallenge
EasyDrop a Zombie in at the start of the path.
- Near the top:
local ServerStorage = game:GetService("ServerStorage")andlocal enemies = workspace.Enemies. - Sort
workspace.Path.Waypoints:GetChildren()bytonumber(a.Name)intowaypoints. - Write
spawnEnemy(kind): cloneServerStorage.Enemies[kind],PivotToit 5 studs abovewaypoints[1], parent it toenemies, return it. CallspawnEnemy("Zombie").
Press Play: a Zombie drops in at the start of the path. Then Check.
Try it yourself
local Players = game:GetService("Players")
local Settings = require(game.ReplicatedStorage.Settings)
print("The base can take " .. Settings.BaseHealth .. " hits")
-- Every player gets Money on the leaderboard
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)
-- Lesson 2: Spawn a Zombie goes here
All lessons in Roblox Game: Tower Defense
Practice on your own: Roblox playground