How do I count models inside of a part

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I’m making a game where which ever team puts the most models inside an area, as parts are added or removed from inside the part,

  2. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Using GetTouchingParts(), but couldnt figure out how to make it not count duplicates if theyre in the same model.

8 Likes

You could the touching parts’ parent to determine which model it is in. If your model has nested parts inside it, then you can use the FindFirstAncestorWhichIsA function that finds the touching parts’ ancestor with the specified Class.

But if you have a model inside a model, I’ll try my best to find another solution. There might be an answer in the forum.

I need all the models parented to a folder in workspace, that or once they leave the area of the part they get remodeled

So you want the models to change if they leave the inside of a part?

I just want a count of the models inside of the area of my part, think of the part like a trash can, I want a score of how many models are in this trash can.

You can use my answer above to try. GetTouchingParts can only get all the descendants, that are BaseParts, inside the models. FindFirstAncestorWhichIsA(“Model”) gets the closest Ancestor of the part that is a Model.

For some reason it recognizes my character, but not my box?

Does your box have models inside it?

image

You could use Part.Parent to check for models like this. But I’m not sure if your other models have the same nested Parts or Models inside it.

So why doesn’t GetTouchingParts recognize it?

Maybe your part has CanCollide turned off. GetTouchingParts also can’t detect parts without CanCollide. If you want them to detect those parts, consider this article:

image

Oh. Then I’m not sure how GetTouchingParts doesn’t work with the box.

Oh wait I know!

The big part you use to detect the parts inside it doesn’t work if CanCollide is turned off.

You should still consider the link I sent above because this one probably works.

Now how would I make sure it doesnt count duplicates?

Using the Parent or the long ahh function I mentioned above. You can use those to compare if they have the same parent.

How would I figure out when it’s no longer in the part?

Idk but you can use the TouchEnded on the big part and run the GetTouchingParts again to see which Model is not in the part.

This reply has been marked has a solution, but I’d like to explain how I kinda solved this topic.

GetTouchingParts function ran by a bigger part does work counting things but it only counts parts and not models. So I suggested finding its ancestor to get the Model and compare each part to see which Model they belong to. If one part has the same model as the other, ok then. The part will be detected as a duplicate and would not be counted.

Additionally, he asked how he would figure out which model is no longer in the part; the answer is already at the top.

Ok bye

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