I don’t wanna outright leak it, but i will break down how it works since it’s not that complicated.
I haven’t noticed any performance issues with it, the activity seems to linger around 5%
For reference, this is a local script, since the game is singleplayer.
it has a patrol mode and a chase mode, which it can switch between quickly. both update every frame because i’ve run into some really strange issues with the patrol mode when calculating the path only once like trying to just… fly up stairs i guess? making it update constantly has fixed that though.
the loop isn’t an actual loop though, it’s a recursive function, i call Walk() once in a different thread (task.spawn) and after it finishes, the function calls itself again, like so

i would love to use Heartbeat, however that means i cannot yield, and i have to yield a second before choosing a new patrol point.
if i need the loop to terminate itself, i have an if statement at the start checking if a “killloop” variable has been set to true, hence it will not continue and it will not be able to call itself again.
![]()
however this feels like a really hacky way to get around it. i would love some feedback on this
i was also made aware that raycasts are supported in parallel now, and i’d like to run them in parallel since i am shooting 11 raycasts every frame (number will increase) which can’t be good for performance.
if spotted then it calls a function decreasing a timer to cause the AI to lose track.
that same timer is added to if you are in direct line of sight (no debounce to it is intentional)
that’s basically the bulk of my code. how should i go about improving it? it’s not hard to manage, i’m just worried about performance. i could probably run the raycasting code in parallel in a heartbeat loop, but aside from that i really don’t know
also i’m really sorry if this is the worst code you’ve read i am genuinely not that good at organizing my code for others to read ![]()



