Broken humanoid with PathFinding service

Hello, i was making NPC for tropico 1 clone and i noticed problems.
I have made v1 of NPC and for first 1 minute, they look normal and walk normal, but after they start to lag hard and after couple of ticks they just stop.

There are 2 module scripts moving them, 2. one is rarely used, but 1. is used.

  1. moving script code
local module = {}


function module.Charmover(NPC,Object)
		local PathfindingService = game:GetService("PathfindingService")	
local zombie = NPC
local humanoid = NPC.Humanoid
local destination=nil
local path = PathfindingService:CreatePath()
local waypoints={}
	local currentWaypointIndex = 0
	local currentWaypointIndex2= 1
	NPC.Values.Timer.Value =((Object.Position-zombie.HumanoidRootPart.Position).magnitude)/4
	
function followPath(destinationObject)
	path:ComputeAsync(zombie.HumanoidRootPart.Position, destinationObject.Position)
	waypoints = {}
 
	if path.Status == Enum.PathStatus.Success then
		waypoints = path:GetWaypoints()
			currentWaypointIndex = 1
			humanoid:MoveTo(waypoints[currentWaypointIndex].Position)
	else
		humanoid:MoveTo(zombie.HumanoidRootPart.Position)
	end
end
	local function onWaypointReached(reached)
		if reached and currentWaypointIndex < #waypoints then
		currentWaypointIndex = currentWaypointIndex + 1
			humanoid:MoveTo(waypoints[currentWaypointIndex].Position)
	end
end
 
local function onPathBlocked(blockedWaypointIndex)
			followPath(Object)
	
end
 
path.Blocked:Connect(onPathBlocked)
 
	humanoid.MoveToFinished:Connect(onWaypointReached)
	
	followPath(Object)
	
	repeat wait(1)
		timer=NPC.Values.Timer.Value
		timer=timer-1
		if timer<1 then
			timer=0
			zombie:SetPrimaryPartCFrame(Object.CFrame+Vector3.new(0,1,0))
			humanoid:MoveTo(zombie.HumanoidRootPart.Position)
			humanoid.WalkToPoint=Vector3.new(0,0,0)
			warn("Route:RESET "..NPC:GetFullName())	
			forc=Instance.new("ForceField")
			forc.Parent=NPC
			game.Debris:AddItem(forc,10)
			break
		end
	until currentWaypointIndex >= #waypoints 
wait()
end 



return module

Its modifyed basic pathfinding service script.

NOTE:each npc has its own scripts to avoid crashing and data coruption
I used one before and it was mess…

Please help me somehow to stop them from lagging hard…

1 Like

You should show us a video on how the code runs.

2 Likes

Ouf… What video format does this fourm accept? If its MP4 then i cant make long videos

2 Likes

You upload videos the same way you upload photos, just don’t make the video too long, 10 seconds is fine for your example.

2 Likes

ok wait please, it may take while…

1 Like

this is them normal:
robloxapp-20200925-1715449.wmv (786.6 KB)
and this is when they get laged
robloxapp-20200925-1717282.wmv (1.5 MB)
oh no, wmv is not accepted, dont wory, no virus here…

At the end loop maybe try using

repeat

until (Boolean)

And also it seems like you generating force fields and not destroying any of them, which may cause lag.

2 Likes

Lets test something with that…

2 Likes

Force field is just visual detail to make me see witch NPC got timer timeouted (or stuck in wall)

2 Likes

Maybe try destroying the game:debris after you don’t need it

Problem 2 has appeard, module script refuses to change values

		timer=timer-1
elseif currentWaypointIndex >= #waypoints then
			finished=true

The error is inside that line of code, maybe let me see.
Added that line.

there are none, wait i will send


look left

I think its time to revrite this scripts and make new humanoids.

Hmm else if needs a space. And did you use local timer maybe or maybe I missed it.

timer is value insiede NPC-s value folder, elseif is one command, if you use else if then you need

if then
else if then
end 
end

That makes sense, I am sorta new to coding.

How did you get Programmer then?