NextPath
Hello guys, it’s me again. For a while now, I’ve searched the toolbox to find the perfect NPC script or Nextbot script, but they all have always had problems. Either they use Humanoid:MoveTo()
to move to a player (which lets the NPC easily get stuck on walls), or use Roblox pathfinding
, which isn’t the greatest. I decided to write my own script for NPCs, but specifically for Nextbots. NextPath offers high customization options, while still being easy to manage, and is practically drag-and-drop. You can find the script on the toolbox, or alternatively, you can get an Example Nextbot here. First, I’ll go over configuration.
Settings
You will find these settings at the top of the script titled NextPathHandler
.
local ThinkingEnabled = true -- If the NPC should think
local Rate = 0.1 -- Rate of ticking NPC
local MaxDistance = 500 -- Max distance NPC can search in
local VisibilityCheck = false -- Determines if NPC should check for visibility
local RunDistance = 20 -- Distance before NPC switches to Humanoid:MoveTo()
local MaxJumpTicks = 20 -- How many ticks the NPC should wait before jumping (if the target is above it)
local VerticalJumpDistance = 20 -- The distance (in studs) that a target must be above an NPC for it to try to jump
local CanJump = true -- If the Nextbot should be able to try and jump if a player is out of their reach
local Damage = 1000 -- Set to 0 to disable
local TimeBeforeAmbientSound = 30 -- If you have an ambient sound, this is how many ticks it takes to play it
And the script is readable and has comments placed over important parts of it, for those who want to look at it but don’t feel like having to process what each chunk of code does.
The NPC can be customized to see through walls, have ambient, looped, and kill sounds, it will jump to try and reach players if they are a customizable distance above them, and it uses a combination of 2 different approaches for moving towards players.
Behavior Rundown
The NPC will first sort through players to see who it wants to target. It will send a raycast to each player (granted you have the visibility check set to true, if it is set to false, it will automatically queue them) to see if realistically the NPC could see them from where it’s standing. It then queues the players and checks their distance to see who is the closest person to the NPC, which will be set to their target.
When the NPC has a target, it will have 2 modes that it can go into, Pathfinding and Dashing. When the NPC is outside of the Run Distance, it will use SimplePath Pathfinding (thank you @GrayzcaIe for making this, I hate dealing with pathfinding. Also go check out that resource, its really good.) to look for them, which means unlike most NPC scripts that just make the NPC move towards its target, it won’t get stuck on walls very often, as it can just pathfind around them. Then, when the NPC gets within Run Distance, it will switch to using Human oid:MoveTo()
to run towards them, meaning it will have a clear path to get to you, and won’t make odd movements. The NPC can also damage players, but this can be disabled if chosen to.
Demonstration
Enough talk, it would be very useful for people stumbling upon this forum post to see some examples, since some people, including me, aren’t a big fan of having to use something in studio just to see what it does. Here are some videos below demonstrating what the script can do:
Nextbot chasing me
This showcases the general behavior of the Nextbot
Nextbot jumping showcase
This demonstrates some of the script which in the case of Roblox pathfinding not being able to detect you, the bot automatically switches to Run Mode.
And thats all for now. This script isn’t performance tested, so I have no clue what any of the stats for it are, but I whipped this up over the course of about a week (off and on because of school), because I wanted to share it, and I really needed a good enough AI script for my games.
No credit is required, and I will never require you to credit me for using any of my resources. I make these resources for other developers to use, so feel free to use it in any projects for any reason!