How to make NPCS not cause lag but also decent movement using pathfinding?
Asked by
3 years ago Edited 3 years ago
I am currently working on a round based game that requires about 36 NPCS that randomly chooses from about 45000 different part to walk to with pathfinding, but their script causes lag, waits a little bit after walking to each waypoint, and also cause wait() to wait a lot more longer than it should. I've tried making one script that controls the NPCS globally and also cloning 2 scripts into each NPC(one for the movement and the other for restarting the movement script if the NPC have been stuck for more than 2 seconds) but it causes even more lag. I've also tried adding a script to the client that hides all NPCS far away from their character and show the ones that are close
This is the movement script
01 | local PathFindingService = game:GetService( "PathfindingService" ) |
03 | local npc = script.Parent |
04 | local Humanoid = npc:WaitForChild( "Humanoid" ) |
05 | local Torso = npc:WaitForChild( "HumanoidRootPart" ) |
06 | local Left = npc:WaitForChild( "Left Leg" ) |
07 | local Right = npc:WaitForChild( "Right Leg" ) |
09 | local height = npc.Head.size.Y + npc.Torso.size.Y + npc [ "Left Leg" ] .size.Y |
10 | local diameter = npc [ "Left Arm" ] .size.X + npc.Torso.size.X + npc [ "Right Arm" ] .size.X |
13 | [ "AgentHeight" ] = math.ceil(height) , |
14 | [ "AgentRadius" ] = math.ceil(diameter/ 2 ), |
15 | [ "AgentCanJump" ] = true |
24 | local path = PathFindingService:CreatePath(pathParams) |
28 | path:ComputeAsync(npc.Torso.Position, game.ReplicatedStorage.paths_folder.paths:GetChildren() [ math.random( 1 , #game.ReplicatedStorage.paths_folder.paths:GetChildren()) ] .Position) |
32 | local waypoints = path:GetWaypoints() |
36 | if path.Status = = Enum.PathStatus.Success then |
40 | for i = 1 , #waypoints do |
44 | if waypoints [ i ] .Action = = Enum.PathWaypointAction.Jump then |
46 | Humanoid:ChangeState(Enum.HumanoidStateType.Jumping) |
50 | Humanoid:MoveTo(waypoints [ i ] .Position) |
52 | Humanoid.MoveToFinished:Wait() |
This is the script that detects when the NPC has stopped moving for more than 2 second
01 | local npc = script.Parent |
02 | local root = npc.HumanoidRootPart |
04 | local max_stop_time = 2 |
06 | local function check(pos) |
07 | local amount = max_stop_time / 0.1 |
11 | if root.Position ~ = pos then |
14 | elseif amount < = 0 and root.Position = = pos then |
16 | if npc:FindFirstChild( "npc_ai" ) then |
17 | npc.npc_ai.Disabled = true |
19 | npc.npc_ai.Disabled = false |
Client code that makes all NPCS far away invisible
01 | local npcs = workspace:WaitForChild( "npcs" , 5 ) |
03 | local camera = workspace.CurrentCamera |
05 | local maxdistance = 120 |
12 | camera:GetPropertyChangedSignal( "CFrame" ):Connect( function () |
14 | if #workspace.npcs:GetChildren() > 0 and camera.CameraSubject and game.Players:GetPlayerFromCharacter(camera.CameraSubject.Parent) and camera.CameraSubject.Parent:FindFirstChild( "Head" ) then |
16 | for _,a in pairs (workspace.npcs:GetChildren()) do |
19 | if child:IsA( "Model" ) then |
21 | local humanoid = child:FindFirstChild( "Humanoid" ) |
22 | local rootP = child:FindFirstChild( "HumanoidRootPart" ) |
23 | local animate = child:FindFirstChild( "Animate" ) |
25 | if humanoid and rootP and animate then |
28 | if (rootP.Position - camera.CameraSubject.Parent.Head.Position).Magnitude > maxdistance then |
31 | for _,v in pairs (child:GetDescendants()) do |
32 | if v:IsA( "BasePart" ) or v:IsA( "Decal" ) then |
40 | for _,v in pairs (child:GetDescendants()) do |
41 | if (v:IsA( "BasePart" ) and v.Name ~ = "HumanoidRootPart" ) or (v:IsA( "Decal" )) then |
All I want is for the NPCS to move to random parts from a folder with pathfinding and not cause that much of a lag on the server
If I didn't make any sense please tell me, its my first post.