Hi, I’m trying to create a pathfinding system that moves towards the nearest player.
I have made the system and it works great!
But my problem is that attempting to use Humanoid:MoveToFinished:Wait() Will make the NPC I’ve made to go to the last waypoint in the last created waypoint and then move towards the player,
Resulting in the player being able to go studs away while the robot tries to catch up to the position you were when it reached the end of the final waypoint.
When Attempting to remove this event it just doesn’t avoid obstacles and instead tries to access the player through the wall, However when I added parts to each waypoint it showed that it sees the path to the player but chooses for some reason to ignore it.
Without MoveToFinished: (The lines indicated the path it sees but does not take)
With MoveToFinished: (The line indicates the path created by the pathfiningservice
As you can see it will first go to the end of the last waypoint and then go to the player, which is expected. But it’s not what I want.
I want the npc to be able to avoid obstacles while being able to follow the player’s position all the time.
I’ve tried these solutions:
Attempted to use path.Blocked (Which didn’t work + activated randomly)
Attempted to raycast to check if there’s a wall infront of the npc and then move him back
(Does not work as it cannot cancel the current moveto and if it does then it will just stop completly)
Attempted to use Path.Status which again did not work as it couldn’t detect the path being blocked because it calculated the path but still decided to go to the player via the wall so it didn’t think it was blocked.
Here is my full script:
It also includes the waypoint showcase script.
If you think you can fix this please try since I’ve been trying for a while now.
-- SERVICES --
local pathfindingservice = game:GetService("PathfindingService")
local runservice = game:GetService("RunService")
--------------
-- VARIABLES --
local Entity = script:FindFirstAncestorOfClass("Model")
local PrimaryPart = Entity.PrimaryPart
local Humanoid = Entity:WaitForChild("Humanoid")
---------------
local pathparameters = {
["AgentHeight"] = 12.6,
["AgentRadius"] = 14.264
}
-- CHANGEABLE VALUES --
local killdistance = 4 -- The distance between the entity and the player which will then kill the player
local MinimumDistance = 100 -- The distance of which the entity can locate the player
-----------------------
PrimaryPart:SetNetworkOwner(nil)
function getnearestcharacter()
local players = game.Players:GetPlayers()
local target
for _, player in players do
if player.Character then
local character = player.Character
local humanoid = character.Humanoid
if humanoid.Health > 0 then
local distance = (PrimaryPart.Position - character.PrimaryPart.Position).Magnitude
if distance <= MinimumDistance then
target = character
end
else
target = nil
end
end
end
return target
end
function attackplayer(target)
local distance = (PrimaryPart.Position - target.PrimaryPart.Position).Magnitude
local path = pathfindingservice:CreatePath(pathparameters)
if distance < 4 then
target.Humanoid.Health = 0
else
path:ComputeAsync(PrimaryPart.Position,target.PrimaryPart.Position,pathparameters)
local waypoints = path:GetWaypoints()
local waypointval = 1
for i, waypoint in waypoints do
local part = Instance.new("Part")
part.Size = Vector3.new(1,1,1)
part.Position = waypoint.Position
part.Parent = game.Workspace
part.Anchored = true
part.CanCollide = false
Humanoid:MoveTo(waypoint.Position)
end
end
end
function createpath(destination)
local path = pathfindingservice:CreatePath(pathparameters)
path:ComputeAsync(PrimaryPart.Position, destination)
return path
end
function walktopath(destination)
local path = createpath(destination)
local waypoints = path:GetWaypoints()
for _, waypoint in waypoints do
local character = getnearestcharacter()
if character then
attackplayer(character)
break
else
Humanoid:MoveTo(waypoint.Position)
Humanoid.MoveToFinished:Wait()
end
end
end
function wander()
local waypoints = workspace:WaitForChild("Waypoints"):GetChildren()
for _, waypoint in waypoints do
local randomwaypoint = math.random(#waypoints)
walktopath(waypoints[randomwaypoint].Position)
end
end
while true do
wander()
end
Hastebin if it’s easier for you:
Please do not change anything in the script unless it’s about the problem it self, I don’t care if it’s not 100% efficient I just want it to work.
If you find a way to make my path finding smoother then sure you can add some edits but don’t change the names of the variables please.