NPC sometimes twiches

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to fix NPC’s twiching
  2. What is the issue? Include screenshots / videos if possible!
    Here is video
    https://www.youtube.com/watch?v=PWhqN-MHp4I

here is my code


local killer = script.Parent
local humanoid = killer:FindFirstChild("Humanoid")

if not humanoid then
	warn("Humanoid not found in killer NPC!")
	return
end

local attacked = false
local chasing = false 
local ps = game:GetService("PathfindingService")
killer.PrimaryPart:SetNetworkOwner(nil)

local function findTarget()
	local players = game.Players:GetPlayers()
	local maxdist = 40
	local nearestTarget

	for _, player in pairs(players) do
		if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
			local target = player.Character
			local distance = (target.HumanoidRootPart.Position - killer.HumanoidRootPart.Position).Magnitude

			if distance < maxdist then
				nearestTarget = target
				maxdist = distance
			end
		end
	end

	return nearestTarget
end

local function getPath(destination)
	local path = ps:CreatePath()
	path:ComputeAsync(killer.HumanoidRootPart.Position, destination.Position)
	return path
end

local function attack(target)
	if not target or not target:FindFirstChild("HumanoidRootPart") then
		return
	end

	local distance = (target.HumanoidRootPart.Position - killer.HumanoidRootPart.Position).Magnitude

	if distance > 2.5 then
		humanoid:MoveTo(target.HumanoidRootPart.Position)
	else
		if not attacked then
			attacked = true
			if target:FindFirstChild("Humanoid") then
				target.Humanoid:TakeDamage(50)
			end
			wait(0.25)
			attacked = false 
		end
	end
end

local function walkTo(destination)
	if not destination then
		print("No destination provided.")
		return
	end

	local path = getPath(destination)

	if path.Status == Enum.PathStatus.Success then
		print("Path found. Moving to destination...")
		for _, waypoint in ipairs(path:GetWaypoints()) do
			humanoid:MoveTo(waypoint.Position)
			humanoid.MoveToFinished:Wait()

			if chasing then
				local target = findTarget()
				if target and target:FindFirstChild("HumanoidRootPart") then
					attack(target)
					if not chasing then
						return 
					end
				end
			end
		end
	else
		print("Pathfinding failed, cannot reach destination.")
	end
end

local function patrol()
	chasing = false 
	local waypoints = workspace:FindFirstChild("Waypoints")
	if waypoints and #waypoints:GetChildren() > 0 then
		local randomnum = math.random(1, #waypoints:GetChildren())
		print("Patrolling to waypoint:", waypoints:GetChildren()[randomnum].Name)
		walkTo(waypoints:GetChildren()[randomnum])
	else
		print("No waypoints found in workspace.Waypoints.")
	end
end

local function chase(target)
	chasing = true  
	if target and target:FindFirstChild("HumanoidRootPart") then
		walkTo(target.HumanoidRootPart)  
	end
end

while wait() do
	local target = findTarget()
	if target then
		chase(target)
	else
		patrol()
	end
end

ok first of all, please don’t use wait(), use task.wait() instead since task.wait() is faster and it’s not deprecated.

i had this problem in my game before and it got fixed once i started using SimplePath. it’s a module that removes all the pathfinding headaches for you, and it also has some excellent documentation and some tutorials that might help you when making a chasing npc.

1 Like

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