Attempt to index nil with position

Alright so i’ve got this script.

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???

2 Likes

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
1 Like

Yeah this will probablly work but the thing is that the script worked before. i just added a line that ruined it but i don’t know which line.

1 Like

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)

nope, waypoint is a number and not an instance. you can see it from the for loop.

If waypoint is a number then logically it must be this

mob.Humanoid:MoveTo(waypoints:GetChildren()[waypoint].Position)

exactly.


Ima try that real quick…,.,.,.,.

I get
“ServerScriptService.Main.MainModuleScript:18: invalid ‘for’ limit (number expected, got Instance)”

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
1 Like

This is a different problem, what is quantity again? It’s a value passed to the function

i have no idea what quantity even is. i just used a tutorial.

Just use @wc3u 's solution, that seems to solve the issue

one more thing. why do i keep getting this??
" Module code did not return exactly one value"

Yeah it worked but i just keep geting the module did not return thingy

The problem is solved, now there’s a different problem at this line

module scripts can only return one value.

local module = {}
local anothertable = {}

return module --> doesn't error
local module = {}
local anothertable = {}

return module, anothertable --> errors
local module = {}
local anothertable = {}

return --> errors

Do i just delete that line???

can we just see the full block of code

it’d be helpful to know what you have now

I have no idea what is calling the function, can you send that script?