Menu
CoddyTech

Walk the Path

Lesson 2 of 18 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:

table.sort(waypoints, function(a, b)
	return tonumber(a.Name) < tonumber(b.Name)
end)

Each enemy is a model in ServerStorage.Enemies: clone it, put it above waypoint 1 with PivotTo, and parent it to workspace.Enemies.

A Humanoid walks with MoveTo, and its MoveToFinished event fires when it arrives. It gives up after 8 seconds and fires with false, so a slow enemy on a long stretch is sent again until it gets there:

repeat
	humanoid:MoveTo(waypoints[i].Position)
until humanoid.MoveToFinished:Wait()

Walking takes time, so walkPath runs in its own thread with task.spawn, and the script carries on.

challenge icon

Challenge

Easy
  1. Get ServerStorage and local enemies = workspace.Enemies.
  2. Put the waypoints in a list and sort it by number.
  3. Write walkPath(enemy): for waypoints 2 to the last, MoveTo it until MoveToFinished says it arrived. Stop if the enemy is gone (not enemy.Parent).
  4. Write spawnEnemy(kind): clone the model, put it 5 studs above waypoint 1, parent it to enemies, start walkPath with task.spawn, and return it.
  5. Spawn one "Zombie".

Press Play and watch it walk. 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)
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Roblox Game: Tower Defense