Menu
CoddyTech

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.Enemies
challenge icon

Challenge

Easy

Drop a Zombie in at the start of the path.

  1. Near the top: local ServerStorage = game:GetService("ServerStorage") and local enemies = workspace.Enemies.
  2. Sort workspace.Path.Waypoints:GetChildren() by tonumber(a.Name) into waypoints.
  3. Write spawnEnemy(kind): clone ServerStorage.Enemies[kind], PivotTo it 5 studs above waypoints[1], parent it to enemies, return it. Call spawnEnemy("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