Tower defense game zombie nodes problem

So I need a way to loop through an instance numerically.

Here’s an image.
Bananaz

So I want to make this code more efficient:
local humanoid = zombie.Humanoid humanoid:MoveTo(pathfolder[2].Position) humanoid.MoveToFinished:Wait() humanoid:MoveTo(pathfolder[3].Position) humanoid.MoveToFinished:Wait() humanoid:MoveTo(pathfolder[4].Position) humanoid.MoveToFinished:Wait() humanoid:MoveTo(pathfolder[5].Position) humanoid.MoveToFinished:Wait() humanoid:MoveTo(pathfolder[6].Position) humanoid.MoveToFinished:Wait() humanoid:MoveTo(pathfolder[7].Position) humanoid.MoveToFinished:Wait() humanoid:MoveTo(pathfolder[8].Position) humanoid.MoveToFinished:Wait() humanoid:MoveTo(pathfolder[9].Position) humanoid.MoveToFinished:Wait() humanoid:MoveTo(pathfolder[10].Position) humanoid.MoveToFinished:Wait() humanoid:MoveTo(pathfolder[11].Position) humanoid.MoveToFinished:Wait() humanoid:MoveTo(pathfolder[12].Position) humanoid.MoveToFinished:Wait() humanoid:MoveTo(pathfolder[13].Position) humanoid.MoveToFinished:Wait() humanoid:MoveTo(pathfolder[14].Position) humanoid.MoveToFinished:Wait() humanoid:MoveTo(pathfolder[15].Position) humanoid.MoveToFinished:Wait() humanoid:MoveTo(pathfolder[16].Position) humanoid.MoveToFinished:Wait() humanoid:MoveTo(pathfolder[17].Position) humanoid.MoveToFinished:Wait() humanoid:MoveTo(pathfolder[18].Position) humanoid.MoveToFinished:Wait() humanoid:MoveTo(pathfolder[19].Position) humanoid.MoveToFinished:Wait() humanoid:MoveTo(pathfolder[20].Position) humanoid.MoveToFinished:Wait() humanoid:MoveTo(pathfolder[21].Position) humanoid.MoveToFinished:Wait() humanoid:MoveTo(pathfolder[22].Position) humanoid.MoveToFinished:Wait() humanoid:MoveTo(pathfolder[23].Position) humanoid.MoveToFinished:Wait() humanoid:MoveTo(pathfolder[24].Position) humanoid.MoveToFinished:Wait()

So what I need is a way to loop through a folder/instance in numeric order. Normal for I in pairs loop will just go through the hierarchy which I do not want.

1 Like
local GetPath = PathFolder:GetChildren()
local Humanoid = Zombie.Humanoid

for i = 2, #GetPath do
Humanoid:MoveTo(PathFolder[i].Position)
Humanoid.MoveToFinished:Wait()
end
1 Like

Tested. it works.

full code
`
– Zombies
local Zombie1 = game.ServerStorage.Zombies.Zombie

– Server Variables
local RunService = game:GetService(“RunService”)

local ServerVariables = game.ReplicatedStorage.ServerVariables

local WaveValue = ServerVariables.Wave

–Zombie Spawn area
local ZombieSpawnArea = game.Workspace.Grassmap.Map.Spawn

– functions

local function WalkThroughPoint(zombie, pathfolder)
if pathfolder:IsA(“Folder”) then
wait(1)
local humanoid = zombie.Humanoid
for i = 2, #pathfolder:GetChildren() do
humanoid:MoveTo(pathfolder[i].Position)
humanoid.MoveToFinished:Wait()
end
end
end

function SpawnZombie(zombie, amount, timedelay)
print(zombie)
repeat
amount = amount - 1
local zomnum = amount + 1
wait(timedelay)
local ClonedZombie = zombie:Clone()
ClonedZombie.Name = “Zombie”
ClonedZombie.Parent = workspace.Zombies
ClonedZombie.HumanoidRootPart.CFrame = ZombieSpawnArea.CFrame
spawn(function()
WalkThroughPoint(ClonedZombie, ZombieSpawnArea.Parent.Parent.Paths)
end)
until amount == 0
end

while wait(0.1) do
if WaveValue.Value == 1 then
local amount = 5
local timedelay = 0.5
SpawnZombie(Zombie1, amount, timedelay)
WaveValue.Value = WaveValue.Value + 1
end
end

`

1 Like