How to make Character hit wall on nockback

Hello!
I am intrested in making a way for my combat system to detect when the enemy that was just thrown by a player hitted a wall from behind them.

I tried making this in the past but i used while loop with raycasting on a server script and made a lot of lag.

Is there any way to achieve this efficiently?

Any suggestions whould be greatly appreciated!