How to tell if a player is inside a part?

Seen some threads about this but using Region3 or something like that, but when i try it myself it says its deprecated. and how would i tell if a player is not in the part

1 Like

What was that deprecated code?

You can use worldroot:getpartsinpart() or worldroot:getpartboundsinbox() + more on the wiki.

My one was inspired by this.

1 Like

Using GetPartsInPart(), how would I tell if a player is touching the part?

if(part.Name == "HumanoidRootPart") then -- if the HRP is inside the wall - they're definitely exploiting.

Then part.Parent. This reads to Player’s character and FindPlayerFromCharacter’s turn.

Getpartsinpart is pretty resource heavy, but it returns a table of parts. All you would do is loop through the table and check if game.Players:getplayerfromcharacter(part.Parent) then there is a player inside.

although 2 years ago but the solution is just using

(there might be spelling issues im currently not on studio)

workspace:GetPartsInPart(Part, Params) -- the Part is just basically the region that you want to check if the player is inside, (or you can just use GetPartsInRegion if you dont want any actual part for the region)

and then just check if one of the parts’ parent have a humanoid, and if you want to check for the local player just check if the parts’ parent that has humanoid’s name is the same as the local player’s name

hi👋

sorry to bump this post again (i know this is so old), but if somebody would’ve said this, it would’ve helped me alot with my issue:

the :GetPartsInPart() function doesn’t accurately detect if a part is actually inside of another. it just returns the parts that are barley touching it.

although the OP specifically said that they are trying to detect if a player is not in a part. many people including myself would be trying to do the opposite, (see if a player is fully inside of a part). which this is not very reliable for, and not to mention exploiters can bypass it so that they aren’t detected.

assuming your barrier/area has an orientation of (0,0,0). (for my case it does) its quite simple to write a script seeing if a player is inside of a part.

(if this is for something important, the script should be hidden from the player, in ServerScriptService)

local partPosition = workspace.BrownPart.Position
local partSize = workspace.BrownPart.Size
local players = game:GetService("Players")

players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		
		local hrp: BasePart = char:WaitForChild("HumanoidRootPart")
		
		while task.wait(5) do
			
			local xDistance = (Vector3.new(partPosition.X, hrp.Position.Y, hrp.Position.Z) - hrp.Position).Magnitude
			local yDistance = (Vector3.new(hrp.Position.X, partPosition.Y, hrp.Position.Z) - hrp.Position).Magnitude
			local zDistance = (Vector3.new(hrp.Position.X, hrp.Position.Y, partPosition.Z) - hrp.Position).Magnitude
			
			local xSize = partSize.X
			local ySize = partSize.Y
			local zSize = partSize.Z
			
			if xDistance < xSize / 2 and yDistance < ySize / 2 and zDistance < zSize / 2 then
				print("player is inside of part")
			else
				print("player isnt inside of the part")
			end
			
		end
	end)
end)

this works for all part sizes, and is easily customizable. you could kill the player whenever they are inside of the bounds, teleport them back, or even kick them after a certain amount of strikes (this is very reliable for anti-cheat) but it can also be used for other more simple things.

my point is, you shouldn’t rely on the GetPartsInPart() function unless its for something simple/cosmetic. hope I could help someone.:+1::smiley:

1 Like