How do I use getpartsinpart()?

hi im tryna make devil fruits for my game, and i wanna know how getpartsinpart() work, Im kinda confused since I didnt understand the roblox api page of it and there was no example scripts. thanks in advance.

8 Likes

What exactly is “getpartsinpart”? Provide examples and api reference (if available)

3 Likes

https://developer.roblox.com/en-us/api-reference/function/WorldRoot/GetPartsInPart

2 Likes

:GetPartsInPart() Searches for other parts inside the space of the provided part on its first parameter and returns a table containing them, here’s an example:

local Workspace = game:GetService("Workspace")

local part = Instance.new("Part")
part.Anchored = true
part.Parent = Workspace

local foundParts = Workspace:GetPartsInPart(part)

This code will create a part and run the :GetPartsInPart() function using it as the first argument, it’ll then store the resulting table in the “foundParts” variable, this table will contain all the parts that were inside the provided part volume at the moment of the execution. The second parameter of the function allows you to change the way it searches for parts by adding different types of filters to it such as an exclusion or inclusion list, to do this you must provide an OverlapParams object to the second argument. Here’s an example code on how to exclude certain parts from the query:

local Workspace = game:GetService("Workspace")

local part = Instance.new("Part")
part.Anchored = true
part.Position = Vector3.new(0, 15, 0)
part.Orientation = Vector3.new(0, 90, 0)
part.BrickColor = BrickColor.new("Bright green")
part.Parent = Workspace

local otherPart = Instance.new("Part")
otherPart.Name = "OtherPart"
otherPart.Anchored = true
otherPart.Position = Vector3.new(0, 15, 0)
otherPart.BrickColor = BrickColor.new("Bright red")
otherPart.Parent = Workspace

local filter = OverlapParams.new()
filter.FilterType = Enum.RaycastFilterType.Exclude
filter:AddToFilter(otherPart)

local foundParts = Workspace:GetPartsInPart(part, filter)
print(foundParts)

This code will create one red part and one green part, run the function and print the resulting table, both parts are created at the same exact position and occupy the same space, however since we added “otherPart” to the exclusion list in our OverlapParams object the function will not detect it and the returned table will be empty.

Hopefully this makes it more clear for you! If you have any questions don’t hesitate to ask.

34 Likes

I have a question: I need to use GetPartsInPart() with a blacklist which ignores certain things in the game. But, in your code I don’t see where you used overlap params. Can you explain what you did?

2 Likes

I actually forgot to add it as the second argument to the function, it’s now fixed.

3 Likes

thanks! I got kinda confused lol :slight_smile:

2 Likes

@SentinelvCup you should mark this guys post as solution dude

4 Likes