NPC Killer lags/stutters behind player despite having faster walkspeed

That is not option 1

For option one

  1. Clone the enemy and place the enemy into workspace

  2. set the network owner of the clone to the player the enemy is chasing

  3. tell the client somehow that they need to control the enemy maybe in a remote event or set a attribute on the enemy

  4. in a localscript for only the client who is controling the enemy they need to set the enemy’s Humanoids MoveTo function the server should not be calling the MoveTo function

1 Like

But if I’m setting network ownership to one specific player, won’t it not work for the other players? (This is a multiplayer game)

When a client has network ownership then that client will tell the server the position and velocity of the enemy and the server will tell all other clients

This is also how the players character works

If you set the players character network owner to nil then the player will become laggy

In this video I talk about network ownership it might be a good thing to watch

And I also talk about network ownership in this video a little

2 Likes

Don’t use .MoveToFinished:Wait(), the delay between the time the humanoid stops moving and the actual event firing is quite noticeable.

What you want to do is a magnitude check to detect whether your humanoid is close enough to its current waypoint.

local timeOut = false
local toWaypoint = (primarypart.Position - waypoint.Position) * Vector3.new(1,0,1)
while toWaypoint.Magnitude < 5 do 
  toWaypoint = (primarypart.Position - waypoint.Position) * Vector3.new(1,0,1)
  task.wait(0.2)
end

Of course this could yield indefinitely, so you’ll want a ways to prevent this:

  1. Have a timestamp for every ‘Move here’ command, so the code knows when to stop this loop.

  2. Something to replace timeOut with in .MoveToFinished:Wait()

So, here’s a fixed code;

if path.Status == Enum.PathStatus.Success then
	
	local now = os.clock()
	
	myHuman:SetAttribute("_lastMove", now)
	
	for _, waypoint in ipairs(waypoints) do
		if waypoint.Action == Enum.PathWaypointAction.Jump then
			myHuman.Jump = true
		end
		myHuman:MoveTo(waypoint.Position)
		
		local timeElapsed = 0
		local timeoutTime = 8; -- 8 secs
		local timeout = false 
		local toWaypoint = (primarypart.Position - waypoint.Position) * Vector3.new(1,0,1)
		while toWaypoint.Magnitude < 5 and myHuman:GetAttribute("_lastMove") == now and timeElapsed < timeoutTime do 
			toWaypoint = (primarypart.Position - waypoint.Position) * Vector3.new(1,0,1)
			timeElapsed += task.wait(0.2)
		end
		
		if timeElapsed > timeoutTime then
			timeout = true
		end
		
		if not timeout then
			--print("Got stuck")
			myHuman.Jump = true
			walkRandomly()
		end
	end
else
	--print("Path failed")
	wait(1)
	walkRandomly()
end
1 Like

Pathfinding issue most likely. My quick suggestion would be to try and set it’s target in front of the player with about 15 studs. So, it will try and walk through players, and in this way kill them.

1 Like

What is “primarypart” in this context? I’m assuming the player it’s chasing’s humanoidrootpart, if so, how can I access that inside of the script?

Oh, I meant the PrimaryPart of your NPC Killer character, since you want to get the distance between your character (the one which is pathfinding, in your case the NPC Killer) to its current waypoint.

1 Like

Oh ok, cause I tried that and the NPC completely broke and stopped moving.

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