Any way to add body part to FilterDescendantsInstance so GetPartsInPart can detect it from any character?

I am making a script that detect player inside a part and I want to reduce the size of array from GetPartsInPart function because I will infinitely iterate through the array and in case of many players are inside the area at once.

I used FilterDescendantsInstance but I can’t directly assign a table of body part’s name into it or using enum because it will cause “Unable to cast value to Objects” error.

local ovl = OverlapParams.new()
ovl.FilterDescendantsInstances = {Enum.BodyPart.Torso}
--or
ovl.FilterDescendantsInstances = {"Torso"}

So I try to get the part in character when player is added to server.

local ovl = OverlapParams.new()
game.Players.PlayerAdded:Connect(function (player)
	player.CharacterAdded:Connect(function (char)
		ovl.FilterDescendantsInstances = {char:WaitForChild("HumanoidRootPart")}
	end)
end)
ovl.FilterType = Enum.RaycastFilterType.Include

The problem is it return only part from player the script above detect first, not any player who are inside the part.

Are there any ways to put name of character’s part to filter or any functions that can do this kind of stuff or name of topic that relate to this issue so I can learn.

Oof, I realize that line 2 and 3 run regardless of the order of code, so I just need to make a list to contain body part of every character that is added and constantly update filter.

local ovl = OverlapParams.new()
local whitelist = {}

game.Players.PlayerAdded:Connect(function (usr)
	usr.CharacterAdded:Connect(function (char)
		table.insert(whitelist, char:WaitForChild("HumanoidRootPart"))
	    ovl.FilterDescendantsInstances = whitelist
	end)
end)
ovl.FilterType = Enum.RaycastFilterType.Include

you could just get all the characters in game and add those to array and pass it to it then it will consider any characters body parts/descendants

maybe like this

local Characters = {}
for _, player in ipairs(game.Players:GetPlayers()) do
	table.insert(Characters, player.Character)
end

ovl.FilterDescendantsInstances = Characters

should work now had to fix Players had typo

and i will say roblox did something in an update today to GetPartsInPart and even when sitting on a part it may not show its touching it

1 Like

In this case, The GetPartsInPart will detect other parts that are in character model too(left/right hand arm and leg, head, hat, etc) so I think it is not the best way to get smallest array of GetPartsInPart.

and thank you for telling me about the update I will look into it, thank you for your time.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.