I have created a basic pathfinding script for my game which will be expanded upon later. Currently, the script doesn’t work. The script is as follows:
--Variables
local humanoidRootPart = game.Players.LocalPlayer.HumanoidRootPart
local Humanoid = script.Parent:WaitForChild("Humanoid")
local Torso = script.Parent:WaitForChild("LowerTorso")
local PathFindingService = game:GetService("PathfindingService")
local Target = humanoidRootPart.Position
local Path = PathFindingService:CreatePath()
local HumanoidRootPart = script.Parent:WaitForChild("HumanoidRootPart")
local function followPath(destinationObject)
-- Compute and check the path
Path:ComputeAsync(HumanoidRootPart.Position, destinationObject.PrimaryPart.Position)
-- Empty waypoints table after each new path computation
waypoints = {}
if Path.Status == Enum.PathStatus.Success then
-- Get the path waypoints and start walking
waypoints = Path:GetWaypoints()
-- Move to first waypoint
currentWaypointIndex = 1
Humanoid:MoveTo(waypoints[currentWaypointIndex].Position)
else
-- Error (path not found); stop humanoid
Humanoid:MoveTo(HumanoidRootPart.Position)
end
end
local function onWaypointReached(reached)
if reached and currentWaypointIndex < #waypoints then
currentWaypointIndex = currentWaypointIndex + 1
Humanoid:MoveTo(waypoints[currentWaypointIndex].Position)
end
end
local function onPathBlocked(blockedWaypointIndex)
-- Check if the obstacle is further down the path
if blockedWaypointIndex > currentWaypointIndex then
-- Call function to re-compute the path
followPath(Target)
end
end
-- Connect 'Blocked' event to the 'onPathBlocked' function
Path.Blocked:Connect(onPathBlocked)
-- Connect 'MoveToFinished' event to the 'onWaypointReached' function
Humanoid.MoveToFinished:Connect(onWaypointReached)
while true do
followPath(Target)
wait(0.1)
end
I think this is correct, but something is still wrong. It is attempting to index ‘Wait For Child’
It is as follows:
--Variables
local character = game.Players.LocalPlayer:WaitForChild("Character")
local HumanoidRootPart = character:WaitForChild("HumanoidRootPart")
local magnitude = (HumanoidRootPart.Position - game.Workspace.Monster.HumanoidRootPart.Position).magnitude
local Monster = game.Workspace.Monster
local Humanoid = Monster:WaitForChild("Humanoid")
local Torso = Monster:WaitForChild("LowerTorso")
local PathFindingService = game:GetService("PathfindingService")
local Target = HumanoidRootPart
local Path = PathFindingService:CreatePath()
local humanoidRootPart = script.Parent:WaitForChild("HumanoidRootPart")
local function followPath(destinationObject)
-- Compute and check the path
Path:ComputeAsync(HumanoidRootPart.Position, destinationObject.PrimaryPart.Position)
-- Empty waypoints table after each new path computation
waypoints = {}
if Path.Status == Enum.PathStatus.Success then
-- Get the path waypoints and start walking
waypoints = Path:GetWaypoints()
-- Move to first waypoint
currentWaypointIndex = 1
Humanoid:MoveTo(waypoints[currentWaypointIndex].Position)
else
-- Error (path not found); stop humanoid
Humanoid:MoveTo(humanoidRootPart.Position)
end
end
local function onWaypointReached(reached)
if reached and currentWaypointIndex < #waypoints then
currentWaypointIndex = currentWaypointIndex + 1
Humanoid:MoveTo(waypoints[currentWaypointIndex].Position)
end
end
local function onPathBlocked(blockedWaypointIndex)
-- Check if the obstacle is further down the path
if blockedWaypointIndex > currentWaypointIndex then
-- Call function to re-compute the path
followPath(Target)
end
end
-- Connect 'Blocked' event to the 'onPathBlocked' function
Path.Blocked:Connect(onPathBlocked)
-- Connect 'MoveToFinished' event to the 'onWaypointReached' function
Humanoid.MoveToFinished:Connect(onWaypointReached)
while true do
if magnitude >= 50 then
followPath(Target)
end
wait(0.1)
print("Looped!!!")
end
I can see, but in theory, this method should work. Think of it like this:
The local script finds the player, and get’s the magnitude of the player to the npc, and if it is under a certain amount, say 50, it would fire a remote event and trigger the npc to go after the player.