so i’m trying to add npcs on my game, all going well till i decided to playtest it ingame and realized the ping was extremely high, sometimes going over 400ms and the most stable ping i’ve got with the npcs was 200ms. if anyone’s wondering i only have 27 npcs and yes i use 1 script to manage all npcs.
this is my ai code:
local module = {}
local baseai = require(script.Parent)
function walkRandomly(npc)
local root = npc.HumanoidRootPart
local hum = npc.Humanoid
local xRand = math.random(-50,50)
local zRand = math.random(-50,50)
local goal = root.Position + Vector3.new(xRand, 0, zRand)
hum:MoveTo(goal)
end
function track(npc, closest_target)
if npc.Humanoid.Health < 1 then npc.Humanoid:MoveTo(npc.HumanoidRootPart.Position);return end
task.spawn(function()
while true do
if npc.target_value.Value ~= closest_target then
npc.Humanoid:MoveTo(npc.HumanoidRootPart.Position)
break
end
local params = RaycastParams.new()
params.FilterDescendantsInstances = {npc, closest_target}
params.FilterType = Enum.RaycastFilterType.Exclude
local origin = npc.HumanoidRootPart.Position
local direction = Vector3.new(0,0,-3)
local ray = workspace:Raycast(origin, direction, params)
if ray then
npc.Humanoid.Jump = true
walkRandomly(npc)
else
npc.Humanoid:MoveTo(closest_target.HumanoidRootPart.Position)
end
task.wait(0.6)
end
end)
end
function module:init(npc)
task.wait(1)
if npc:FindFirstChild("test_NPC_TAG") then return end
for _, v in pairs(npc:GetChildren()) do
if v:IsA("BasePart") then
v:SetNetworkOwner(nil)
end
end
npc.HumanoidRootPart:SetNetworkOwner(nil)
local tag = Instance.new("NumberValue")
tag.Parent = npc
tag.Name = "test_NPC_TAG"
local targetval = Instance.new("ObjectValue")
targetval.Parent = npc
targetval.Name = "target_value"
targetval.Value = nil
local con
local con3
local con2
local con4
con3 = targetval.Changed:Connect(function()
if npc.Humanoid.Health < 1 then
if con ~= nil then con:Disconnect() end
con2:Disconnect()
con3:Disconnect()
targetval.Value = nil
npc.Humanoid:MoveTo(npc.HumanoidRootPart.Position)
return
end
if con ~= nil then
con:Disconnect()
end
track(npc, targetval.Value)
con = targetval.Value.Humanoid.Died:Connect(function()
con:Disconnect()
targetval.Value = nil
npc.Humanoid:MoveTo(npc.HumanoidRootPart.Position)
end)
end)
con2 = npc.Humanoid.Died:Connect(function()
if con ~= nil then con:Disconnect() end
con2:Disconnect()
con3:Disconnect()
targetval.Value = nil
npc.Humanoid:MoveTo(npc.HumanoidRootPart.Position)
end)
task.spawn(function()
while true do
if npc.Humanoid.Health < 1 then
if con ~= nil then con:Disconnect() end
con2:Disconnect()
con3:Disconnect()
targetval.Value = nil
npc.Humanoid:MoveTo(npc.HumanoidRootPart.Position)
break
end
npc.HumanoidRootPart:SetNetworkOwner(nil)
local exclusions = {}
if npc:FindFirstChild("exclusion") then
table.insert(exclusions, npc.exclusion.Value)
end
local closest_target = baseai:GetClosestTarget(npc, exclusions)
if closest_target ~= nil then
if not closest_target:FindFirstChild("exclusion") then
if (closest_target.HumanoidRootPart.Position - npc.HumanoidRootPart.Position).Magnitude < 70 then
npc:FindFirstChildWhichIsA("Tool"):Activate()
end
if npc:FindFirstChild("maximumdistance") then
if (closest_target.HumanoidRootPart.Position - npc.HumanoidRootPart.Position).Magnitude < npc.maximumdistance.Value then
targetval.Value = closest_target
end
else
targetval.Value = closest_target
end
end
end
task.wait(1)
end
end)
end
return module
and this is my npc manager script that i mentioned earlier:
local baseai = game.ReplicatedStorage.BaseAI
local npcs = workspace.NPCS
local function handle(npc)
task.spawn(function()
task.wait(1)
local tag_find = npc:FindFirstChild("ai_tag")
if tag_find then
local module_find = baseai:FindFirstChild(tag_find.Value)
if module_find then
task.spawn(function() require(module_find):init(npc) end)
end
end
local weapon_find = npc:FindFirstChild("weapon_tag")
if weapon_find then
if game.ReplicatedStorage.tools:FindFirstChild(weapon_find.Value) then
game.ReplicatedStorage.tools:FindFirstChild(weapon_find.Value):Clone().Parent = npc
end
end
npc.IsRagdoll.Changed:Connect(function(val)
if val == true then
npc.Humanoid.PlatformStand = true
else
npc.Humanoid.PlatformStand = false
end
end)
end)
end
npcs.ChildAdded:Connect(function(child)
handle(child)
end)
for _, v in pairs(npcs:GetChildren()) do
handle(v)
end