I'm attempting to make a brick that has a function and when "BuildingBrick" touches it, it will delete. The function works but only for un-anchored "BuildingBrick". How would I make the function un-anchor the part(BuildingBrick) and destroy it when it touches the brick with the script? Edit: I'm using CFraming.
What I attempted to do:
function onTouched(hit) if hit.Name == 'BuildingBrick' and hit.Anchored then hit.Anchored = false hit:Destroy() end end script.Parent.Touched:connect(onTouched)
I've also tried
function onTouched(hit) if hit.Name == 'BuildingBrick' or hit.Anchored then hit:Destroy() end end script.Parent.Touched:connect(onTouched)
and it dosen't work
As someone mentioned in the comments, the Touched event will not fire if you are CFraming a part through another part. However - you could use a while loop and the GetTouchingParts() method to see if said part is touching the part. Script:
while wait(.1) do -- Not foolproof, because if the part takes less than .1 seconds to move through the part, it won't necessarily pick up the touch local parts = script.Parent:GetTouchingParts() for i, part in pairs(parts) do if part.Name == "BuildingBrick" and part.Anchored then part.Anchored = false part:Destroy() end end end