How to make an NPC push player?

How would I make a system where if the player gets too close, the NPC shoves the player back?

You would need to create a state for each NPCs and apply the state for them each time it gets cloned, The reason why i picked finite state machine is because it’s easier to control such behaviors and you can switch them if something happens, That way you don’t have to struct them in one function.

there’s a video for this one that explains how FSM works and how you can implement it in your own

one thing I’d like to mix is the OOP structure into the NPC, that way it can be easier to manage and you don’t have to create such things over and over again with self being used, I’ve been doing this for my lovely game “DVN: Warfare” to speed the process up, but OOP isn’t rlly friendly if you don’t know how it works which i always blamed it for complexity but after times it became easier.

if you’re already familiar with it, check out my open source wave based game to see how it works, it isn’t rlly in a good structure as it had bugs and memory leaking but i plan to remake the game after my updates for my fanmade game (the game isn’t 100% all made with OOP but only NPC)

this one uses an advanced module of FSM called Stater which i find it convenient and easier to control without using CollectionSrrvice and loop them in one script, instead, they are a module.


and to implement such behavior like what you’re describing, you would need a function that checks for the closest player and check their distance, if the target distance is lower than what you gave, run the state and add something called AlignPosition to the player it pushes for temporary and delete it with Debris Service which doesn’t yield but schedules to destroy just like callin destroy.

3 Likes