Npc gets stuck easily, and cant break out, sometimes lags

  1. Im want to achieve npc following player and avoiding objects normally without getting stuck in wall, and make him break out if he gets stuck, (also he sometimes lags) doesnt follow the player as its supposed to (goes to random places)
  2. SCRIPT
    local npc = script.Parent
    local npchum = npc.Humanoid
    local AttackAnim = Instance.new(“Animation”)
    AttackAnim.Parent = game.Workspace
    AttackAnim.AnimationId = “rbxassetid://12641925443”
    local PFS = game:GetService(“PathfindingService”)
    local RUNSERVICE = game:GetService(“RunService”)
    local debounce = false
    local DmgDebounce = false
    npc.PrimaryPart:SetNetworkOwner(nil)
    local Hitbox = game:GetService(“ReplicatedStorage”).Hitbox

local function findTarget ()
local Players = game:GetService(“Players”):GetPlayers()
local character

for _,player in pairs(Players) do
	local char = player.Character or player.CharacterAdded:Wait()
	character = char
end
return character

end

local function getPath(destination)

local path = PFS:CreatePath()
path:ComputeAsync(npc.HumanoidRootPart.Position, destination)

return path

end

local function pathFindTo (destination)
local path = getPath(destination)
local character = findTarget()

if character and character.Humanoid.Health > 0 then
	for i,waypoint in pairs(path:GetWaypoints()) do
		if waypoint.Action == Enum.PathWaypointAction.Jump then
			npchum.Jump = true
		end
		npchum:MoveTo(waypoint.Position)
		npchum.MoveToFinished:Wait()
	end
end	

end

while task.wait() do
local character = findTarget()
if character then
pathFindTo(character:WaitForChild(“HumanoidRootPart”).Position)
end

npc.Humanoid.Running:Connect(function()
	if npc.Humanoid.WalkSpeed == 0 then
		print("bros standing")
		pathFindTo(character:WaitForChild("HumanoidRootPart").Position)
	end
end)





if character then
	local Magnitude = (npc.HumanoidRootPart.Position - character:WaitForChild("HumanoidRootPart").Position).Magnitude
	
	if Magnitude <= 5 and not debounce then
		debounce = true
		local LoadAttackAnim = npc.Humanoid:LoadAnimation(AttackAnim)
		LoadAttackAnim:Play()
		local ClonedHitbox = Hitbox:Clone()
		ClonedHitbox.Parent = game.Workspace
		ClonedHitbox.Transparency = 1
		ClonedHitbox.CanCollide = false
		ClonedHitbox.Massless = true
		ClonedHitbox.Anchored = false
		ClonedHitbox.CFrame = npc.HumanoidRootPart.CFrame + npc.HumanoidRootPart.CFrame.LookVector * 2 + Vector3.new(0,2,0) 
		local Weld = Instance.new("WeldConstraint")
		Weld.Parent = ClonedHitbox
		Weld.Part0 = ClonedHitbox
		Weld.Part1 = npc.HumanoidRootPart

		ClonedHitbox.CanCollide = false
		ClonedHitbox.Massless = true
		ClonedHitbox.Anchored = false

		ClonedHitbox.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") and not DmgDebounce  then
				hit.Parent:FindFirstChild("Humanoid").Health -= 10
				DmgDebounce = true
			end

		end)

		game:GetService("Debris"):AddItem(ClonedHitbox,1)
		task.wait(1)
		debounce = false
		DmgDebounce = false
	end
end

end

2 Likes

what can i do? help please!
im stuck with it

Where did you get the script for this NPC?

My friend was writing it with me