Hello, I’ve been having this issue recently with my pathfinding A.I that’s been bugging me for awhile. Basically, whenever the A.I gets close to the player, it starts moving really slow and goes to each waypoint one at a time making it really easy to avoid.
Here’s my code:
--Variables
local pathfindingService = game:GetService("PathfindingService")
--Dummy variables
local myHumanoid = script.Parent.Humanoid
local myTorso = script.Parent.HumanoidRootPart
--Configuration
local mag = script.Configuration.Magnitude
local view = script.Configuration.View
local check = script.Configuration.Check
--Variables
local foundPlayer
--Function that finds the closest player
function findPlayer()
local view = view.Value --View distance of dummy
--Look in the workspace for players
for i, v in pairs(game.Workspace:GetChildren()) do
local humanoid = v:FindFirstChild("Humanoid")
local torso = v:FindFirstChild("UpperTorso")
if humanoid and humanoid.Health > 0 and torso and v.Name ~= script.Parent.Name then
--Check if player is in view
if(myTorso.Position - torso.Position).magnitude < view and humanoid.Health ~= 0 then
foundPlayer = torso --Set them to the foundPlayer
end
end
end
end
--Function that creates a path to player
function createPath(player)
--Create the path
local path = pathfindingService:CreatePath()
path:ComputeAsync(myTorso.Position, player.Position)
--Get the waypoints
local waypoints = path:GetWaypoints()
--Loop through each waypoint
for i, waypoint in pairs(waypoints) do
--Make dummy jump if neeeded
if waypoint.Action == Enum.PathWaypointAction.Jump then
myHumanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
--Move to the closest waypoint
myHumanoid:MoveTo(waypoint.Position)
--Check if player moved
if(myTorso.Position - waypoints[1].Position).magnitude > check.Value then
createPath(player)
break
end
--Detect if dummy has reached waypoint
repeat wait(0.01)
local distanceBetweenPoints = (waypoint.Position - myTorso.Position).magnitude
until distanceBetweenPoints <= mag.Value
end
end
--Function that finds a player, and creates a path to them
function main()
findPlayer()
createPath(foundPlayer)
end
--Run all of the functions above
while wait() do
main()
end