AI Optimization issue

  1. Hello, I’m trying to make a system that will spawn a lot of enemies.
    But I dont have any experience in optimizing so I made a simple rig with AI inside of it (AI contains while loop to find a target and chase it). But if spawning a lot of them and game gets really high ping (above 1000 probably). In some games I saw same but with optimized ping.
    Anyone can give tips how to make new AI or something that will reduce ping?

ai example:

local hum = script.Parent:FindFirstChildOfClass("Humanoid")

local function tofind()
for i,v in pairs(workspace:GetChildren()) do
if v:FindFirstChildOfClass('Humanoid') and v:FindFirstChild('HumanoidRootPart') and v ~= script.Parent then
return v.HumanoidRootPart
end
end
end

while wait() do
if hum.Health <= 0 then break end
local enemy = tofind()
if enemy then
hum:MoveTo(enemy.Position)
end
end

I know a lot of people have optimized by replacing the old wait() function with the new task.wait(), I think some doubled the number of bots in the game by doing this

That’s the only optimization I know about, but many recommend it, which is why I’m posting this.

so basically,

while task.wait() do

end

You should probably do

while task.wait(0.2) do

end

Because most likely, 0.2 seconds is faster than the ai walking to the target.

Also, performance.

But I think there is a built in function for when a character moves, so maybe use that?

not trying to be rude, but I doubt that task.wait can change a lot. Im talking about massive amount of enemies at the same time, its the problem if huge ping, meanwhile in a game that have almost same thing as me doing, there’s no issue about massive amount of enemies increasing ping to 1000+

and yeah task.wait() is better than wait.

I think these posts can answer your question:

Posts:

Task.wait(n) vs wait(n)
Task Library - Now Available! - Updates / Announcements - DevForum | Roblox
Avoiding wait() and why - Resources / Community Tutorials - DevForum | Roblox

Also, if you need character positions, try using Humanoid events, such as:

Is it a server script ?

This text will be blurred

yeah it is since its inside of a npc

1 Like

I dont need character positions, since its NPC’s AI

If you are getting a HumanoidRootPart, I am betting there is a Humanoid as well.

Never tried using it myself, but maybe CollectionService might be helpful here? In essence, you use it to control a lot of different objects using a single script. Here’s a devforum post explaining how to use it.

Use ParallelLua. Great for repetitive tasks, like 1000s of enemies doing the same thing.

Consider changing it to a LocalScript.

If it doesn’t work, I uh… don’t know what to say.

uhh, could you show an example?

ahh, so basically its a service that contains objects values in it / or a global table (_G.enemies

Using task.spawn, and coroutines can, to my knowledge, do this. Also I believe using events, such as the Humanoid events I had given above, run async.

Do you want to the players see the same thing at the same time ?

I dont want to make everything even harder for myself, so ill just stand away from local scripts

1 Like

From my knowledge, you shouldn’t use global variables.

And what do you mean by this:

actually lemme try to use collection service