Help with npc that surrounds player?

Hi! I am currently trying to get this npc that surrounds the player to work

However, I am pretty sure I am not doing this in the best way possible + it stutters and lags a lot. This wouldn’t be a problem if the npc didn’t keep being outran by the player (even with a higher walkspeed) and the walking animation wasn’t stuttering

Basically, the target (for testing purposes this target is the player at the moment) has a cylinder welded to it’s HumanoidRootPart and the npc keeps circling around random areas right outside of the cylinder.

Here’s the script:

--SCRIPT 1
local Path
local PreviousWaypoint
local Part = workspace.Part -- The cylinder that is welded to the player
script.Parent.PrimaryPart:SetNetworkOwner(nil)

local function GetPath(destination)
	Path = game:GetService("PathfindingService"):CreatePath({WaypointSpacing = math.huge, Costs = {TargetCylinder = math.huge}}) -- the cylinder has a PathfindingModifier labeled "TargetCylinder"
	Path:ComputeAsync(script.Parent.HumanoidRootPart.Position, destination)
end
while wait() do
	local SetupWaypointCFrame = CFrame.new(Part.Position) * CFrame.Angles(0, math.random(0, 360), 0)
	local WaypointCFrame = SetupWaypointCFrame + (SetupWaypointCFrame.LookVector * Vector3.new(Part.Size.Z - 5, 0, Part.Size.Z - 5))
	GetPath(WaypointCFrame.Position)
	for i, v in pairs(Path:GetWaypoints()) do
		local GreatHeight = false
		if PreviousWaypoint ~= nil then
			if PreviousWaypoint.Position.Y - v.Position.Y >= 20 then
				GreatHeight = true
			end
		end
		PreviousWaypoint = v
		if not GreatHeight then
			script.Parent.Humanoid:MoveTo(v.Position)
			script.Parent.Humanoid.MoveToFinished:Wait()
		else
			break
		end
	end
end
--

--SCRIPT 2, WHERE I'M PRETTY SURE THE STUTTER AND LAGS ISSUE IS ORIGINATING FROM
while wait(.1) do
	script.Parent.PrimaryPart.CFrame = CFrame.new(script.Parent.HumanoidRootPart.Position, workspace.Part.Position)
end
--

Is there any way I can fix the outrunning / walking animation stutter problem? :happy2:

1 Like

Should’ve probably shared a video too…

(Note how much faster I am than the npc + it is stuttering a lot…)

Hello, from what I see, this is most likely what is causing the stutter issue here, since the position may be a bit behind where the NPC actually is, which makes it seem to teleport back. You can fix this though using an AlignOrientation, which only modifies rotation and not position. Here is an example of what you can do:

local root = script.Parent.PrimaryPart

script.Parent.Humanoid.AutoRotate = false

local attachment = Instance.new("Attachment", root)

local align = Instance.new("AlignOrientation")
align.Mode = Enum.OrientationAlignmentMode.OneAttachment
align.Attachment0 = attachment
align.PrimaryAxis = Vector3.new(0,1,0)
align.PrimaryAxisOnly = true
align.RigidityEnabled = true
align.Parent = root


while wait(.1) do
	align.CFrame = CFrame.new(script.Parent.HumanoidRootPart.Position, workspace.Part.Position)
end

Hopefully this helps, haven’t tested it so you might have to adjust it if necessary.

1 Like

Hi. The animation issue has been fixed, but 2 new issues are now appearing…

I have some knowledge about the first one. The npc is going to outdated waypoints, and this is why:

I’m pretty sure I know how to fix this issue, break and rerun the code whenever the cylinder(or target, doesn’t matter) switches positions. Now, the only problem is that I do not know how to execute that…

now the second issue is something I’m not sure what it is. After changing script 2 to what you sent me, the npc seems to fall on the ground and then get back up after some seconds…

Try turning RigidityEnabled on the AlignOrientation off, and tweak the MaxTorque/MaxAngularVelocity values, since it may be this causing the falling over.

1 Like

Yes, it worked now! I am gonna test out some stuff to find the fix to the outdated waypoints now, if you’ve got any ideas please share them :happy1:

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