How would I make a NPC be able to open doors using pathfinding or raycasting?

Hello! I am working on a stealth game where you are given missions by someone to complete certain tasks. In the game, I am currently building a school environment for the player to be able to explore in. I would also like to add NPCs to give the school a little bit of action and give the feeling of a “real school.” However, I do not know how to make NPCs be able to open doors to access classrooms and other important features students should be able to have in real-life schools. I have gone to Roblox’s official discord and went into the developer help section and asked how I would be able to make it happen, which someone answered with “Raycasting.” I have never used Raycasting before and am still kinda confused on how to make it possible. Can someone please help me? Thanks for reading!


Well when someone says “raycasting” they’re suggesting you should use the power of raycasting combined with other functions. What you’re asking for is pretty complicated and you’ll have to do most of you’re research on the Developer Hub.

Here are some resources that may help you in your endeavour.

Pathfinding Service This one will be quite difficult so you’ll have to spend quite some time learning this in particular.: Character Pathfinding | Documentation - Roblox Creator Hub

Raycasting Basics: Raycasting | Documentation - Roblox Creator Hub

Magnitude: Vector3 | Documentation - Roblox Creator Hub

Collection Service: CollectionService | Documentation - Roblox Creator Hub

CFrame: CFrames | Documentation - Roblox Creator Hub

Those are a bunch to get you started, persevere and you’ll reach your goal of creating a lively school!

Good luck! :grinning:


Alright! I’ll read up on those to gain a bit more knowledge on this topic. Thanks!

1 Like

I was finally able to make the NPCs open doors after reading the pages you listed. This took hours even though it sounds kinda simple, lol, thanks for helping me! (Don’t mind my brother’s and my avatars… along with the laptop I forgot to anchor lol)


That’s some crazy progress. I didn’t expect you’d create something that good so quickly.

You’re growing up as a scripter, good luck in your scripting career! :grinning:

Forgot to add:

Here is a TweenService tutorial to animate the door: TweenService | Documentation - Roblox Creator Hub

Try loading a door open animation on the NPC to give a more “lively” feel.

1 Like

Alright! I’ll improve the door as much as I can! Thank you so much for your help, I wouldn’t have known what to do if it wasn’t for you lol, thanks!

1 Like

This is the final product! :smiley: Any suggestions or feedback?

1 Like

Perfect! The NPC could be moved a bit closer, but that’s a little nit-picky.

1 Like

thats really nice dude, out of curiosity… what code did you use? Can you show us? if not thats cool :))

1 Like

Of course! So, I started out by designing the doors and after I finished doing that, I added a bool value called “Open.” I added a script to the door that whenever it detects that the bool value has changed, it will open/close the door accordingly to whether it is true or false using TweenService. I also decided to add another bool value called “Locked” so if the locked value is set to true, the door can’t be open. Here is the code for that:

image image image image

This is what the door should look like after you’ve done that:


After I got the system of opening and closing the door working, I started working on the NPC. I started out by making a list of all the Vector3 points in the workspace that the bot has to stop to open the doors from. Since it took minutes for the NPC to get to that EXACT Vector3 position, I made it so the code could proceed if the NPC was from a certain magnitude from the Vector3 position. I wrote that all down in a script and got it to work! But then as @xKaihatsu stated, I could make it better by adding animations to the NPC. So after making an animation, I added to the function of opening doors that whenever the NPC does decide to open a door, it will make the NPC play an animation. Here’s the code for that:

image image image

And this is what the NPC looks like in the workspace with that script:

(It’s the script called “Pathfinding” and it’s currently disabled because I didn’t want it to replicate right away, but if you want it to work, make sure its enabled lol)

And that’s all I did!, if you have any questions about my code, just DM me so I can help you figure it out! I hope this helped!

(And for the record, you might wanna change some stuff up because you may want your door to open a different direction and maybe your door is completely different from mine so make sure you do what works with you. And also, don’t forget to change the Vector3 positions of where you want your NPC to stop and check for doors to open)


I just read through this entire thread, and wow–great job! Completely coincidental, but I’m planning on implementing something just like this over the next several days, so it’s cool to see someone’s already done what I want to do!

I do, however, still feel obligated to give you a bit of constructive criticism on your code, since we’re in the Scripting Support category :grin:

I’d highly, highly, highly recommend trying to find ways to make your code more situational, especially if you’re planning on having lots of doors and lots of NPCs. What I mean by this is instead of using the exact CFrames and Position Vector3’s for each door open position, close position, & the target positions for NPCs, try to automate the process, such that if you add a new door into the game, you won’t have to manually figure those out.

In this specific situation, it may be more trouble than it’s worth, but I wanted to suggest you do this because as a programmer, it’s good to build a habit out of optimizing reusable assets & their functionality. It’d also probably be a really good learning experience!

Take this advice with a grain of salt, but do consider it. Again, great work–it looks really good!

P.S. You can copy-and-paste code into the textbox instead of posting screenshots, and format the code by surrounding it with three `'s:
pic of code
. . . becomes:

function ARandomFunction()
      print "Hello, world!"
1 Like

Thank you for the criticism! I will take everything you said into account and make the script better. I guess my coding was a bit messy because I was kind of in a rush to get it finished lol, but now, I’m gonna take my time to improve my code and make it more situational as you stated. Thanks for the advice!

1 Like

Lmaooo that is a lot of code for just a npc opening a door… (I’m new so that is why I said that lmao)


Yeah, I had to figure out a method to make it work and the only one I was able to come up with was the one in that code lol, but yeah, it is ig lol