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.