Sensing humanoid leaving a specific range

Basically I want to sense a humanoid in a specific range of a block ( and I already achieved that by calculating the distance between the humanoid and the block, if it is lower than a value then it will start attacking the humanoid ).

The problem is, how do I detect when the humanoid leaves that specific range? ( It is like when in a tower defense game, the tower attacks enemies inside a specific range and when the enemy leaves the range, it stops attacking ). I have searched online and people keep saying to use ray but I don’t really get it. Can somebody explain it to me? Thanks

This might be addressed by the architecture you’re using to attack the humanoid. Are you housing the enemies being attacked in a table? I would approach it by, if a humanoid “enters” the area around the block, add them to a table, so you can continue referencing that humanoid who you know is now within range. Then, you can separately iterate that table, seeing if they are still within range. If they are, attack them! otherwise, remove them from the table, as they must have “left” the area by that point.

1 Like