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.
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…