Issue chasing AI. Freezes and stuttering, more common with mutiple at once

I’m trying to achieve an AI that chases you until you reach high ground or are out of it’s range. Once you are out of range, the AI should run back to it’s original position using pathfinding service.

The issue is after awhile the AI bugs out and isn’t responsive. It still works, it just goes extremely slow. This issue is somehow reduced when I set the network ownership to nil. The problem with doing that is the AI can unfairly kill the player because it is using the server for physics.

The issue also becomes more common when multiple AI chase at the same time.

local ai = script.Parent
local speed = 20
local radius = ai.Config.Range.Value
local elevatedSurfaceDistanceThreshold = ai.Config.ElevatedSurfaceHeight.Value
local originalPosition = ai.HumanoidRootPart.Position
local movingback = false

ai.Humanoid.WalkSpeed = ai.Config.Speed.Value

--[[local children = ai:GetChildren()
for i,v in pairs(children) do
	if v:IsA("Part") then
		v:SetNetworkOwner(nil)
	end
end]]
-- this is where i attempted changing the network ownership

local function getClosestPlayer()
	local closestDistance = math.huge
	local closestPlayer = nil
	for _, player in ipairs(game.Players:GetPlayers()) do
		local character = player.Character
		local humanoid = character and character:FindFirstChildOfClass("Humanoid")
		local distance = (character and character:FindFirstChild("HumanoidRootPart") and (character.HumanoidRootPart.Position - ai.HumanoidRootPart.Position).Magnitude) or math.huge
		if distance <= radius and distance < closestDistance and humanoid and humanoid.Health > 0 and character.HumanoidRootPart.Position.Y <= elevatedSurfaceDistanceThreshold then
			closestDistance = distance
			closestPlayer = player
		end
	end
	return closestPlayer
end

local computing = false
function MoveToPosition(position)
	if computing == true then return end
	computing = true
	--ai.Humanoid.WalkSpeed = ai.Config.Speed.Value*2
	local path = game:GetService("PathfindingService"):CreatePath()
	path:ComputeAsync(ai.HumanoidRootPart.Position, position)
	
	if Enum.PathStatus.Success then
		local waypoints = path:GetWaypoints()
		for i, waypoint in ipairs(waypoints) do
			ai.Humanoid:MoveTo(waypoint.Position)
			if getClosestPlayer() then
				computing = false
				ai.Humanoid.WalkSpeed = ai.Config.Speed.Value
				return
			end
			ai.Humanoid.MoveToFinished:Wait()
		end
		computing = false
		ai.Humanoid.WalkSpeed = ai.Config.Speed.Value
	else
		computing = false
		ai.Humanoid.WalkSpeed = ai.Config.Speed.Value
	end
end

local function chasePlayer(dt)
	
	local closestPlayer = getClosestPlayer()
	
	if closestPlayer then
		ai.Humanoid:MoveTo(closestPlayer.Character.HumanoidRootPart.Position)
	else
		MoveToPosition(originalPosition)
	end

end

while task.wait(0.3) do
	chasePlayer()
end

If there’s any ideas on why this is happening it would be really appreciated!

1 Like

This has already been answered 89245297345 times. Roblox humanoids are bad. If you need them, use them in small quantity or recreate your own. Reason why they stutter is a lot of the time due to them simply being bad, or you calling the ComputeAsync function while its already on a path.

I’ve seen humanoids be used in this way without issues. I am not looking to create my own because what I’m trying to achieve is pretty simple (around 5 AI chasing you at a time)

1 Like

The problem isn’t even the humanoids themselves lol, what are you talking about?

Make sure the AI’s root part network ownership is set to the server by doing this in the AI’s script: pathtoNPCModel.PrimaryPart:SetNetworkOwner(nil), this forces the server to simulate the physics, if you don’t set any network ownership it will make the player simulate the AI’s physics and not the server if close to the AI’s root part.

1 Like

Pretty sure this solved it! I attempted setting the entire npc to nil and the location was not accurate so sometimes the player would be cheated but i think just setting the root part helped fix that

1 Like

Wait the server automatically sets AI NetworkOwnership to nearby Players? Isnt it already controlled by the server?

Yeah, when NetworkOwnership is not set to nil, it will search for a player nearby for that player to simulate the physics.

1 Like

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