Hi, I’m scripting and I realized something. I was making a script that destroys a part if it’s falling down and touching a part that’s in a model called “Map”. But I don’t know how to make a script that detects if the part is touching something in the model.
script:
while true do task.wait()
if script.Parent.AssemblyLinearVelocity.Y < -5 and script.Parent.??? then
script.Parent:Destroy()
end
end
Don’t use a while loop for checking they are messy and very inefficient.
There are events for when parts touch things (part.Touched) that also pass what touched(hit). You can check if hit is something in the model using an if statement.
part.Touched:Connect(function(hit)
if hit.Parent == Model then
-- code
end
end)
You’ll need to use an event, as already aforementioned. Here is the right way to check if it a part in the model has been touched by your part:
script.Parent.Touched:Connect(function(hit) -- assuming that the parent is the part
if script.Parent.AssemblyLinearVelocity.Y < -5 and hit:IsADescendantOf(Model) then
script.Parent:Destroy()
end
end)
Change 'Model' on Line 2 so it references the model called ‘Map’.
I also thought I’d mention that event is something that is fired when something happens, e.g. the Touched event fires when the mentioned part collides with another object, and then the function attached to it runs automatically when touched. The argument in the function, which is ‘hit’ in this scenario, is the variable containing the part that it collided with.