Hello Developers!
I've made a simple AI which will run away from players using Pathfinding, the issue however that it does indeed work just it doesn't change directions when encountering different players for example, I'm chasing the AI and the AI is almost infront of another player but the AI doesn't try to move around the player rather it just simply rams said player
Script:
--//NPC\\--
local NPC = script.Parent
local Humanoid = NPC.Humanoid
--//Services\\--
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local PathFindingService = game:GetService("PathfindingService")
--//Functions\\--
local function RunAway()
local Radius = 50
for _, Player in ipairs(Players:GetChildren()) do
if Player:IsA("Player") then
local Character = Player.Character
local RootPart = Character:WaitForChild("HumanoidRootPart")
if (RootPart.Position - NPC.HumanoidRootPart.Position).Magnitude < Radius then
local NewPath = PathFindingService:CreatePath()
NewPath:ComputeAsync(RootPart.Position, NPC.HumanoidRootPart.Position)
if NewPath.Status == Enum.PathStatus.Success then
local Waypoints = NewPath:GetWaypoints()
for _, Waypoint in pairs(Waypoints) do
if Waypoint.Action == Enum.PathWaypointAction.Jump then
Humanoid.Jump = true
end
Humanoid:MoveTo(NPC.HumanoidRootPart.Position * Vector3.new(10,0,0))
Humanoid.MoveToFinished:Wait()
end
end
end
end
end
end
--//Pathfinding\\--
RunService.Heartbeat:Connect(function()
RunAway()
end)

1 comment
I edited the answer (again). Hope this helps.