NPC ignores players until at final position, plz help fix

Honestly tried many changes but I’ve never went too deep into programming. Could someone tell me what’s up?

(Me walking into the NPC attack zone before it gets to final point)
https://gyazo.com/f64a17b1fcccdecc39a5e72798d9d4bc

(Me after it gets to final point)
https://gyazo.com/b51857958e9613c24cfa7947e64c2458

– Set the target position for the NPC to move towards
local targetPosition = Vector3.new(0, 5, 0)

– Set the range at which the NPC will start attacking a player
local attackRange = 50

– Get a reference to the NPC’s humanoid and root part
local npcHumanoid = script.Parent:WaitForChild(“Humanoid”)
local npcRootPart = script.Parent:WaitForChild(“HumanoidRootPart”)

– Flag variable to track whether the NPC is currently attacking a player or not
local isAttackingPlayer = false

– Function to calculate the distance between two Vector3 positions
local function distanceBetweenPoints(point1, point2)
return (point1 - point2).Magnitude
end

– Function to find the nearest player to the NPC
local function findNearestPlayer()
local players = game.Players:GetPlayers()
local nearestPlayer = nil
local nearestDistance = math.huge
for i, player in ipairs(players) do
local playerDistance = distanceBetweenPoints(player.Character.HumanoidRootPart.Position, npcRootPart.Position)
if playerDistance < nearestDistance and player.Character.Humanoid.Health > 0 then
nearestPlayer = player
nearestDistance = playerDistance
end
end
return nearestPlayer
end

– Function to make the NPC move towards a position using pathfinding
local function pathfindTowardsPosition(targetPosition)
local path = game:GetService(“PathfindingService”):CreatePath()
path:ComputeAsync(npcRootPart.Position, targetPosition)
local waypoints = path:GetWaypoints()
for i, waypoint in ipairs(waypoints) do
if isAttackingPlayer then break end – Stop pathfinding if attacking player
npcHumanoid:MoveTo(waypoint.Position)
npcHumanoid.MoveToFinished:Wait()
end
end

– Function to make the NPC attack a player
local function attackPlayer(player)
isAttackingPlayer = true
while player.Character.Humanoid.Health > 0 and distanceBetweenPoints(player.Character.HumanoidRootPart.Position, npcRootPart.Position) <= attackRange do
npcHumanoid:MoveTo(player.Character.HumanoidRootPart.Position)
player.Character.Humanoid:TakeDamage(10)
wait(0.5)
end
isAttackingPlayer = false
end

– Loop forever to update the NPC’s behavior
while true do
– Check if a player is within range
local nearestPlayer = findNearestPlayer()
if nearestPlayer and distanceBetweenPoints(nearestPlayer.Character.HumanoidRootPart.Position, npcRootPart.Position) <= attackRange then
– If a player is within range and the NPC is not already attacking a player, attack them
if not isAttackingPlayer then
attackPlayer(nearestPlayer)
end
else
– Otherwise, move towards the target position
isAttackingPlayer = false – Reset attacking flag when not attacking player
pathfindTowardsPosition(targetPosition)
end
wait()
end

When you run the pathfindTowardsPosition function, you are no longer checking if a player character is within range. So your pathfinding loops through all waypoints before it looks for the nearest player character again.
I tend to check for character within range at every waypoint move. Alternatively, you could spawn a function to do that check, which would work with your current method

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.