How exactly does :GetPartBoundsInBox work?

I have a bunch of tiles located near each other. I want to know if tile is occupied or not. To do that, I tried using :GetPartBoundsInBox, but no matter what I do, I can not get the list of parts in bound box, and it always returns empty array. Am I doing something wrong?

Script:

local tile = script.Parent.Parent.Base

script.Parent.MouseClick:Connect(function()
	local OverlapParams = OverlapParams.new()
	OverlapParams.FilterType = Enum.RaycastFilterType.Include
	OverlapParams.FilterDescendantsInstances = {workspace.Tile}
	OverlapParams.MaxParts = 4
	OverlapParams.CollisionGroup = "Default"

	local boundBox = workspace:GetPartBoundsInBox(CFrame.new(tile.Position.X, tile.Position.Y + 4.5, tile.Position.Z), Vector3.new(8, 8, 8), OverlapParams)
	if boundBox then
		print(boundBox)
	end
end)```
1 Like

When you hover over a part in studio, do you see that blue box around it? That is it’s bounds. What the function does is just get a segment of your choice, then checks to see if there are any bounds inside of it. Then puts all the parts into a table. Great for stuff like hitboxes if you don’t want to use .Touched!

1 Like

Yeah, that is indeed great. I just don’t understand why my script specifically does not work. It creates like a 8 by 8 by 8 studs box above the tile, and gets the parts inside that box, but that is just in theory. Do I have a wrong idea of how it works?

1 Like

You might need to put it inside a loop, but I do notice that it only checks if the click detector is clicked. It could be due to the fact that in line 5, you used Enum.RaycastFIlterType.Include. I’m not too good with enums, but shouldn’t you use something else instead? I don’t think :GetPartBoundsInBox() uses raycasts.

include means only the thing you’re including is going to be, well, included. Use exclude instead.

local tile = script.Parent.Parent.Base

script.Parent.MouseClick:Connect(function()
	local OverlapParams = OverlapParams.new()
	OverlapParams.FilterType = Enum.RaycastFilterType.Exclude
	OverlapParams.FilterDescendantsInstances = {workspace.Tile}
	OverlapParams.MaxParts = 4
	OverlapParams.CollisionGroup = "Default"

	local boundBox = workspace:GetPartBoundsInBox(CFrame.new(tile.Position.X, tile.Position.Y + 4.5, tile.Position.Z), Vector3.new(8, 8, 8), OverlapParams)
	if boundBox then
		print(boundBox)
	end
end)

Nevermind, I just did some reasearch myself and found out it does use Enum.RaycastFilterType. Are you using a Server Script or a Local Script for the click detector? Because you can’t use Local Scripts on a click detector.

1 Like

Of course I am using regular server script.

1 Like

It doesn’t matter what scripts you use, it’s just that you should use Exclude instead of Include for the raycast params. Streaming enabled may cause problems if you run it on a client.

Try doing what Precise said, that seems to be the solution!


Sadly, it still returns empty array, you can see that the regular part is above the tile though.

does thsi change anything?

local tile = script.Parent.Parent.Base

script.Parent.MouseClick:Connect(function()
	local OverlapParams = OverlapParams.new()
	OverlapParams.FilterType = Enum.RaycastFilterType.Exclude
	OverlapParams.FilterDescendantsInstances = {workspace.Tile}
	OverlapParams.MaxParts = 4
	OverlapParams.CollisionGroup = "Default"

	local boundBox = workspace:GetPartBoundsInBox(CFrame.new(tile.Position.X, tile.Position.Y + 0, tile.Position.Z), Vector3.new(8, 8, 8), OverlapParams)
	if boundBox then
		print(boundBox)
	end
end)
1 Like

Actually it’s not returning empty! The ... inside the table just means there are a lot of items inside!

3 Likes

No, it didn’t change anything.

try pressing the little arrow next to the table in the output

1 Like

Oh? But why would it say that there is a lot of items, if it is just 1 part?

My orginal solution did work, it’s just printing … like the other guy said.

Disable log mode
image

then see what it prints. try expanding.

1 Like

Press the arrow facing right., it expands.

1 Like

Not too sure, probably just something to do with roblox. But at least it works!

Ok, I checked, and that bounding box seems to just slightly touch other parts nearby. Ill make box smaller, and see if it changes anything

then in that case the problem has been solved, as your original problem was it not detecting at all.

even if a table has 1 object in it, it appears as { . . . } and you have to expand it.