YouTube2Text

How to make guard NPCs using a state machine — Transcript

by Roblox Creators · 1,739 words · 249 segments · language en · Watch on YouTube

Full transcript

  1. 0:00So, let's say you want to create
  2. 0:01exciting NPCs to fill your game world,
  3. 0:04but dumping all your logic into one big
  4. 0:06file leads to chaotic code you'll never
  5. 0:08be able to debug.
  6. 0:09If you want NPCs that you can customize
  7. 0:11quickly, reuse all over your world, and
  8. 0:14debug easily, you're going to want a
  9. 0:15state machine.
  10. 0:17Hi, I'm Miller, a software engineer here
  11. 0:19at Roblox. Today, we're going to learn
  12. 0:21how to use a state machine library to
  13. 0:23make a guard NPC that patrols and gets
  14. 0:26alerted if you come too close. All the
  15. 0:28code I'm going to show today, plus the
  16. 0:30place file, you can download and follow
  17. 0:32along if you want to.
  18. 0:34So, let's have a look at the world we'll
  19. 0:35be working in today.
  20. 0:36>> [music]
  21. 0:37>> As you can see, I've already gone ahead
  22. 0:38and set up a castle, as well as a simple
  23. 0:40NPC. This guy just has a humanoid,
  24. 0:43nothing else, and no other code telling
  25. 0:45him what to do. I've also set up these
  26. 0:47four flags that we use as waypoints for
  27. 0:49the guard's patrol and tag them. And
  28. 0:52lastly, I've put a sword in the middle
  29. 0:53on a golden pedestal that we can try to
  30. 0:55grab before the guards attack us. So,
  31. 0:57let's get started by having a look at
  32. 0:59our code. Here, I've got our types file.
  33. 1:01We're going to check out the different
  34. 1:02kind of tables we're going to be using
  35. 1:04in our state machine. We're going to
  36. 1:05have a context table, which is going to
  37. 1:07be a [music] global blackboard for all
  38. 1:08the information our NPCs need. We're
  39. 1:10going to have state tables, which are
  40. 1:12the possible configurations of each NPC.
  41. 1:14Test functions, which are run by
  42. 1:16transitions to connect the states, and
  43. 1:18finally, an actual state machine,
  44. 1:19[music]
  45. 1:20which is all the transitions we have,
  46. 1:22our starting context, our starting
  47. 1:23state, and we give it a name. Now, this
  48. 1:25right here is the meat and potatoes of
  49. 1:27the state machine. This is the tick
  50. 1:28function. This takes in one of those
  51. 1:30state machine tables we just saw, and
  52. 1:32it's going to run the tick function,
  53. 1:33checking each transition coming out of
  54. 1:35the active state to see if we should
  55. 1:37take it or not.
  56. 1:38>> [music]
  57. 1:38>> If we take a transition, we're going to
  58. 1:39run an on exit and an on enter function
  59. 1:42on the state we're leaving and entering,
  60. 1:44respectively. And if we don't
  61. 1:45transition, we're going to run an on
  62. 1:47stay function. That's really all the
  63. 1:49code that actually matters for using the
  64. 1:50state machine. I've also provided a
  65. 1:52composition test thing that lets you
  66. 1:54combine all multiple test functions
  67. 1:56[music] to create a combination, like a
  68. 1:58boolean and, or, or not. And then also,
  69. 2:02a single module that you can access all
  70. 2:03of them from called simple state
  71. 2:05machine. All right, now we're going to
  72. 2:06create an NPC that I'll call the walker.
  73. 2:08This NPC is going to walk between flags
  74. 2:10one and two. So, here we've got the code
  75. 2:12for the walker. Uh if you have a look
  76. 2:14here at the top, we've got the types,
  77. 2:16which we've just imported from simple
  78. 2:17state machine. I've set up a transition
  79. 2:19test, and then what I've also done is
  80. 2:21set up the actual states. So, if we have
  81. 2:22a look at walk to flag one, now when the
  82. 2:25NPC is in this state, it's just going to
  83. 2:27make them walk towards flag one. We've
  84. 2:29got a second state called [music] walk
  85. 2:30from to flag two. It's going to make
  86. 2:32them walk towards flag two. Now, let's
  87. 2:33talk about what this state does. What
  88. 2:35happens is when the NPC enters the
  89. 2:38state, we're going to set
  90. 2:39context.targetflag
  91. 2:41to flag one. Context, that's a global
  92. 2:43blackboard. We can keep information
  93. 2:45about the NPC in there. Then, in the on
  94. 2:47stay function, which is run every tick,
  95. 2:50we're going to have the humanoid set its
  96. 2:52move to to the flag one's pivot
  97. 2:55position. Walk to flag two is going to
  98. 2:57be the exact same thing, just replacing
  99. 2:59flag one with flag two. Coming back to
  100. 3:01our original file, you can see I've set
  101. 3:03up transitions. If we're in walk to flag
  102. 3:05one, we check if we're near the target
  103. 3:07flag. That's flag one. If we are, we go
  104. 3:09to flag two, and I made a transition to
  105. 3:12do the opposite. I just load those
  106. 3:14things into the state machine, and I set
  107. 3:16up some context information. The
  108. 3:17script's going to need to know what our
  109. 3:18character is, what our humanoid is, and
  110. 3:21also which flag we should start going
  111. 3:23to. We're going to start by going to
  112. 3:24flag two. And then all I've got to do is
  113. 3:26set heartbeat to run the tick function I
  114. 3:28showed you earlier. Now, all I've got to
  115. 3:30do is drag our walker script to the
  116. 3:31guard, and we'll see what happens.
  117. 3:38As you can see, the guard's walking to
  118. 3:39flag two, and when he reaches there, he
  119. 3:41switches back to the first state, walk
  120. 3:43to flag one,
  121. 3:45and he goes back and forth.
  122. 3:48So, that's a start, but we can do
  123. 3:49better. You might have noticed in the
  124. 3:50last one that walk to flag Now, let's
  125. 3:52try that again. You might have noticed
  126. 3:54for the walker that the states walk to
  127. 3:55flag one and walk to flag two [music]
  128. 3:57are pretty similar. So, the first thing
  129. 3:59I'm going to do is create a more generic
  130. 4:01called walk to random flag.
  131. 4:04What this [music] state is going to do
  132. 4:05is it's going to pick the next flag to
  133. 4:07go to randomly. So, instead of just
  134. 4:08going between one and two,
  135. 4:10>> [music]
  136. 4:10>> we're going to go to one of the two
  137. 4:12flags on an adjacent corner. So, the
  138. 4:14walk to random flag, when we enter it,
  139. 4:16is going to have us set the next flag.
  140. 4:19So, we'll figure out which flag we
  141. 4:21should go to randomly. And then what
  142. 4:23we'll do is in the on stay, we'll do the
  143. 4:24exact same thing we saw earlier with
  144. 4:26humanoid move to to have the character
  145. 4:28walk towards the next flag. [music] When
  146. 4:30you take that logic and you add on to it
  147. 4:32a simple idle state that has the
  148. 4:34character hang around for a second after
  149. 4:36reaching a flag, we end up with the
  150. 4:38patroller. The patroller's going to have
  151. 4:41a lot of the same code, but we're going
  152. 4:42to switch around the transitions. We're
  153. 4:44going to go [music] from idling, waiting
  154. 4:46around for a second, to walking to the
  155. 4:48next random flag. We're going to have
  156. 4:50two tests for this one. We're going to
  157. 4:52have is near target flag, which checks
  158. 4:54if we're close enough to the flag, and
  159. 4:56we're also going to have is idle timer
  160. 4:58done, which tells us if we've been
  161. 5:00idling for long enough that it's time to
  162. 5:02go back to patrolling.
  163. 5:03Again, we're going to take all that and
  164. 5:05put it into a state machine. We're going
  165. 5:06to add some new values to our context.
  166. 5:09Now, we're going to have an idle start
  167. 5:11time, and we're going to have a current
  168. 5:13flag number, so we can track all four
  169. 5:15flags. Connect it to heartbeat, and
  170. 5:17we'll watch it go.
  171. 5:30As you can see, our NPC now chooses
  172. 5:32flags randomly, letting it explore the
  173. 5:34entire castle and keep it protected from
  174. 5:36all sides.
  175. 5:38That said, although this guard does
  176. 5:40patrol between all the flags, it doesn't
  177. 5:42really protect it. So, the next thing
  178. 5:44we're going to add is new states for the
  179. 5:46guard to attack us if we get too close
  180. 5:48to prevent us from stealing the sword.
  181. 5:50So, let's soup up our guard with the
  182. 5:51ability to attack. The first thing I'm
  183. 5:53going to do is [music]
  184. 5:56We're going to add in this attack state.
  185. 5:58What this attack state does is while
  186. 6:01we're in the state, we're going to
  187. 6:03record the last player we've seen, and
  188. 6:05we're going to chase them down. And when
  189. 6:07we get close by, we're going to attack
  190. 6:09them on a cool down. Having a look at
  191. 6:12the final version of our NPC, which I'm
  192. 6:14calling the defender, you can see that
  193. 6:16we've added a new transition test, is
  194. 6:18player nearby. Just like before, this
  195. 6:20takes in the context, and we can use it
  196. 6:22to determine if a transition should
  197. 6:24occur. This test checks to see if a
  198. 6:26player is within a range of the NPC. And
  199. 6:29if it is, it sets our
  200. 6:30context.targetplayer
  201. 6:32to that value and returns true, letting
  202. 6:34us know that it's time to attack.
  203. 6:37If we come down and have a look at our
  204. 6:38transitions, you'll see that I'm
  205. 6:40leveraging a feature of the state
  206. 6:41machine. If you see the first transition
  207. 6:44there, I'm providing two different start
  208. 6:46states, letting us essentially have two
  209. 6:48arrows pointing at the attack state. If
  210. 6:51you have a look at the transitions
  211. 6:52table, I've added a new transition from
  212. 6:55idle or walk to random flag to our
  213. 6:57attack state, and that's using our is
  214. 7:00player nearby test. Coming down to look
  215. 7:02at our defender state machine, the only
  216. 7:04thing that's changed [music] is that
  217. 7:05I've added in a target player value,
  218. 7:08which is set to nil until the transition
  219. 7:11is player nearby finds a player to
  220. 7:13attack. Now that the defender is ready,
  221. 7:15let's see how the guard acts when we add
  222. 7:17that script to it.
  223. 7:20Let's try getting close to the guard and
  224. 7:22see what happens.
  225. 7:26Now, let's see how state machine NPCs
  226. 7:28can make a scene really exciting by
  227. 7:30adding tons of guards. All right, I've
  228. 7:32got nine guards walking around the
  229. 7:34palace. Let's see if I can get in, get
  230. 7:36the sword, and get out without dying.
  231. 7:38Okay, I've already aggroed a couple of
  232. 7:40them. This is not good. Okay, let me try
  233. 7:42that again.
  234. 7:47All right, sneaking past while they're
  235. 7:49idle.
  236. 7:50I'm in.
  237. 7:53Oh, shoot. Got the sword. Take one down.
  238. 7:57Took two down. Oh, there's a lot more.
  239. 7:59I got them all.
  240. 8:02Victory! So, there you have it. A guard
  241. 8:05that patrols, alerts, and battles you
  242. 8:07when you get too close. Next time you're
  243. 8:09making an NPC, feel free to try out the
  244. 8:11state machine library, or design your
  245. 8:12own. You'll be surprised how active and
  246. 8:14engaging your NPCs can be with just a
  247. 8:16few states and transitions. That's all
  248. 8:18from me. I hope you learned something
  249. 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.