How to make guard NPCs using a state machine — Transcript
Full transcript
- 0:00So, let's say you want to create
- 0:01exciting NPCs to fill your game world,
- 0:04but dumping all your logic into one big
- 0:06file leads to chaotic code you'll never
- 0:08be able to debug.
- 0:09If you want NPCs that you can customize
- 0:11quickly, reuse all over your world, and
- 0:14debug easily, you're going to want a
- 0:15state machine.
- 0:17Hi, I'm Miller, a software engineer here
- 0:19at Roblox. Today, we're going to learn
- 0:21how to use a state machine library to
- 0:23make a guard NPC that patrols and gets
- 0:26alerted if you come too close. All the
- 0:28code I'm going to show today, plus the
- 0:30place file, you can download and follow
- 0:32along if you want to.
- 0:34So, let's have a look at the world we'll
- 0:35be working in today.
- 0:36>> [music]
- 0:37>> As you can see, I've already gone ahead
- 0:38and set up a castle, as well as a simple
- 0:40NPC. This guy just has a humanoid,
- 0:43nothing else, and no other code telling
- 0:45him what to do. I've also set up these
- 0:47four flags that we use as waypoints for
- 0:49the guard's patrol and tag them. And
- 0:52lastly, I've put a sword in the middle
- 0:53on a golden pedestal that we can try to
- 0:55grab before the guards attack us. So,
- 0:57let's get started by having a look at
- 0:59our code. Here, I've got our types file.
- 1:01We're going to check out the different
- 1:02kind of tables we're going to be using
- 1:04in our state machine. We're going to
- 1:05have a context table, which is going to
- 1:07be a [music] global blackboard for all
- 1:08the information our NPCs need. We're
- 1:10going to have state tables, which are
- 1:12the possible configurations of each NPC.
- 1:14Test functions, which are run by
- 1:16transitions to connect the states, and
- 1:18finally, an actual state machine,
- 1:19[music]
- 1:20which is all the transitions we have,
- 1:22our starting context, our starting
- 1:23state, and we give it a name. Now, this
- 1:25right here is the meat and potatoes of
- 1:27the state machine. This is the tick
- 1:28function. This takes in one of those
- 1:30state machine tables we just saw, and
- 1:32it's going to run the tick function,
- 1:33checking each transition coming out of
- 1:35the active state to see if we should
- 1:37take it or not.
- 1:38>> [music]
- 1:38>> If we take a transition, we're going to
- 1:39run an on exit and an on enter function
- 1:42on the state we're leaving and entering,
- 1:44respectively. And if we don't
- 1:45transition, we're going to run an on
- 1:47stay function. That's really all the
- 1:49code that actually matters for using the
- 1:50state machine. I've also provided a
- 1:52composition test thing that lets you
- 1:54combine all multiple test functions
- 1:56[music] to create a combination, like a
- 1:58boolean and, or, or not. And then also,
- 2:02a single module that you can access all
- 2:03of them from called simple state
- 2:05machine. All right, now we're going to
- 2:06create an NPC that I'll call the walker.
- 2:08This NPC is going to walk between flags
- 2:10one and two. So, here we've got the code
- 2:12for the walker. Uh if you have a look
- 2:14here at the top, we've got the types,
- 2:16which we've just imported from simple
- 2:17state machine. I've set up a transition
- 2:19test, and then what I've also done is
- 2:21set up the actual states. So, if we have
- 2:22a look at walk to flag one, now when the
- 2:25NPC is in this state, it's just going to
- 2:27make them walk towards flag one. We've
- 2:29got a second state called [music] walk
- 2:30from to flag two. It's going to make
- 2:32them walk towards flag two. Now, let's
- 2:33talk about what this state does. What
- 2:35happens is when the NPC enters the
- 2:38state, we're going to set
- 2:39context.targetflag
- 2:41to flag one. Context, that's a global
- 2:43blackboard. We can keep information
- 2:45about the NPC in there. Then, in the on
- 2:47stay function, which is run every tick,
- 2:50we're going to have the humanoid set its
- 2:52move to to the flag one's pivot
- 2:55position. Walk to flag two is going to
- 2:57be the exact same thing, just replacing
- 2:59flag one with flag two. Coming back to
- 3:01our original file, you can see I've set
- 3:03up transitions. If we're in walk to flag
- 3:05one, we check if we're near the target
- 3:07flag. That's flag one. If we are, we go
- 3:09to flag two, and I made a transition to
- 3:12do the opposite. I just load those
- 3:14things into the state machine, and I set
- 3:16up some context information. The
- 3:17script's going to need to know what our
- 3:18character is, what our humanoid is, and
- 3:21also which flag we should start going
- 3:23to. We're going to start by going to
- 3:24flag two. And then all I've got to do is
- 3:26set heartbeat to run the tick function I
- 3:28showed you earlier. Now, all I've got to
- 3:30do is drag our walker script to the
- 3:31guard, and we'll see what happens.
- 3:38As you can see, the guard's walking to
- 3:39flag two, and when he reaches there, he
- 3:41switches back to the first state, walk
- 3:43to flag one,
- 3:45and he goes back and forth.
- 3:48So, that's a start, but we can do
- 3:49better. You might have noticed in the
- 3:50last one that walk to flag Now, let's
- 3:52try that again. You might have noticed
- 3:54for the walker that the states walk to
- 3:55flag one and walk to flag two [music]
- 3:57are pretty similar. So, the first thing
- 3:59I'm going to do is create a more generic
- 4:01called walk to random flag.
- 4:04What this [music] state is going to do
- 4:05is it's going to pick the next flag to
- 4:07go to randomly. So, instead of just
- 4:08going between one and two,
- 4:10>> [music]
- 4:10>> we're going to go to one of the two
- 4:12flags on an adjacent corner. So, the
- 4:14walk to random flag, when we enter it,
- 4:16is going to have us set the next flag.
- 4:19So, we'll figure out which flag we
- 4:21should go to randomly. And then what
- 4:23we'll do is in the on stay, we'll do the
- 4:24exact same thing we saw earlier with
- 4:26humanoid move to to have the character
- 4:28walk towards the next flag. [music] When
- 4:30you take that logic and you add on to it
- 4:32a simple idle state that has the
- 4:34character hang around for a second after
- 4:36reaching a flag, we end up with the
- 4:38patroller. The patroller's going to have
- 4:41a lot of the same code, but we're going
- 4:42to switch around the transitions. We're
- 4:44going to go [music] from idling, waiting
- 4:46around for a second, to walking to the
- 4:48next random flag. We're going to have
- 4:50two tests for this one. We're going to
- 4:52have is near target flag, which checks
- 4:54if we're close enough to the flag, and
- 4:56we're also going to have is idle timer
- 4:58done, which tells us if we've been
- 5:00idling for long enough that it's time to
- 5:02go back to patrolling.
- 5:03Again, we're going to take all that and
- 5:05put it into a state machine. We're going
- 5:06to add some new values to our context.
- 5:09Now, we're going to have an idle start
- 5:11time, and we're going to have a current
- 5:13flag number, so we can track all four
- 5:15flags. Connect it to heartbeat, and
- 5:17we'll watch it go.
- 5:30As you can see, our NPC now chooses
- 5:32flags randomly, letting it explore the
- 5:34entire castle and keep it protected from
- 5:36all sides.
- 5:38That said, although this guard does
- 5:40patrol between all the flags, it doesn't
- 5:42really protect it. So, the next thing
- 5:44we're going to add is new states for the
- 5:46guard to attack us if we get too close
- 5:48to prevent us from stealing the sword.
- 5:50So, let's soup up our guard with the
- 5:51ability to attack. The first thing I'm
- 5:53going to do is [music]
- 5:56We're going to add in this attack state.
- 5:58What this attack state does is while
- 6:01we're in the state, we're going to
- 6:03record the last player we've seen, and
- 6:05we're going to chase them down. And when
- 6:07we get close by, we're going to attack
- 6:09them on a cool down. Having a look at
- 6:12the final version of our NPC, which I'm
- 6:14calling the defender, you can see that
- 6:16we've added a new transition test, is
- 6:18player nearby. Just like before, this
- 6:20takes in the context, and we can use it
- 6:22to determine if a transition should
- 6:24occur. This test checks to see if a
- 6:26player is within a range of the NPC. And
- 6:29if it is, it sets our
- 6:30context.targetplayer
- 6:32to that value and returns true, letting
- 6:34us know that it's time to attack.
- 6:37If we come down and have a look at our
- 6:38transitions, you'll see that I'm
- 6:40leveraging a feature of the state
- 6:41machine. If you see the first transition
- 6:44there, I'm providing two different start
- 6:46states, letting us essentially have two
- 6:48arrows pointing at the attack state. If
- 6:51you have a look at the transitions
- 6:52table, I've added a new transition from
- 6:55idle or walk to random flag to our
- 6:57attack state, and that's using our is
- 7:00player nearby test. Coming down to look
- 7:02at our defender state machine, the only
- 7:04thing that's changed [music] is that
- 7:05I've added in a target player value,
- 7:08which is set to nil until the transition
- 7:11is player nearby finds a player to
- 7:13attack. Now that the defender is ready,
- 7:15let's see how the guard acts when we add
- 7:17that script to it.
- 7:20Let's try getting close to the guard and
- 7:22see what happens.
- 7:26Now, let's see how state machine NPCs
- 7:28can make a scene really exciting by
- 7:30adding tons of guards. All right, I've
- 7:32got nine guards walking around the
- 7:34palace. Let's see if I can get in, get
- 7:36the sword, and get out without dying.
- 7:38Okay, I've already aggroed a couple of
- 7:40them. This is not good. Okay, let me try
- 7:42that again.
- 7:47All right, sneaking past while they're
- 7:49idle.
- 7:50I'm in.
- 7:53Oh, shoot. Got the sword. Take one down.
- 7:57Took two down. Oh, there's a lot more.
- 7:59I got them all.
- 8:02Victory! So, there you have it. A guard
- 8:05that patrols, alerts, and battles you
- 8:07when you get too close. Next time you're
- 8:09making an NPC, feel free to try out the
- 8:11state machine library, or design your
- 8:12own. You'll be surprised how active and
- 8:14engaging your NPCs can be with just a
- 8:16few states and transitions. That's all
- 8:18from me. I hope you learned something
- 8:20new.
About this transcript
This page contains the full transcript of How to make guard NPCs using a state machine by Roblox Creators, generated from the public captions YouTube serves with the video. The transcript has 1,739 words across 249 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.