Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

01local PathFindingService = game:GetService("PathfindingService")
02 
03local npc = script.Parent
04local Humanoid = npc:WaitForChild("Humanoid")
05local Torso = npc:WaitForChild("HumanoidRootPart")
06local Left = npc:WaitForChild("Left Leg")
07local Right = npc:WaitForChild("Right Leg")
08 
09local height = npc.Head.size.Y + npc.Torso.size.Y + npc["Left Leg"].size.Y
10local diameter = npc["Left Arm"].size.X + npc.Torso.size.X + npc["Right Arm"].size.X
11 
12local pathParams = {
13    ["AgentHeight"] = math.ceil(height) ,
14    ["AgentRadius"] = math.ceil(diameter/2),
15    ["AgentCanJump"] = true
View all 57 lines...

This is the script that detects when the NPC has stopped moving for more than 2 second

01local npc = script.Parent
02local root = npc.HumanoidRootPart
03 
04local max_stop_time = 2
05 
06local function check(pos)
07    local amount = max_stop_time / 0.1
08    while true do
09        amount -= 1
10        wait(0.1)
11        if root.Position ~= pos then
12            break
13 
14        elseif amount <= 0 and root.Position == pos then
15 
View all 32 lines...

Client code that makes all NPCS far away invisible

01local npcs = workspace:WaitForChild("npcs",5)
02 
03local camera = workspace.CurrentCamera
04 
05local maxdistance = 120
06 
07 
08 
09 
10 
11 
12camera:GetPropertyChangedSignal("CFrame"):Connect(function()
13 
14    if #workspace.npcs:GetChildren() > 0 and camera.CameraSubject and game.Players:GetPlayerFromCharacter(camera.CameraSubject.Parent) and camera.CameraSubject.Parent:FindFirstChild("Head") then
15 
View all 59 lines...

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.

0
One way I suppose is to 1. Don't use NPCs, 2. Following that, code your own pathfinding AI. radiant_Light203 1166 — 3y
0
Another way is to nest tables in tables. Maybe partsTable[section].Number; with each section containing a thousand parts. radiant_Light203 1166 — 3y

1 answer

Log in to vote
1
Answered by 3 years ago

I am not really good in Lua But I know this guy "TheDevKing" he had a "Advanced Roblox Scripting Tutorial" For pathfinding Here [TheTutorial] (http://www.youtube.com/watch?v=VWKNtqjPKn0) Note: I started learning Lua just about 2 weeks ago So... Ya

Please don't judge me if I couldn't help

I wish Helped

If the link above didn't work here : https://www.youtube.com/watch?v=VWKNtqjPKn0 :D

Ad

Answer this question