local ServerStorage = game:GetService("ServerStorage")
local MOB = {}
function MOB.Move(mob, map)
local humanoid = mob:WaitForChild("Humanoid")
local waypoints = workspace.WayPoints
for waypoint=1, #waypoints:GetChildren() do
mob.Humanoid:MoveTo(waypoints:FindFirstChild(waypoint).Position)
mob.Humanoid.MoveToFinished:Wait()
end
mob:Destroy()
end
function MOB.Spawn(name, quantity, map)
local mobExists = ServerStorage.MOBS:FindFirstChild(name)
if mobExists then
for i=1, quantity do
task.wait(0.5)
local newMob = mobExists:Clone()
newMob.HumanoidRootPart.CFrame = workspace.MAP.ZombieSpawn.CFrame
newMob.Parent = map.Mob
coroutine.wrap(MOB.Move)(newMob, map)
end
else
warn("idk")
end
end
return MOB
And whenever i play test my game i get this error
“ServerScriptService.Main.MainModuleScript:8: attempt to index nil with ‘Position’”
How would i fix it???
Try this, it’s probably because you are finding the waypoint via the instance and not the waypoint name.
for waypoint=1, #waypoints:GetChildren() do
--mob.Humanoid:MoveTo(waypoints:FindFirstChild(waypoint).Position)
mob.Humanoid:MoveTo(waypoints:FindFirstChild(waypoint.Name).Position)
mob.Humanoid.MoveToFinished:Wait()
end
this is line 8.
the error says “attempt to index nil with position” so logically waypoints:FindFirstChild(waypoint) is nil.
also can you screenshot the workspace.WayPoints folder? (from the explorer)
waypoint contains the index, whilst you need the name for FindFirstChild. Using a numeric for loop like that is confusing and is slower than just a generic for loop.
Use a generic for loop so you can get the instance directly:
for _, waypoint in waypoints:GetChildren() do
mob.Humanoid:MoveTo(waypoint.Position)
mob.Humanoid.MoveToFinished:Wait()
end