How to make AI?

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.

2 Likes

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.

2 Likes

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.

4 Likes

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.

7 Likes

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
        npc.Humanoid:MoveTo(player.PrimaryPart.Position)
    end
end

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
    end
end

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
        end
    end
    npc.Humanoid:MoveTo(nearestPlayer.PrimaryPart.Position)
end

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

3 Likes

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

What ship? I am getting very confused…

I am making game about fighting with ships, and I need to make bot, that can control ship like this (with some strategy)

If you would like to incorporate strategy and planning into the AI, there are many methods. All of them get more difficult if the AI have to work together.

I just finished CS-6600 (Intelligent systems) in which I made a sword fighting AI here on Roblox. The method, Q-Learning, is applicable for your AIs as well. If you have maps that make some positions more favorable then you may want to augment the Q-Learner with a neural network to help predict state values. The reasons why are explained in a report I put together. Here is a video of the AI training:

Here is the repo with all the source code:

Here is the class report (put together in 30 minutes, don’t tell my professor!) about what issues I ran into and what I learned:
https://drive.google.com/file/d/1MbCnC22bmX5uQZtBNHx1BKECHM_Sygu0/view?usp=sharing
And here is an article I found useful to learn about Q-learning:

Lastly, here is the place file:
AI.rbxl (80.5 KB)

To summarize, Q-Learning learns the best “policy”, for a given state (at a specific location on a map, enemy’s position, reload times, fuel, health, ext) what is the best action to take? It learns the best actions by trial and error over time. This AI was trained with a fairly small number of states so learns really fast. Other AIs may require more training or even a neural network to estimate state values. The long-term planning comes in via the propagation of rewards back along the states preceding it.

26 Likes

ok, but i am making strategy ai

1 Like

You don’t have to actually dive down deep to make strategic AI’s. You can just chain a bunch of if statements.

Like, when the player level is above, for example 20, make the npc flee. You should incorporate some randomness in your npcs. For example, some npcs may flee if the player level is above 20, but for some, level 25, etc. You could also make a list of npc actions when a player does something, then pick a random action.

Also, add in some pseudo-AI strategy to your npc. For example, when the npc is low on health, you can make it flee, or release a shield, etc.

I use this method in most of my games. It works only if your game is not to complicated. If it is, you have to use some of the methods that the other posters above suggested. I’m not a expert in AI, but pseudo-AI works as an alternate solution. (Heck, I even made an unbeatable tic-tac-toe AI with this method)

Nice assignment! Wish my profs allowed Roblox submissions… let alone be able to say the word in public without everyone cringing…
Anyway, Q-learning is a rather interesting method of machine learning but a bit shallow and hard to apply to many situations. In your report, you mentioned how you’d like to try the same thing but with standard artificial neural networks; a much more universal and well known form of machine learning.
For this, you can find a use for my neural library. Comes with all the methods you’ll need for feedthrough/recurrent networks along with some UI elements.
Would be very interesting to see how your sword fighting AI will perform with a complex network.

Also, one question: Why are you teaching the bot to fight itself? Wouldn’t this result in the bot learning how to fight only another bot? Its fast but this may result in the bot not working well or at all when versed against a player. Saying ‘may’ because with networks, all bets are off.

20 Likes

This project was unfortunately thrown together real fast with a focus on making a presentable product while I focused on other projects / finals. In the future I think it would be interesting to add deep-Q learning, a fair player controller, and allow the AI train on other players. As is it doesn’t even have a method to save the simple Q-table :stuck_out_tongue:

Very cool library. It’s unfortunate that we can’t use matrix libraries written in C like Python or Lua machine learning packages do. I think if I do end up doing a lot of AI work on Roblox, I’d probably use a web API. Now that I think about it, the communication from the Roblox server to an AI server would be like a very low latency from the Roblox server to a player.

3 Likes