Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Is it possible to check if specific item is in a model?

Asked by
Xyternal 247 Moderation Voter
2 years ago
Edited 2 years ago

For example, lets say I dropped a part on a model. I want it to tell me if a specific part is in there or not. (I do not want to use getDecendents() because its not going to work in this situation). I haven't posted any code because I don't know what to do.

To clarify this question, I want to check if a specific int value is in the model that the part touched, but i can't use getDecendents because there is going to be multiple models with the same name.

Addition

So whipped up a script using you're help guys, but it says workspace.d.Script:8: attempt to index nil with 'GetDecendents' what is wrong now?

local part = script.Parent

part.Touched:Connect(function()
    local par = part:GetTouchingParts()
    local model = par.Parent
    local z = model:GetDecendents()
    print(z)
end)

0
.Touched? GetTouchingParts()? greatneil80 2647 — 2y

2 answers

Log in to vote
1
Answered by 2 years ago

Yes, you can check using loops or if you specifically know the name you can check it through an index. Also, your objective is not quite clear. can you explain more because you can drop an object but it will most likely end up in the workspace?

Well if you want to check for example you may do something like

if object:FindFirstChild("ObjNameHere") then
    -- Do code
end
0
so lets say I have a few models in the workspace, but one of them has a special part in it. I want to see which model has the special part, by making another part touch it. Xyternal 247 — 2y
0
its kinda like checking to see if the thing a cannon ball hits has an int value for you to subtract from. Xyternal 247 — 2y
0
also, i want the function to trigger on touch, so i exactly would i do it? Xyternal 247 — 2y
0
Another thing, we're working with models that are going to be cloned, so i dont wanna use Decendentents. And find first child doesn't work Xyternal 247 — 2y
View all comments (2 more)
0
If you want an on touch you can use the touched event https://developer.roblox.com/en-us/api-reference/event/BasePart/Touched voidofdeathfire 148 — 2y
0
Alright, with some of your help, I made a nother script, can you check whats wrong now? Xyternal 247 — 2y
Ad
Log in to vote
1
Answered by
Xapelize 2658 Moderation Voter Community Moderator
2 years ago

I don't understand your answer but I think you mean checking a part is in a model, then you should be using IsDescendantOf().

Lets say if a part is inside a model, you want to check if the part is inside the model or not, this example line will print true after you ran the code in that case.

print(Part:IsDescendantOf(Model))

If I'm not wrong, I think you meant that if there is a few model in the workspace and you want to check the one model that have a specific part in it, then this line of code will help.

for _, model in pairs(workspace:GetChildren())
    if model:FindFirstChild("SpecialPart") then
        print(model.Name)
    end
end

Result here or use this link: https://imgur.com/a/NO1xyrY (I'm using parts for the example)

If the model's parent aren't in workspace and the models have different parent, then you should be using workspace:GetDescendants() instead of workspace:GetChildren(). But if all of the models have the same parent, consider doing :GetChildren() but instead of using workspace, use the pathway of the models' parent.


Well if you want to run a function when a part touches another part, use this code:

local Part = workspace.Part
local AnotherPart = workspace.AnotherPart

AnotherPart.Touched:Connect(function(Hit)
    if Hit == Part then
        print("Part touched AnotherPart")
    end
end)

Next time please explain your question better because it's kinda confusing

0
IAlright, so I sort of have a part which if u touch, it makes a boat spawn, using Clone(). Now if I use getDecendents with objects with the same name, its not going to work. Xyternal 247 — 2y
0
And i want this function to trigger if a part hits something, but ik how to do that. I just need another way of doing it. Xyternal 247 — 2y

Answer this question