How to make AI?

I need npc that can opponent player in 1v1 fight, so add some strategy to it, for example, in world of warship, there are unfiled places filed by bots, but you can’t exactly say if he is boot or not so pro player. Can you give me some ideas for algorithms?


You can refer to this page found on roblox’s old wiki to aid in creating AIs, as well as this devforum post. I haven’t created AIs before, but these could probably help. There is one pathfinding algorithm called A*. You could research it, as well as other algorithms.


Ok, but I think near player quality skills, not just folowing nearest player.

You’re going to need to find a way to program that if you want them to attack like players. Still follow the other tutorials and then get a base down, then build off of that to improve on it and make them attack more like a player would. (Again, more custom scripting)

Ok, but this is my question. I am asking how to for example detect if the player is atackeble, or if its sure death for the bot, because the player have too big level, or there are more players (the players are leveled), or he can detect if there is someone else in combat and he can help him, or what is harder, he can detect some bad situations and find some strategy, for example, when there is plane going to atact someone, he will send his planes (if they can get him, before the other plane land), or he can detect if it’s worth it to go directly to target, or to go around shoot range of some enemy.

Ik, how to define it situation per situation, but how to make it think, that ik this and this, so I will do this.

1 Like

This sounds like you’re trying to make some type of AI for an npc.

1 Like

Yes, I need idea how to make, that I don’t have to define every situation and the bot can detect combination of situations, that he knows and can find some solution for the situation.

1 Like

To do that you’re going to have to find a way to make a neural network, which isn’t easy and requires a lot of work. You would probably be better off just programming for scenarios if you’re just starting out.


and how does this work, because when I have 10 situation modified by 10 aspect and with 10 gamestyles, I need to make 1k functions and I don’t want to make 1k functions.

1 Like

A neural network works by training AI in stages to do things until it properly accomplishes a task. You would have to go and run through every single scenario thousands, if not millions of times for it to get the correct way to respond to something happening.


ok, but this can be done in game by players, scripting every situation must be done by me.

If you successfully create a neural network AI for this, and put it into your game, it would probably take a few months of players playing it, just to make the AI learn one scenario.

This isn’t accounting for all the storage involved.

If you have a problem with how many functions you’ll need, lower your starting goal.
It’s not smart to start with a massive goal; you’re not going to be able to code this all without updates; especially without making the possibility of breaking it 60% or more.

And to add on to that, AI is not a smart way to program situations. Hardcoding a situation results in you controlling the experience, no one has made an AI that decides on player situations and has had it succeed, because it’s impossible to get good results with current technology. And any common AIs need hundreds to millions of different scenarios in the first place to be trained.
Edit: Removed rude comment, my apologies.

I need to make pvp bot, so when it don’t know every scenario (that is impossible), its useless, because players will find what it can’t do and make the attact on it in this situation. So I need some AI for fighting in all scenarios.

What i’m suggesting here isn’t perfect by all means, but here’s a few ideas that might help you out. This is assuming that you will create the AI from scratch, aside from using Roblox’s built-in pathfinding service.

Since you seem to be worried about players taking advantage of weaknesses in the AI, you could design your AI to be unpredictable, by giving it a small handful of possible moves it can make against the player. Even if the player knew all the possible moves of the bot, if you made it so that the bot randomly selects moves it would be much more difficult for the player to take advantage of it.

I’m not sure what combat abilities that the player and bot has in your case, but you may need to design your AI differently depending on it. For example, if the player and bot uses swords, you could have your bot only make a move when the player moves within a certain vicinity of it. And from this you could have a variety of possible moves for your AI when this occurs. Your bot could circle around the player then approach, or have it jump over the player first and then attack. It will have a chance and no absolute guarantee of performing these moves so that it is always unpredictable for the player.

If your player and bot uses guns, you could program your bot to hide behind cover, strafe around the player, or even jump around continuously to avoid getting hit. In all these cases, the bot will always keep track of the player’s exact position as well as minding the map that it’s in.

Finally, regarding the map. If the map the player and bot are battling on has obstacles, you will have to incorporate pathfinding into the bot. In this case, you could have your bot find it’s way to the player, and when they are within line of sight your bot could change it’s behavior so that they could battle the player. If your player and bot are battling on a flat arena, you don’t have to use pathfinding but you might have to account for the borders of the arena so that the bot doesn’t get stuck or cornered.

The AI I’m suggesting here will not handle every possible situation out there, But I hope this helps regardless.


Ok, but my game isnt only pvp, but combination of pvp and strategy

English language doesnt determine someones ability to think. That’s not only rude, but its also ignorant. Be a nice human. Your ai has bugs. Bugs can be fixed once you find the root of the problem.

That said, OP, you should start with a base. And slowly work your way through adding more and more.

Make a checklist and imagine how you as a person would think and branch off to what would happen if said state worked or if it didnt and continue branching until you’re happy with their intelligence.


I’d say start with a function that tells the NPC when to follow and attack the player, something like this:

local maxDistance = 20 -- change this to whatever number works best for you

function followPlayer(player, npc)
    local magnitude = (character.PrimaryPart.Position - npc.PrimaryPart.Position).Magnitude
    if magnitude maxDistance < then

function attackPlayer(player, npc)
    local magnitude = (character.PrimaryPart.Position - npc.PrimaryPart.Position).Magnitude
    if magnitude < 5 then
        attack(player, npc) -- you'll need to make your own function here for NPC attack animation or whatever else you plan to use here

You’ll also need to define which player the NPC is fighting with, which can be easily done when the NPC is spawned or you may use Magnitude again to find the nearest player:

function findNearestPlayer(npc)
    local nearestPos = maxDistance
    local nearestPlayer = nil
    for _, player in pairs(game.Players:GetPlayers()) do
        local primary = player.Character.PrimaryPart
        local magnitude = (primary.Position - npc.PrimaryPart.Position).Magnitude
        if magnitude < nearestPos then
            nearestPos = magnitude
            nearestPlayer = player.Character

If there’s anything else you don’t understand (or I misunderstood) please let me know


I am making npc, that will control ship (ok, i know, that it isnt clear from the topic)