Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why does my script break at "MiniToon.HumanoidRootPart.Position"?

Hey guys, I was working with some PathfindingService, and then I came to a error. My character never moved. I'll show you all my code now! Here's the code:

local PathfindingService = game:GetService("PathfindingService")

local MiniToon = game.Workspace:WaitForChild("MiniToon")
local Humanoid = game.Workspace.MiniToon:WaitForChild("Humanoid")
local endPointBrain = game.Workspace:WaitForChild("EndPointBrain")

local path = PathfindingService:CreatePath()

path:ComputeAsync(MiniToon.HumanoidRootPart.Position, endPointBrain.PrimaryPart.Position)

local waypoints = path:GetWaypoints()

for _, waypoint in pairs(waypoints) do
	local part = Instance.new("Part")
	part.Shape = "Ball"
	part.Material = "Neon"
	part.Size = Vector3.new(0.4, 0.4, 0.4)
	part.Position = waypoint.Position
	part.Anchored = true
	part.CanCollide = false
	part.Parent = game.Workspace
end


wait(10)
Humanoid:MoveTo(waypoints.Position)

Humanoid.MoveToFinished:Wait()

In my error message, it said Workspace.MiniToon.Script:10: attempt to index nil with 'Position'

I don't understand where it went wrong.

Sincerely,

Ducksneedhelp - Scripter

1 comment

0

Waypoints is an array, you're not referencing any Instance alone; the Position property doesn't exist in this context. You'll need to iterate through each Waypoint and use MoveTo respectively, along with MoveToFinished.

1 answer

Hello!

The issue is because you defined waypoints as

local waypoints = path:GetWaypoints()

However, when using the pathfinding service you have to use :MoveTo() within the for i,v loop. So, your script should be like this:

for _, waypoint in pairs(waypoints) do
	--Create the part here--
	Humanoid:MoveTo(waypoint.Position)
	Humanoid.MoveToFinished:Wait()
end

Moving on, you should add a :WaitForChild('HumanoidRootPart') so the script will yield until the HumanoidRootPart is loaded.

1 comment

0

Ah. Thank you. I'll try that now!

This discussion is preserved as it was. New answers, comments, and votes are closed.