Hey! I wasn’t sure what category to use for this post but this seems like the most fitting one.
I was recently doing research on enemy AI for a project I’m working on and decided to look into the most classic example on the Roblox marketplace - the Drooling Zombie.
Just researching the code was a great learning experience about state machines (which is what Roblox utilised for the drooling zombie’s behavior) and I would recommend it to anyone looking to learn about this stuff.
I wanted to share some interesting things I found while looking over the source code:
-
On line 259 of the ROBLOX_ZombieAI modulescript, there is a bypass to a function that evaluates whether the zombie lost line of sight with the player it’s targetting.
Normally, the zombie should be able to lose sight of the player when in the “Pursue” state making it go into the “Hunt” state where it goes to the player’s last known position, and eventually gives up and goes back to the “Idle”/“Search” state.
Because of thisif true then return false endstatement, the zombie will never lose line of sight of the player. In practice this means that when the zombie starts chasing a player, it won’t stop until that player is dead.
I can imagine this bypass was added because the “Hunt” state was really bad at finding the player, so escaping from zombies was too easy. -
The zombies have a repulsion force that pushes them away from eachother when within less than 2.5 studs of separation. This ensures that big hordes don’t get too crammed up.
-
When in the “Search” state where the zombie isn’t chasing any target and is just randomly wandering around, it chooses a random location and walks there every 2 seconds.
It does so by picking a random X and Z offset between 5 and 10 studs (random sign) and then check if that location collides with something. If it doesn’t, the zombie pathfinds there. Interestingly enough, If that location does collide with something, it will check again exactly 4 studs up.
Anyways, If you’re interested in this stuff I recommend you look into the drooling zombie’s code yourself as it seems like a solid foundation for a general state machine NPC AI.

