Check if a player is stuck

So I’m trying to create a teleportation gun for ttt - You look at an entity, shoot at it, and you teleport to that entity.
The main issue I came across is players teleporting to outside the map - when an entity is out of bounds (Like a clear view to a prop/the floor outside the map).
Luckily, all of our maps don’t have any out-of-bounds areas that allow you to move freely, if you get outside of the map you’re just stuck. So I’m wondering - How can I check if a player is “stuck”, even though there is supposedly nothing around him? (I think it’s with player clips, I’m not sure, I’m clueless at this area :saddowns:)

Even better (if possible) - How would I prevent a player from teleporting to such a place to start with?

I believe using TraceHull can solve this. It traces from the player to whatever they’re looking at. Therefore, if the thing the player is looking at makes the trace end at where it starts, they are stuck… unless they’re in no clip.

It may be (marginally) easier to just use

util.TraceEntity because you don’t have to manually set the mins and maxes. I’ve used this function before to check if people are stuck and it seems to work pretty well.



-- Please actually define "pl" as your player 

if util.TraceEntity({start = pl:GetPos(), endpos = pl:GetPos(), filter = pl}, pl) then
    -- lol this guy is stuck
else
    -- lol this guy is not stuck
end


As for your second question, you could use

util.IsInWorld to check the position that you are going to send the player to. Alternatively, to prevent the player from getting stuck in anything, you can run a trace like the one I did above with the new position instead of the player’s position. This checks if the player will get stuck if they teleport there. I’d prefer the latter.