So I am trying to script an NPC to pathfind to random waypoints in a city, but the npc just crosses the roads at any spot. What I would know is if there is a way to get the npc to recognize and use crosswalks opposed to just using the street.
1 Like
You can use Modifiers, see the post I’ve attached for examples on how to use them.
The post says it’s in Beta but it has since had a full release.
2 Likes
-
You can change the material of the crosswalks and set the material to a very low cost
-
or you can change the material of road and set the material to a very high cost
1 Like
It doesn’t do anything for me. Here is my code;
local PathfindingService = game:GetService("PathfindingService")
local Humanoid = script.Parent:WaitForChild("Humanoid")
local Root = script.Parent:WaitForChild("HumanoidRootPart")
local Roads = game.Workspace.Roads:GetChildren()
for i, Road in pairs(Roads) do
local modifierVolume = Instance.new("Part", workspace)
modifierVolume.CanCollide = false
modifierVolume.Anchored = true
modifierVolume.Transparency = 1.0
modifierVolume.Position = Road.Position
modifierVolume.Size = Road.Size
local modifier = Instance.new("PathfindingModifier", modifierVolume)
--modifier.ModifierId = "Crosswalk"
agentParameters = {
Costs = {
Road = 10.0
}
}
end
local PosibleGoals = game.Workspace.Waypints:GetChildren()
local goal = PosibleGoals[math.random(1, #PosibleGoals)]
local path = PathfindingService:CreatePath(agentParameters)
path:ComputeAsync(Root.Position, goal.Position)
local waypoints = path:GetWaypoints()
local MovingToWaypoint = false
local function Move()
MovingToWaypoint = true
for i, waypoint in ipairs(waypoints) do
Humanoid:MoveTo(waypoint.Position)
if waypoint.Action == Enum.PathWaypointAction.Jump then
Humanoid.Jump = true
end
Humanoid.MoveToFinished:Wait()
end
MovingToWaypoint = false
end
while wait(2) do
if MovingToWaypoint == false then
goal = PosibleGoals[math.random(1, #PosibleGoals)]
path = PathfindingService:CreatePath()
path:ComputeAsync(Root.Position, goal.Position)
waypoints = path:GetWaypoints()
Move()
end
end
Set the modifier.Label to “Road” on line 17
1 Like
Didn’t change anything. Any thing else that could be the reason?