AI pathfinding is lagging

Hello, I have an issue with my ai pathfinding script. I used GnomeCode’s AI pathfinding script. It works well when i run the game on the server, but when i play the game on client then i can see that my dummy lags everytime the function patrol() is called. Any ideas on how to fix it are welcome. Thanks!

Here is the script:

script.Parent.HumanoidRootPart.HiddenExausted4:Play()
script.Parent.HumanoidRootPart["SWELL 22"]:Play()
script.Parent.HumanoidRootPart.heartbeat:Play()

local teddy = script.Parent
local humanoid = teddy.Humanoid

local PathfindingService = game:GetService("PathfindingService")
teddy.PrimaryPart:SetNetworkOwner(nil)

local function canSeeTarget(target)
	local origin = teddy.HumanoidRootPart.Position
	local direction = (target.HumanoidRootPart.Position - teddy.HumanoidRootPart.Position).unit * 40
	local ray = Ray.new(origin, direction)

	local hit, pos = workspace:FindPartOnRay(ray,teddy)

	if hit then
		if hit:IsDescendantOf(target) then
			return true
		end
	else
		return false
	end
end

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

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

			if distance < maxDistance and canSeeTarget(target) then
				nearestTarget = target
				maxDistance = distance
			end
		end
	end

	return nearestTarget
end

local function getPath(destination)
	local pathParams = {
		["AgentHeight"] = 6,
		["AgentRadius"] = 3,
		["AgentCanJump"] = false
	}
	local path = PathfindingService:CreatePath(pathParams)

	path:ComputeAsync(teddy.HumanoidRootPart.Position, destination.Position)

	return path
end

local function attack(target)
	if target and target:FindFirstChild("HumanoidRootPart") then
		local distance = (teddy.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude

		if distance > 8 then
			humanoid:MoveTo(target.HumanoidRootPart.Position)
		else
			target.Humanoid.Health = 0
		end
	end
end

local function walkTo(destination)
	local path = getPath(destination)

	if path.Status == Enum.PathStatus.Success then
		for index, waypoint in pairs(path:GetWaypoints()) do
			local target = findTarget()
			if target and target:FindFirstChild("HumanoidRootPart") and target.Humanoid.Health > 0 then
				print("TARGET FOUND", target.Name)
				game.SoundService["Walking Bathtub Leg Movement 16 (SFX)"]:Play()
				teddy.Humanoid.WalkSpeed = 16
				attack(target)
				break
			else
				
				humanoid:MoveTo(waypoint.Position)
				humanoid.MoveToFinished:Wait()
				teddy.Humanoid.WalkSpeed = 8
			end
		end
	else
		humanoid:MoveTo(destination.Position - (teddy.HumanoidRootPart.CFrame.LookVector * 10))
	end
end

local function patrol()
	local waypoints = workspace.waypoints:GetChildren()
	local randomNum = math.random(1, #waypoints)
	walkTo(waypoints[randomNum])
end

while wait(0.25) do
	patrol()
end

I was wondering if it would work if I made two different loops, one loop will check for player every 0.25 seconds, and the other loop will calculate the path and make the dummy go to it, this loop will trigger the Dummy moving function only when it’s current path is completed. But i really do not know how to put it together as i am not really that good in advanced scripting.

1 Like