I’m trying to create a system where NPCs spawn in, and walk in a straight line until they touch the wall on the other end. While the fastest path is apparent, NPCs on the rightmost side always swerve, leaving quite a bit of dead space. Any idea as to what the problem could be?
(waypoints shown by neon parts)
In the above image, the furthermost darkness part has passthrough enabled, as does the wall at the end of the course, since the goal of the NPCs is set just beyond it (so that they bump into the wall)
Function that spawns the NPCs
local Rig = game.ServerStorage.Rig
local SpawnPart = game.Workspace.SpawnPart
local Words = require(game.ServerScriptService.Words)
local PathInitialize = game.ServerScriptService.PathInitialize
local function SpawnEnemy(Word)
local Enemy = Rig:Clone()
local humanoid = Enemy:WaitForChild("Humanoid")
local Root = Enemy:WaitForChild("HumanoidRootPart")
local ZCoordinate = math.random((SpawnPart.Position.Z - SpawnPart.Size.X/2), (SpawnPart.Position.Z + SpawnPart.Size.X/2))
Enemy.Name = ""
Enemy.Parent = game.Workspace
Enemy:MoveTo(Vector3.new(SpawnPart.Position.X, SpawnPart.Position.Y + 3, ZCoordinate))
wait(1)
PathInitialize:Fire(Enemy)
end
while wait(2) do
SpawnEnemy()
end
Function that computes their path.
local PathInitialize = game.ServerScriptService.PathInitialize
local PathfindingService = game:GetService("PathfindingService")
PathInitialize.Event:Connect(function(EnemyNPC)
local humanoid = EnemyNPC:WaitForChild("Humanoid")
local Root = EnemyNPC:WaitForChild("HumanoidRootPart")
local path = PathfindingService:CreatePath()
local success, errorMessage = pcall(function()
path:ComputeAsync(Root.Position, Vector3.new(Root.Position.X + 138, Root.Position.Y, Root.Position.Z))
end)
if success and path.Status == Enum.PathStatus.Success then
local waypoints = path:GetWaypoints()
for i, waypoint in pairs(waypoints) do
local WaypointMarker = Instance.new("Part")
WaypointMarker.Anchored = true
WaypointMarker.Size = Vector3.new(0.5,0.5,0.5)
WaypointMarker.Material = Enum.Material.Neon
WaypointMarker.CanCollide = false
WaypointMarker.BrickColor = BrickColor.new("White")
WaypointMarker.Position = waypoint.Position
WaypointMarker.Parent = game.Workspace
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
else
print(errorMessage)
print(path.Status)
end
end)
Thus far, I’ve tried removing the wall entirely, yet the issue still persists. Any help or advice is greatly appreciated!