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:
1 | function onTouched(hit) |
2 | if hit.Name = = 'BuildingBrick' and hit.Anchored then |
3 | hit.Anchored = false |
4 | hit:Destroy() |
5 | end |
6 | end |
7 | script.Parent.Touched:connect(onTouched) |
I've also tried
1 | function onTouched(hit) |
2 | if hit.Name = = 'BuildingBrick' or hit.Anchored then |
3 | hit:Destroy() |
4 | end |
5 | end |
6 | 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:
1 | 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 |
2 | local parts = script.Parent:GetTouchingParts() |
3 | for i, part in pairs (parts) do |
4 | if part.Name = = "BuildingBrick" and part.Anchored then |
5 | part.Anchored = false |
6 | part:Destroy() |
7 | end |
8 | end |
9 | end |