Attempt to index nil with position, even though position clearly exists

I’m making an underground walk system for my monster (for my game) and here’s (a bit of) my code:

inunderground:GetPropertyChangedSignal("Value"):Connect(function() --Line 33
	if inunderground.Value == true then
		local posibo = pathpoints[CODINGISHARD].Position
		print(posibo)
		hum.WalkSpeed = 25
		hum:MoveTo(posibo - Vector3.new(0, -8, 0), pathpoints[CODINGISHARD])
		task.wait(math.random(1, 4))
		hum.WalkSpeed = 0
		inunderground.Value = false
	end
end)
hum.MoveToFinished:Connect(function()
	CODINGISHARD += 1
	local posibo = pathpoints[CODINGISHARD].Position
	print(posibo)
	if inunderground.Value == true then
		hum:MoveTo(posibo - Vector3.new(0, -8, 0), pathpoints[CODINGISHARD])
	else
		hum:MoveTo(posibo, pathpoints[CODINGISHARD])
	end
end) --Line 53

Playtesting it I get these errors:

16:01:03.799  Workspace.Umbra Monstrum.FollowPath:35: attempt to index nil with 'Position'  -  Server - FollowPath:35

and

16:01:02.835  Workspace.Umbra Monstrum.FollowPath:46: attempt to index nil with 'Position'  -  Server - FollowPath:46

About the second error, I actually print posibo and it gives a Vector3, so I don’t get why it’s still giving an error.
The first one I really don’t get because I also print posibo there and it doesn’t print.

So why is this happening?
BTW pathpoints is an array of parts for pathfinding and CODINGISHARD is literally just like i in a for i, v in (blah blah) do loop

After printing like this

CODINGISHARD = 1
print(pathpoints[CODINGISHARD])

it prints 1 (the name of the instance)
So why in the world does it not work

Well the script is basically saying pathpoits[CODINGSHARD] is equal to nil. (Both errors appear in the function when this line first appears. It would be helpful if we got the full script.

While waiting for a reply I messed around trying to find a solution

repeat
	task.wait()
until (script.Parent):IsA("Model")
local col = require(game.ServerStorage:WaitForChild("Collisions"))
local inunderground
local hum = script.Parent:WaitForChild("Humanoid")
local hrp = script.Parent:WaitForChild("HumanoidRootPart")
if script.Parent.Name == "Umbra Monstrum" then
inunderground = script.Parent:WaitForChild("IsUnderground")
end
local CODINGISHARD
local currentyeeyee = hum.Parent:WaitForChild("Ajajajajaja")
local hrpy = hrp.CFrame.Y
local path = script.Parent:WaitForChild("ChosenPath")
local pathway = game.Workspace:WaitForChild("PathWay")
local chosenPath = pathway:WaitForChild(path.Value)
local merged = pathway:WaitForChild("Merge")
local chosenchildren = chosenPath:GetChildren()
local mergedchildren = merged:GetChildren()
local pathpoints = {}
local undergroundpath = (game.Workspace:WaitForChild("UndergroundPathWay")):GetChildren()
col.SetPartToGroup(undergroundpath, "Paths and Doorways")
for i, v in pairs(chosenchildren) do
	table.insert(pathpoints, v)
end
for i, v in pairs(mergedchildren) do
	table.insert(pathpoints, v)
end
print(pathpoints)
CODINGISHARD = 1
print(pathpoints[CODINGISHARD])
local posibo
local yoo
if script.Parent.Name == "Umbra Monstrum" then
	inunderground:GetPropertyChangedSignal("Value"):Connect(function()
		if inunderground.Value == true then
			yoo = pathpoints[CODINGISHARD]
			print(yoo)
			posibo = yoo.Position
			print(posibo)
			hum.WalkSpeed = 25
			hum:MoveTo(posibo - Vector3.new(0, -8, 0))
			task.wait(math.random(1, 4))
			hum.WalkSpeed = 0
			inunderground.Value = false
		end
	end)
end
hum.MoveToFinished:Connect(function()
	CODINGISHARD += 1
	yoo = pathpoints[CODINGISHARD]
	posibo = yoo.Position
	print(posibo)
	if script.Parent.Name == "Umbra Monstrum" then
		if inunderground.Value == true then
			hum:MoveTo(posibo - Vector3.new(0, -8, 0))
		else
			hum:MoveTo(posibo, yoo)
		end
	else
		hum:MoveTo(posibo, yoo)
	end	
end)
--[[for i, v in pairs(pathpoints) do
	if i == 1 or i == 5 then
		continue
	end
		inunderground:GetPropertyChangedSignal("Value"):Connect(function()
		if inunderground.Value == true then
			hum.WalkSpeed = 25
			hum:MoveTo(v.Position - Vector3.new(0, -8, 0), v)
			task.wait(math.random(1, 4))
			hum.WalkSpeed = 0
			inunderground.Value = false
		end
	end)
	hum:MoveTo(v.Position, v)
	hum.MoveToFinished:Wait()
	print("CODING IS HARD")
end]]
hum:MoveTo(pathpoints[CODINGISHARD].Position, pathpoints[CODINGISHARD])

PS: Don’t mind that huge comment down at the end, it’s part of old code

try printing pathpoints before yoo = pathpoints[CODINGISHARD] and see what it prints.

Is the monster walking at all?

Yes, the pathfinding works, it’s just the code involving posibo and stuff that isn’t working

I put print in

	CODINGISHARD += 1
	print(pathpoints[CODINGISHARD]) --Prints 3
	yoo = pathpoints[CODINGISHARD]

and

if inunderground.Value == true then
			print(pathpoints[CODINGISHARD]) --Prints nil (???)
			yoo = pathpoints[CODINGISHARD]

print the table by itself print(pathpoints),
How many points are there? It could be going passed the amount of points by the time it gets underground.

Already did, it has 5 points, based on past attempts of fixing this, MoveToFinished is fired when I start moving the monster down, and

this line runs

hmm, maybe print this print(CODINGISHARD) before

if inunderground.Value == true then
			print(pathpoints[CODINGISHARD]) --Prints nil (???)
			yoo = pathpoints[CODINGISHARD]

can’t really see the issue other wise if this prints something lower then 6

		if inunderground.Value == true then
			print(CODINGISHARD) --Line 37
			print(pathpoints[CODINGISHARD])
6  -  Server - FollowPath:37

I have an idea that didn’t work earlier, maybe I can implement it later. I’ll go off for a while. (The idea was that the CODINGISHARD += 1 line would be inside an if statement that would check if the hrp’s Y coordinate is equal to what it was before inunderground is set to true, if it is then CODINGISHARD += 1 will fire)

Ye, the issue is that it is getting to 6 when there is only 5 points.

In the function that has hum.MoveToFinished it moves the humanoid twice if it is underground since both functions move it? This could be where it is going wrong.

1 Like

The code is just barely working right, just need to fix this

if hrp.CFrame.Y == hrpy
hum.MoveToFinished:Connect(function()
	print(hrp.CFrame.Y)
	if hrp.CFrame.Y == hrpy or inunderground.Value == true then
		CODINGISHARD += 1
		print(pathpoints[CODINGISHARD])
		yoo = pathpoints[CODINGISHARD]
		posibo = yoo.Position
		print(posibo)
		if script.Parent.Name == "Umbra Monstrum" then
			if inunderground.Value == true then
				hum:MoveTo(posibo - Vector3.new(0, -8, 0))
			else
				hum:MoveTo(posibo, yoo)
			end
		else
			hum:MoveTo(posibo, yoo)
		end	
	end
end)

I can see the end

EDIT: ALMOST THERE, JUST NEED TO MAKE THE MONSTER MOVE WHEN IT GOES BACK UP

if script.Parent.Name == "Umbra Monstrum" then
	inunderground:GetPropertyChangedSignal("Value"):Connect(function()
		if inunderground.Value == true then
			print(CODINGISHARD)
			print(pathpoints[CODINGISHARD])
			yoo = pathpoints[CODINGISHARD]
			print(yoo)
			posibo = yoo.Position
			print(posibo)
			hum.WalkSpeed = 25
			hum:MoveTo(posibo - Vector3.new(0, -8, 0))
			task.wait(math.random(1, 2))
			hum.WalkSpeed = 0
			inunderground.Value = false
		end
	end)
end
hum.MoveToFinished:Connect(function()
	print(hrp.CFrame.Y)
	if cannotname.Value == 1 or inunderground.Value == true then
		CODINGISHARD += 1
		print(pathpoints[CODINGISHARD])
		yoo = pathpoints[CODINGISHARD]
		posibo = yoo.Position
		print(posibo)
		if script.Parent.Name == "Umbra Monstrum" then
			if inunderground.Value == true then
				hum:MoveTo(posibo - Vector3.new(0, -8, 0))
			else
				hum:MoveTo(posibo, yoo)
			end
		else
			hum:MoveTo(posibo, yoo)
		end	
	end
end)

After thinking, sleeping, waking up, and trying lots of times, I have the answer

repeat
	task.wait()
until (script.Parent):IsA("Model")
local col = require(game.ServerStorage:WaitForChild("Collisions"))
local inunderground
local cannotname
local hum = script.Parent:WaitForChild("Humanoid")
local hrp = script.Parent:WaitForChild("HumanoidRootPart")
if script.Parent.Name == "Umbra Monstrum" then
	inunderground = script.Parent:WaitForChild("IsUnderground")
	cannotname = hum.Parent:WaitForChild("LiterallyCan'tNameAnything")
end
local CODINGISHARD
local currentyeeyee = hum.Parent:WaitForChild("Ajajajajaja")
local hrpy = hrp.CFrame.Y
print(hrpy)
local path = script.Parent:WaitForChild("ChosenPath")
local pathway = game.Workspace:WaitForChild("PathWay")
local chosenPath = pathway:WaitForChild(path.Value)
local merged = pathway:WaitForChild("Merge")
local chosenchildren = chosenPath:GetChildren()
local mergedchildren = merged:GetChildren()
local pathpoints = {}
local undergroundpath = (game.Workspace:WaitForChild("UndergroundPathWay")):GetChildren()
col.SetPartToGroup(undergroundpath, "Paths and Doorways")
for i, v in pairs(chosenchildren) do
	table.insert(pathpoints, v)
end
for i, v in pairs(mergedchildren) do
	table.insert(pathpoints, v)
end
print(pathpoints)
CODINGISHARD = 1
print(pathpoints[CODINGISHARD])
local posibo
local yoo
if script.Parent.Name == "Umbra Monstrum" then
	cannotname.Changed:Connect(function(a)
		print("Gottem")
		print(a)
		if a == 1 then
			print("I DID IT")
			print(CODINGISHARD)
			print(pathpoints[CODINGISHARD])
			yoo = pathpoints[CODINGISHARD]
			print(yoo)
			posibo = yoo.Position
			print(posibo)
			hum:MoveTo(posibo, yoo)
		end
	end)
	inunderground:GetPropertyChangedSignal("Value"):Connect(function()
		if inunderground.Value == true then
			print(CODINGISHARD)
			print(pathpoints[CODINGISHARD])
			yoo = pathpoints[CODINGISHARD]
			print(yoo)
			posibo = yoo.Position
			print(posibo)
			hum.WalkSpeed = 25
			hum:MoveTo(posibo - Vector3.new(0, -8, 0))
			task.wait(math.random(1, 2))
			hum.WalkSpeed = 0
			inunderground.Value = false
		end
	end)
end
hum.MoveToFinished:Connect(function()
	print(hrp.CFrame.Y)
	if cannotname.Value == 1 or inunderground.Value == true then --Goals for tomorrow (In case I forget) make monster move when it goes back up, use GetPropertyChangedSignal for cannotname
		CODINGISHARD += 1
		print(pathpoints[CODINGISHARD])
		yoo = pathpoints[CODINGISHARD]
		posibo = yoo.Position
		print(posibo)
		if script.Parent.Name == "Umbra Monstrum" then
			if inunderground.Value == true then
				hum:MoveTo(posibo - Vector3.new(0, -8, 0))
			else
				hum:MoveTo(posibo, yoo)
			end
		else
			hum:MoveTo(posibo, yoo)
		end	
	end
end)
hum:MoveTo(pathpoints[CODINGISHARD].Position, pathpoints[CODINGISHARD])