Pathfinding script not working

Hello! I am currently trying to make an advanced pathfinding AI script. However, when I tried to test it, I get these errors: ‘attempt to index nil with ‘Position’’ at Line 34, and ‘attempt to index nil with ‘WaitForChild’’ at line 25.

This is my code inside a custom rig:

--//Services
local PFS = game:GetService("PathfindingService")
local PLAYERS = game:GetService("Players")

--//Variables
local wps = {}
local path = PFS:CreatePath({
	AgentRadius = 4;
	AgentHeight = 7;
	AgentCanJump = true;
	Costs = {
		Climb = 2
	}
})
local wp_index = 1
local character = script.Parent
local humanoid = script.Parent:WaitForChild("Humanoid")
local human_root = script.Parent:WaitForChild("HumanoidRootPart")

--//Getting torso
PLAYERS.PlayerAdded:Connect(function(player)
	local character = player.Character
	
	while true do wait(.1)
		local find_hum = character:WaitForChild("Humanoid") --Line 25
		local find_torso = character:FindFirstChild("Torso")
		other_torso = find_torso
		other_hum = find_hum
	end
end)

--//Follow path
function follow_path(end_node)
	path:ComputeAsync(human_root.Position, end_node.Position) --Computing the path; Line 34
	wps = {} --Refreshing waypoints array
	
	if path.Status == Enum.PathStatus.Success then --Successful
		wps = path:GetWaypoints()
		wp_index = 1
		humanoid:MoveTo(wps[wp_index].Position) --Moving the monster to the waypoint
	end
end

--//MoveToFinished
humanoid.MoveToFinished:Connect(function(node_reached)
	if node_reached and wp_index < #wps then --If the node we reached and the waypoint index is less than the number of waypoints then
		wp_index += 1 --Increase the waypoint index
		
		if wps[wp_index].Action == Enum.PathWaypointAction.Jump then --If the waypoint we're on requires a jump then the character will jump
			humanoid.Jump = true
		end
		humanoid:MoveTo(wps[wp_index].Position)
	end
end)

--//Path blocked
path.Blocked:Connect(function(blocked_wpidx)
	if blocked_wpidx > wp_index then --If the blocked waypoint index is greater than our regualar waypoint index then
		follow_path(other_torso)
	end
end)

follow_path(other_torso)

Any help will be appreciated :cowboy_hat_face:

Okay so I fixed the errors. However, when I test the game, the monster moves to the character slowly and is shaking for some reason.

Video:

Updated code:

--//Services
local PFS = game:GetService("PathfindingService")
local PLAYERS = game:GetService("Players")

--//Variables
local wps = {}
local path = PFS:CreatePath({
	["AgentRadius"] = 4;
	["AgentHeight"] = 7;
	["AgentCanJump"] = true;
	["Costs"] = {
		["Climb"] = 2
	}
})
local wp_index = 1
local character = script.Parent
local humanoid = script.Parent:WaitForChild("Humanoid")
local human_root = script.Parent:WaitForChild("HumanoidRootPart")

--//Getting torso
PLAYERS.PlayerAdded:Connect(function(player)
	char = player.Character or player.CharacterAdded:Wait()
	if char == nil then return end
	torso = char:FindFirstChild("Torso")
	if torso == nil then return end
end)

--//Follow path
function follow_path(node)
	if node == nil then return end
	local success, error_message = pcall(function()
		path:ComputeAsync(human_root.Position, node.Position) --Computing the path; Line 34
	end)
	wps = {} --Refreshing waypoints array
	
	if success and path.Status == Enum.PathStatus.Success then --Successful
		wps = path:GetWaypoints()
		wp_index = 1
		humanoid:MoveTo(wps[wp_index].Position) --Moving the monster to the waypoint
	end
end

--//MoveToFinished
humanoid.MoveToFinished:Connect(function(node_reached)
	if node_reached and wp_index < #wps then --If the node we reached and the waypoint index is less than the number of waypoints then
		wp_index += 1 --Increase the waypoint index
		
		if wps[wp_index].Action == Enum.PathWaypointAction.Jump then --If the waypoint we're on requires a jump then the character will jump
			humanoid.Jump = true
		end
		humanoid:MoveTo(wps[wp_index].Position)
	end
end)

--//Path blocked
path.Blocked:Connect(function(blocked_wpidx)
	if blocked_wpidx > wp_index then --If the blocked waypoint index is greater than our regualar waypoint index then
		follow_path(torso)
	end
end)

while true do
	task.wait(.1)
	follow_path(torso)
end

I fixed it! I just set the task.wait to .5!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.