I think that you can achieve what you want using raycasting.
See this: youtube.com/watch
You will have a laser to the player`s angle. Before teleport, compare if the laser is bigger than the amount of space needed for the teleport, if have space enought, just do the regular teleport, if not, teleport it to where the laser ends.
Thanks for this method but I don't know how to do it to teleport through the wall i really dont know.
that's why i would ask for a capx
Thank you for this material, it will definitely be useful to me