Issues with script to stop players noclipping through the map.

Hey guys, I’m trying to create a script to stop players noclipping through the map (eg noclipping through a wall of a building) however I’ve hit some issues. The main issue is that if the object is too thin the script doesn’t register that the player has passed through the map and therefore doesn’t stop them (presumably because the player is moving faster than the script is checking player position).

The script so far:
[lua]
hook.Add( “Tick”, “inWorldCheck”, function()

for _, ply in pairs( player.GetAll() ) do
	local plypos = ply:GetPos()
	
	local inworld = util.IsInWorld( plypos )
	if inworld == false then
		local plyang = ply:GetAngles()
		local backdir = -plyang:Forward() * 65
		
		ply:SetPos( plypos + backdir )
		ply:SetMoveType( 2 )
	end
end

end )
[/lua]

Just set the players movetype to MOVETYPE_FLY? It is the same as noclip, but it cannot go through the map.

I have considered this. Correct me if I’m wrong, but doesn’t that also stop you from noclipping through props? If so that would be quite annoying, so much so that it wouldn’t really be worth it.

You are correct, you could try using **[Gamemode.Move

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=Gamemode.Move)** and checking if the current velocity will bring the player outside of world, if it does, then change the velocity so it doesn’t happen.