I’m trying to make realistic npc’s that wander around and after sometime decide to go home.
But PathFindingService isn’t going to the house positions for some reason.
Does anyone have a fix?
Code:
local housetable = workspace.Houses:GetChildren()
local NPCHome = housetable[math.random(1,#housetable)]
NPCHome.Color = Color3.fromRGB(255, 0, 0)
local pathfindingService = game:GetService("PathfindingService")
local char = script.Parent.Parent
local hum = char:FindFirstChildOfClass("Humanoid")
local root = char:WaitForChild("HumanoidRootPart")
script.Parent.Values.Home.Value = NPCHome
local walkinghome = false
function pathToLocation(location)
local path = pathfindingService:CreatePath{AgentCanClimb = true, AgentCanJump = true}
path:ComputeAsync(root.Position, location)
local waypoints = path:GetWaypoints()
for k, waypoint in pairs(waypoints) do
hum:MoveTo(waypoint.Position)
if waypoint.Action == Enum.PathWaypointAction.Jump then
hum:ChangeState(Enum.HumanoidStateType.Jumping)
end
hum.MoveToFinished:Wait()
end
end
while wait(1) do
local rand = math.random(1,5)
if rand == 1 then
if walkinghome == false then
print("going home")
walkinghome = true
char.Wander.Enabled = false
pathToLocation(NPCHome.Position)
end
end
end
It gets the random house position perfectly, and it also decides to go home perfectly.
The only thing wrong is that it wont walk home.
It must have to do with the character itself, make sure no parts are anchored and use prints to make sure that everything in the script is working fine.
Yes, it means that the NPC couldn’t find a path.
Roblox added a quick “Navigation Mesh” option, if you can’t find it, it’s in Studio settings, activate it and see how clear the area is.
I’m not sure, but it could be due to the thickness of the part, idk, you could add an Attachment that indicates the entry and use that to find the path.