Creating AI help

Hello, I own an ant simulator. In this, there will be enemy insects which the players can fight (as an ant), I have never created AI though and I’d like to gain understanding of how it works and if any creators have recommendations for making it the best way. It will need to simply run around, and when a player gets near it, it will try to attack them, when it attacks it will play an animation and damage whoever it hits.

2 Likes

Try messing around with this Character Pathfinding | Documentation - Roblox Creator Hub

AI is not a simple topic, but you can make it as complicated as you want.
It requires a good fundamental understanding of code and as UneducatedScripter mentioned, Pathfinding is a great tool and will help you achieve most of the base movement.

Playing animations is easy, you just have to create one.
You can make a tool and have it have a part that damages whoever it touches (like the blade of a sword for example)

My suggestion for building the AI is to look at examples. A great video to start off is the following:

If you feel a bit more comfortable and have a good grasp in code you could try this:

3 Likes

Adding on to the posts above, regarding pathfinding, I made a module that could potentially help you make the process of scripting a pathfinding script easier. Furthermore, this module also includes some neat features. It will save you the trouble of making an entire pathfinding script.

How could I detect a target though? Like if a player is nearby them, how can I detect that and have them attack?

Well, first of all, you’ll need to make a function that returns nearest character. Then you can pathfind the NPC to the character. I guess then you could make them go a certain distance toward the character but not too close. Close enough so the weapon can be reached then attack.

Yes, i’m asking how I should do that.

You would get the vector magnitude between the AI and character. Then you can pathfind to it. If you use my module, you can do this using one line. You can have a loop which constantly pathfinds to the character until the AI reaches its goal (the player character), then the AI attacks. The whole thing repeats.

You could go through the list of players and calculate which one is nearest. You can do this by subtracting the distance between the AI Character and the players. Find which one is closest by using magnitude and simply use a pathfinder. I provided a sample below:

if (object.className == "Model") and (object ~= script.Parent) then -- If this child is a Model type and not the Zombie Model
				local targetRoot = object:findFirstChild("HumanoidRootPart")
				local human = object:findFirstChild("Humanoid")
				
				if (targetRoot ~= nil) and (human ~= nil) and (human.Health > 0) then -- If model has a RootPart, Humanoid, and above 0 HP...
					if (targetRoot.Position - NPC.HumanoidRootPart.Position).magnitude < range then
						nearest = targetRoot
						range = (targetRoot.Position - NPC.HumanoidRootPart.Position).magnitude
					end
				end
			end

Wouldn’t that be laggy? Is there an easier way to do this? It seems like looping something on that on server, especially with numerous npcs would lag.

Not really, computers are built to manage this type of thing. You can always test this too and see how it does. The AI I have built works just fine. Honestly, parts that move are moving probably take more effort from the computer, like a ball rolling on your game.

I believe a great example would be Drooling Zombies from the Toolbox. Look at their code.

1 Like