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

Does anyone know why I'm getting the error code of attempt to index nil with 'Position'?

When trying to define the player HumanoidRootPart Position, I'm getting the error code of attempt to index nil with 'Position'

local PathfindingService = game:GetService("PathfindingService")

local enemy = script.Parent
local humanoid = enemy.Humanoid
local Torso = enemy.Torso

	for i, v in pairs(game.Workspace:GetChildren())do-- loops through everyone in the game
	local human = (v:FindFirstChild("Humanoid"))
	local Ntorso = v:FindFirstChild("HumanoidRootPart")

while wait() do
	local currentDestination = Ntorso
	local startingPosition = Torso.Position
	local goalPosition = currentDestination.Position
	
	local path = PathfindingService:FindPathAsync(startingPosition, goalPosition)
	local waypoints = path:GetWaypoints()
	
	-- Loop through all of the waypoints in the path
	for waypointIndex, waypoint in pairs(waypoints) do
		if waypoints.Action == Enum.PathWaypointAction.Jump then
		humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
		end
		
		local waypointPosition = waypoint.Position
		
		humanoid:MoveTo(waypointPosition)
		
		end	
	end
end

1 answer

Hello there! There are some problems with your code.


Problems:

  • You'd be looping through all the children of the game, even things like baseplate, scripts, etc. The other things probably don't have a humanoid or torso. If FindFirstChid() doesn't find anything, it will return nil and cause an error.

  • The while loop will keep on looping and never get on to the other children of workspace.


Solutions:

  • Add an if statement checking if the child has a humanoid and torso. If so, it will run the while loop.

  • Add a coroutine. Coroutines are a way to do things without stopping the rest of the script.


Fixed Code:

local PathfindingService = game:GetService("PathfindingService")

local enemy = script.Parent
local humanoid = enemy.Humanoid
local Torso = enemy.Torso

for i, v in pairs(game.Workspace:GetChildren())do-- loops through everyone in the game
    local human = (v:FindFirstChildOfClass("Humanoid"))
    local Ntorso = v:FindFirstChild("HumanoidRootPart")

	coroutine.wrap(function()
        if human and Ntorso then
			while wait() do
	        	local currentDestination = Ntorso
	            local startingPosition = Torso.Position
	            local goalPosition = currentDestination.Position
	
	            local path = PathfindingService:FindPathAsync(startingPosition, goalPosition)
	            local waypoints = path:GetWaypoints()
	
	                -- Loop through all of the waypoints in the path
	            for waypointIndex, waypoint in pairs(waypoints) do
	            	if waypoints.Action == Enum.PathWaypointAction.Jump then
	                	humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
	                end
	
	               	local waypointPosition = waypoint.Position
	
	                humanoid:MoveTo(waypointPosition)

			humanoid.MoveToFinsihed:Wait()
	
	            end
	      	end
		end	
	end)()
end

3 comments

0

Thanks but when I tried this I wouldn't run the for loop in line 22.

0

What do you mean?

0

Try now.

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