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.
What exactly is “getpartsinpart”? Provide examples and api reference (if available)
: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.
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?
I actually forgot to add it as the second argument to the function, it’s now fixed.
thanks! I got kinda confused lol
Hey.
Kinda late but, would you know how to make it so the script checks for the player’s head inside the part and print something if it detects it. Using getpartsinpart() ofcourse.
To do what you said. it is better to use GetPartBoundsInBox.
local function checkForHead(Part: Part)
local haveHead = false
local result = workspace:GetPartBoundsInBox(Part.CFrame,Part.Size)
for i,v in pairs(result) do
if v.Name == "Head" then
haveHead = true
end
end
return haveHead
end
local have = checkForHead()
print(have)
MAN
Just what I was looking for, thank you SO MUCH. Their documentation can be so lack luster. The provided examples helped me understand this a TON.