Miller elegantly demystifies complex AI logic by grounding it in fundamental software design patterns, making high-level engineering accessible to everyday creators. It is a perfect bridge for developers moving from basic scripting to professional system architecture.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
How to make guard NPCs using a state machineAdded:
So, let's say you want to create exciting NPCs to fill your game world, but dumping all your logic into one big file leads to chaotic code you'll never be able to debug.
If you want NPCs that you can customize quickly, reuse all over your world, and debug easily, you're going to want a state machine.
Hi, I'm Miller, a software engineer here at Roblox. Today, we're going to learn how to use a state machine library to make a guard NPC that patrols and gets alerted if you come too close. All the code I'm going to show today, plus the place file, you can download and follow along if you want to.
So, let's have a look at the world we'll be working in today.
>> [music] >> As you can see, I've already gone ahead and set up a castle, as well as a simple NPC. This guy just has a humanoid, nothing else, and no other code telling him what to do. I've also set up these four flags that we use as waypoints for the guard's patrol and tag them. And lastly, I've put a sword in the middle on a golden pedestal that we can try to grab before the guards attack us. So, let's get started by having a look at our code. Here, I've got our types file.
We're going to check out the different kind of tables we're going to be using in our state machine. We're going to have a context table, which is going to be a [music] global blackboard for all the information our NPCs need. We're going to have state tables, which are the possible configurations of each NPC.
Test functions, which are run by transitions to connect the states, and finally, an actual state machine, [music] which is all the transitions we have, our starting context, our starting state, and we give it a name. Now, this right here is the meat and potatoes of the state machine. This is the tick function. This takes in one of those state machine tables we just saw, and it's going to run the tick function, checking each transition coming out of the active state to see if we should take it or not.
>> [music] >> If we take a transition, we're going to run an on exit and an on enter function on the state we're leaving and entering, respectively. And if we don't transition, we're going to run an on stay function. That's really all the code that actually matters for using the state machine. I've also provided a composition test thing that lets you combine all multiple test functions [music] to create a combination, like a boolean and, or, or not. And then also, a single module that you can access all of them from called simple state machine. All right, now we're going to create an NPC that I'll call the walker.
This NPC is going to walk between flags one and two. So, here we've got the code for the walker. Uh if you have a look here at the top, we've got the types, which we've just imported from simple state machine. I've set up a transition test, and then what I've also done is set up the actual states. So, if we have a look at walk to flag one, now when the NPC is in this state, it's just going to make them walk towards flag one. We've got a second state called [music] walk from to flag two. It's going to make them walk towards flag two. Now, let's talk about what this state does. What happens is when the NPC enters the state, we're going to set context.targetflag to flag one. Context, that's a global blackboard. We can keep information about the NPC in there. Then, in the on stay function, which is run every tick, we're going to have the humanoid set its move to to the flag one's pivot position. Walk to flag two is going to be the exact same thing, just replacing flag one with flag two. Coming back to our original file, you can see I've set up transitions. If we're in walk to flag one, we check if we're near the target flag. That's flag one. If we are, we go to flag two, and I made a transition to do the opposite. I just load those things into the state machine, and I set up some context information. The script's going to need to know what our character is, what our humanoid is, and also which flag we should start going to. We're going to start by going to flag two. And then all I've got to do is set heartbeat to run the tick function I showed you earlier. Now, all I've got to do is drag our walker script to the guard, and we'll see what happens.
As you can see, the guard's walking to flag two, and when he reaches there, he switches back to the first state, walk to flag one, and he goes back and forth.
So, that's a start, but we can do better. You might have noticed in the last one that walk to flag Now, let's try that again. You might have noticed for the walker that the states walk to flag one and walk to flag two [music] are pretty similar. So, the first thing I'm going to do is create a more generic called walk to random flag.
What this [music] state is going to do is it's going to pick the next flag to go to randomly. So, instead of just going between one and two, >> [music] >> we're going to go to one of the two flags on an adjacent corner. So, the walk to random flag, when we enter it, is going to have us set the next flag.
So, we'll figure out which flag we should go to randomly. And then what we'll do is in the on stay, we'll do the exact same thing we saw earlier with humanoid move to to have the character walk towards the next flag. [music] When you take that logic and you add on to it a simple idle state that has the character hang around for a second after reaching a flag, we end up with the patroller. The patroller's going to have a lot of the same code, but we're going to switch around the transitions. We're going to go [music] from idling, waiting around for a second, to walking to the next random flag. We're going to have two tests for this one. We're going to have is near target flag, which checks if we're close enough to the flag, and we're also going to have is idle timer done, which tells us if we've been idling for long enough that it's time to go back to patrolling.
Again, we're going to take all that and put it into a state machine. We're going to add some new values to our context.
Now, we're going to have an idle start time, and we're going to have a current flag number, so we can track all four flags. Connect it to heartbeat, and we'll watch it go.
As you can see, our NPC now chooses flags randomly, letting it explore the entire castle and keep it protected from all sides.
That said, although this guard does patrol between all the flags, it doesn't really protect it. So, the next thing we're going to add is new states for the guard to attack us if we get too close to prevent us from stealing the sword.
So, let's soup up our guard with the ability to attack. The first thing I'm going to do is [music] We're going to add in this attack state.
What this attack state does is while we're in the state, we're going to record the last player we've seen, and we're going to chase them down. And when we get close by, we're going to attack them on a cool down. Having a look at the final version of our NPC, which I'm calling the defender, you can see that we've added a new transition test, is player nearby. Just like before, this takes in the context, and we can use it to determine if a transition should occur. This test checks to see if a player is within a range of the NPC. And if it is, it sets our context.targetplayer to that value and returns true, letting us know that it's time to attack.
If we come down and have a look at our transitions, you'll see that I'm leveraging a feature of the state machine. If you see the first transition there, I'm providing two different start states, letting us essentially have two arrows pointing at the attack state. If you have a look at the transitions table, I've added a new transition from idle or walk to random flag to our attack state, and that's using our is player nearby test. Coming down to look at our defender state machine, the only thing that's changed [music] is that I've added in a target player value, which is set to nil until the transition is player nearby finds a player to attack. Now that the defender is ready, let's see how the guard acts when we add that script to it.
Let's try getting close to the guard and see what happens.
Now, let's see how state machine NPCs can make a scene really exciting by adding tons of guards. All right, I've got nine guards walking around the palace. Let's see if I can get in, get the sword, and get out without dying.
Okay, I've already aggroed a couple of them. This is not good. Okay, let me try that again.
All right, sneaking past while they're idle.
I'm in.
Oh, shoot. Got the sword. Take one down.
Took two down. Oh, there's a lot more.
I got them all.
Victory! So, there you have it. A guard that patrols, alerts, and battles you when you get too close. Next time you're making an NPC, feel free to try out the state machine library, or design your own. You'll be surprised how active and engaging your NPCs can be with just a few states and transitions. That's all from me. I hope you learned something new.
Related Videos
OpenHuman VS Hermes AI: Who Wins?
JulianGoldieSEO
285 views•2026-05-29
Long-Running Agents — Build an Agent That Never Forgets with Google ADK
suryakunju
142 views•2026-05-30
This computer is made from real human brain cells. And you can buy it.
Talktmsmedia
3K views•2026-05-28
BREAKING: Microsoft’s New Image Generating Model Beat Out GPT 1.5 and Nano Banana 2
aimmediahouse
122 views•2026-06-03
I Made the Same Anime Fight Scene in Every AI Video Generator
NobleGooseAnime
295 views•2026-05-30
Nvidia Bets Big On AI PCs | New Chip To Power Windows Laptops | Technology | AI Updates | N18S
cnnnews18
3K views•2026-06-01
I Tested NEW Opus 4.8 on Four Projects (Updated LLM Leaderboard)
AICodingDaily
298 views•2026-05-29
3D Platformer Update - NO CAPES
SolarLune
294 views•2026-05-30











