Hello everyone, I’m having this issue when npc is chasing target it only occurs when distance is less than 8
Here is the code:
local humanoid = ugly.Humanoid
local PathfindingService = game:GetService("PathfindingService")
ugly.PrimaryPart:SetNetworkOwner(nil)
local function canSeeTarget(target)
local origin = ugly.HumanoidRootPart.Position
local direction = (target.HumanoidRootPart.Position - ugly.HumanoidRootPart.Position).unit * 40
local ray = Ray.new(origin, direction)
local hit, pos = workspace:FindPartOnRay(ray, ugly)
if hit then
if hit:IsDescendantOf(target) then
return true
end
else
return false
end
end
local function findTarget()
local players = game.Players:GetPlayers()
local maxDistance = 40
local nearestTarget
for index, player in pairs(players) do
if player.Character then
local target = player.Character
local distance = (ugly.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance < maxDistance and canSeeTarget(target) then
nearestTarget = target
maxDistance = distance
end
end
end
return nearestTarget
end
local function getPath(destination)
local pathParams = {["AgentHeight"] = 5, ["AgentRadius"] = 2, ["AgentCanJump"] = false}
local path = PathfindingService:CreatePath(pathParams)
path:ComputeAsync(ugly.HumanoidRootPart.Position, destination.Position)
return path
end
local function attack(target)
local distance = (ugly.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance > 4 then
humanoid:MoveTo(target.HumanoidRootPart.Position)
else
local attackAnim = humanoid:LoadAnimation(script.Catch)
attackAnim:Play()
target.Humanoid.Health = 0
end
end
local function walkTo(destination)
local path = getPath(destination)
if path.Status == Enum.PathStatus.Success then
for index, waypoint in pairs(path:GetWaypoints()) do
local target = findTarget()
if target and target.Humanoid.Health > 0 then
attack(target)
break
else
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
end
else
humanoid:MoveTo(destination.Position - (ugly.HumanoidRootPart.CFrame.LookVector * 10))
end
end
local function patrol()
local waypoints = workspace.waypoints:GetChildren()
local randomNum = math.random(1, #waypoints)
walkTo(waypoints[randomNum])
end
while task.wait(0.5) do
patrol()
end
The “Little stops” are caused by this. Because the waypoints are close to eachother, it does little stops as they are too close that the npc starts moving, then has to stop again. This is what I think is happening, as it has happened to me before. This may not be correct, however.
Oh yeah, and to answer that one question on the replacement for :FindPartOnRay(), im pretty sure you can just use Ray.Instance (returns the basepart that the ray touched
alright maybe you are right my waypoints are near from each other but not that close I will try to make them far of each other, also I will add Ray.Instance as you say
Also, if this pathfinding code you are making is too complicated and you cant seem to fix it, you can use this module to make pathfinding easier. You can also hibernate some code, so it is more accessible. (Hibernating meaning, you can still check if player is in sight, as you wrote in your pathfinding code)
I believe your issue is a result of the player’s position (you) who has network ownership of your character, is updating too quick for the server. Your movement takes a minute to replicate to the server.
I had similar issues before, and it’s extremely identical.