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