AI Optimization issue

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

Oh don’t worry you can turn the RunContext of a script and set it to Client !

Using a LocalScript opens the ai to exploits, and means it runs in a client, which doesn’t make sense for this use case.

1 Like

Basically it all boils down to how much code you are running at any given time. To really increase the amount of enemies you can have before the game slows down, you need to find a way to reduce the amount of resources each enemy takes up. So let’s break down what each enemy is doing.

Firstly you have this tofind function

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

This is slow. You are asking your code to run through every item in workspace. Doing this once or twice is ok. (though the fact it returns once it finds one is good) But you are doing this every time in the loop later. That means for every enemy in the game, they have to go through all the items in workspace. That leads to a ton of wasted time. To improve this, we need to reduce the search space.

You seem to be interested in finding players. A better solution for this is to just search for characters of players

for _, player in pairs(game.Players:GetChildren()) do
    if player.Character and player.Character.Parent ~= nil and player.Character.PrimaryPart then --Just make sure it's a valid character before proceeding
        return v.HumanoidRootPart
    end
end

It’s worth noting that the code as you presently have it will just find the player that comes first in the list, not the closest player like most games do. But that’s a bit aside from the optimization point.

Now for the next part

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

The only problem I really have with this part is that you are doing wait() with no parameter. The problem is this will wait the minimum amount of time. Is it really necessary to run your code at 30 times per second? (I think that’s about the default wait, but maybe it was 60) You can make your code run a lot less with barely any impact on how it works by telling it to wait for a quarter or a third of a second.

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

From how your code is, that’s about the best I think I can do. There are some things that are a bit more complicated to optimize, but those tend to be game specific like despawning enemies that don’t need to be active at the moment.

1 Like

30 im pretty sure,
and I got 1 idea how to optimize everything so let me take a try!

1 Like

No, you can use a script but only set the RunContext to Client, but I agree with you it opens the ai to exploits …