Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

NPC runs away from players, but doesn't changes directions when a player is close to it (?)

Asked by 2 years ago

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:

01--//NPC\\--
02local NPC = script.Parent
03local Humanoid = NPC.Humanoid
04--//Services\\--
05local RunService = game:GetService("RunService")
06local Players = game:GetService("Players")
07local PathFindingService = game:GetService("PathfindingService")
08--//Functions\\--
09local function RunAway()
10    local Radius = 50
11    for _, Player in ipairs(Players:GetChildren()) do
12        if Player:IsA("Player") then
13            local Character = Player.Character
14            local RootPart = Character:WaitForChild("HumanoidRootPart")
15            if (RootPart.Position - NPC.HumanoidRootPart.Position).Magnitude < Radius then
View all 36 lines...
0
I edited the answer (again). Hope this helps. sleazel 1287 — 2y

1 answer

Log in to vote
1
Answered by
sleazel 1287 Moderation Voter
2 years ago
Edited 2 years ago

This is because even though you are looping through all players, only the path away from first player is calculated. Second and subsequent players will not be taken into account, until the humanoid reaches the end of the calculated path.

In practice it will never happen though, as You have connected Your function to the Heartbeat. So by the time the loop reaches second player, another Heartbeat recalculates everything. Heartbeat runs up to 60FPS!

So this script is essentially reverse zombie. As such, NPS must find the NEAREST player, and try to run away from it. To avoid grinding server resources to the ground, I suggest to re-check which player is closest about each second. Then you can adjust it to needs as needed.

Connecting to Heartbeat is an Overkill imho.

EDIT 2: It seems your pathfinding targets are wrong, and I do not know how did it work for you previously. For NPC to run away along the path, you have to choose a specific point on the map during the COMPUTE phase, rather than adjusting the path afterwards.

01local currentFollowedRoot
02 
03local function RunAway(RootPart)
04 
05    local NewPath = PathFindingService:CreatePath()
06    NewPath:ComputeAsync(NPC.HumanoidRootPart.Position, NPC.HumanoidRootPart.Position * Vector3.new(10,0,0)) -- adjust the target here
07 
08    if NewPath.Status == Enum.PathStatus.Success then
09 
10        local Waypoints = NewPath:GetWaypoints()
11        for _, Waypoint in pairs(Waypoints) do
12 
13            if Waypoint.Action == Enum.PathWaypointAction.Jump then
14                Humanoid.Jump = true
15                        end
View all 61 lines...

EDIT: Added coroutine for the RunAway function, to prevent it from pausing the loop.

EDIT 2: Fixed the pathfind.

0
It works, the only issue however is that it doesn't use Pathfinding anymore (Basically, when running away from a player, it doesn't pathfind so if it encounters a wall it will just simply move through it) imnotaguest1121 362 — 2y
Ad

Answer this question