Optimizing pathfinding code

Hi, I was wondering how I could optimize my pathfinding code. When I use attacks on the NPCs they become either really laggy or they don’t move at all. The script:

--Variables
local char = script.Parent
local hum = char:WaitForChild("Humanoid")

local isRunning = false
local currentWaypointIndex

local isRunningAnimPlaying = false
local isWalkingAnimPlaying = false

local walkSpeed = hum.WalkSpeed
local runSpeed = 25

--Animations
local WalkAnim = script:WaitForChild("Walk")
local RunAnim = script:WaitForChild("Run")

--Tracks
local WalkingTrack = hum:WaitForChild("Animator"):LoadAnimation(WalkAnim)
local RunningTrack = hum:WaitForChild("Animator"):LoadAnimation(RunAnim)

--Services
local PathfindingService = game:GetService("PathfindingService")

hum.Died:Connect(function()
	
	local killer = char:FindFirstChild("Killer")
	
	if killer and killer.Value ~= nil then
		
		local player = killer.Value
		
		player:WaitForChild("leaderstats"):WaitForChild("Cash").Value +=20
		
	end
	
	task.wait(2)
	
	char:Destroy()
	
	task.wait(5)
	
	local clone = game:GetService("ReplicatedStorage"):WaitForChild("Weak Bandit"):Clone()
	clone.Parent = workspace:WaitForChild("R15Dummies")
	
	
	
end)

hum:GetPropertyChangedSignal("Health"):Connect(function()
	
	if hum.Health == 100 then
		
		isRunning = false
		hum.WalkSpeed = walkSpeed
		
	else
		
		isRunning = true
		hum.WalkSpeed = 25

		task.wait(10)
		isRunning = false
		
	end
	
	
	
end)

local function StartPathfinding()
	
	local path = PathfindingService:CreatePath()
	path:ComputeAsync(script.Parent:WaitForChild("UpperTorso").Position, Vector3.new(math.random(0,100),3.7, math.random(0,100)))

	local waypoints = path:GetWaypoints()

	currentWaypointIndex = 1
	
	if isRunning then
		WalkingTrack:Stop()
		RunningTrack:Play()
		isRunningAnimPlaying = true
	else
		RunningTrack:Stop()
		WalkingTrack:Play()
		isRunningAnimPlaying = false
	end

	for i, waypoint in pairs(waypoints) do

		if i < #waypoints then
			if isRunning and isRunningAnimPlaying == false then
				
				RunningTrack:Play()
				WalkingTrack:Stop()
				isRunningAnimPlaying = true
				
			elseif not isRunning and isWalkingAnimPlaying == false then
				
				RunningTrack:Stop()
				WalkingTrack:Play()
				isWalkingAnimPlaying = true
				
			end

			script.Parent:WaitForChild("Humanoid"):MoveTo(waypoint.Position)
			hum.MoveToFinished:Wait(2)

		else
			
			RunningTrack:Stop()
			WalkingTrack:Stop()

			StartPathfinding()

		end

	end
	
end

while true do

	StartPathfinding()

end

Edit: This system is used to have some basic random movement, give player coins upon death, and the run away when dying

Use :SetNetworkOwner(nil) on the HumanoidRootPart to help get rid of the stuttering.

Sir, what are you doing? Why arent you using Humanoid.HealthChanged Sir?

Wrap this in a pcall, then check if the Computed Path was Successful.

1 Like

Hi! Thanks for replying. I have applied all of your changes although when I hit them they still don’t really move and they stutter etc

Edit: 2023-06-04_09-46-58

I have modified the provided script so that it will fix the issues you were having with it.

--Variables
local char = script.Parent
local hum = char:WaitForChild("Humanoid")

local isRunning = false
local currentWaypointIndex

local isRunningAnimPlaying = false
local isWalkingAnimPlaying = false

local walkSpeed = hum.WalkSpeed
local runSpeed = 25

--Animations
local WalkAnim = script:WaitForChild("Walk")
local RunAnim = script:WaitForChild("Run")

--Tracks
local WalkingTrack = hum:WaitForChild("Animator"):LoadAnimation(WalkAnim)
local RunningTrack = hum:WaitForChild("Animator"):LoadAnimation(RunAnim)

--Services
local PathfindingService = game:GetService("PathfindingService")

hum.Died:Connect(function()
	local killer = char:FindFirstChild("Killer")
	
	if killer and killer.Value ~= nil then
		local player = killer.Value
		
		player:WaitForChild("leaderstats"):WaitForChild("Cash").Value +=20
	end
	
	task.wait(2)
	
	char:Destroy()
	
	task.wait(5)
	
	local clone = game:GetService("ReplicatedStorage"):WaitForChild("Weak Bandit"):Clone()
	clone.Parent = workspace:WaitForChild("R15Dummies")
end)

local lastHealth = hum.Health

hum.HealthChanged:Connect(function(health)
	coroutine.wrap(function()
		if health < lastHealth then
			isRunning = true
			hum.WalkSpeed = runSpeed
			
			task.wait(10)
			
			isRunning = false
			hum.WalkSpeed = walkSpeed
		elseif health == hum.MaxHealth then
			isRunning = false
			hum.WalkSpeed = walkSpeed
		end
	end)()
	
	lastHealth = health
end)

local function StartPathfinding()
	if char.HumanoidRootPart:CanSetNetworkOwnership() then
		char.HumanoidRootPart:SetNetworkOwner(nil)
	end
	
	local path: Path = PathfindingService:CreatePath()
	
	pcall(function()
		path:ComputeAsync(char.HumanoidRootPart.Position, Vector3.new(math.random(0,100), 3.7, math.random(0,100)))
	end)
	
	if path.Status == Enum.PathStatus.Success then
		local waypoints = path:GetWaypoints()
		
		if isRunning then
			WalkingTrack:Stop()
			RunningTrack:Play()
			isRunningAnimPlaying = true
		else
			RunningTrack:Stop()
			WalkingTrack:Play()
			isRunningAnimPlaying = false
		end

		for i, waypoint in pairs(waypoints) do
			if i < #waypoints then
				if isRunning and isRunningAnimPlaying == false then
					RunningTrack:Play()
					WalkingTrack:Stop()
					isRunningAnimPlaying = true
				elseif not isRunning and isWalkingAnimPlaying == false then
					RunningTrack:Stop()
					WalkingTrack:Play()
					isWalkingAnimPlaying = true
				end
				
				hum:MoveTo(waypoint.Position)
				hum.MoveToFinished:Wait(2)
			else
				RunningTrack:Stop()
				WalkingTrack:Stop()
			end
		end
	end
end

while true do
	StartPathfinding()
end

Let me know if you are have any issues with this script!

1 Like

Hey! I really appreciate the help. This has worked although you can’t combo them because they just run away. Could you please modify the script so that they get stunned.

1 Like