Hello Developers!
I created a NPC which is called: "Sad Ninja". Anyway, the NPC has a Pathfinding script which will activate once someone damages the NPC. The Issue however is that the NPC Pathfinding script is buggy, let me explain. When the NPC is chasing a player, the NPC will usually briefly stop in place which makes the NPC looks like it's lagging
Script:
--//NPC\\--
local NPC = script.Parent
local Humanoid = NPC.Humanoid
local OldHealth = Humanoid.Health
--//Services\\--
local RunService = game:GetService("RunService")
local Pathfinding = game:GetService("PathfindingService")
local Players = game:GetService("Players")
--//Functions\\--
local function Jump()
Humanoid.Jump = true
end
local function FindNearestPlayer()
local RadiusToFindUser = 500
local Target = nil
for i,v in pairs(Players:GetChildren()) do
if v:IsA("Player") then
local PrimaryPart = v.Character.PrimaryPart
if (PrimaryPart.Position-NPC.HumanoidRootPart.Position).Magnitude < RadiusToFindUser then
local Path = Pathfinding:CreatePath()
Path:ComputeAsync(PrimaryPart.Position, NPC.HumanoidRootPart.Position)
if Path.Status == Enum.PathStatus.Success then
local Positions = Path:GetWaypoints()
for i,v in pairs(Positions) do
local Position = Instance.new("Part",game.Workspace)
game:GetService("Debris"):AddItem(Position,1)
Position.Anchored = true
Position.Transparency = 1
Position.CanCollide = false
Position.Position = v.Position
Humanoid:MoveTo(PrimaryPart.Position)
end
end
end
end
end
end
local function Disappear()
for _, BodyPart in ipairs(NPC:GetChildren()) do
if BodyPart:IsA("BasePart") then
BodyPart.Transparency = 1
end
end
NPC.Torso.CFrame = NPC.Torso.CFrame * CFrame.new(math.random(-5,5),0,math.random(-5,5))
wait(3)
for _, BodyPart in ipairs(NPC:GetChildren()) do
if BodyPart:IsA("BasePart") then
BodyPart.Transparency = 0
end
end
NPC.HumanoidRootPart.Transparency = 1
RunService.Heartbeat:Connect(function()
FindNearestPlayer()
end)
end
--//FireFunctions\\--
Humanoid.HealthChanged:Connect(function(newHealth)
local healthDifference = OldHealth - newHealth
if newHealth < OldHealth then
Disappear()
end
OldHealth = newHealth
end)
