Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would I make this function unanchor and destroy?

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

7 comments

0

Are you CFraming BuildingBrick towards the other brick?

0

Yes.

0

Ok, look here: http://wiki.roblox.com/index.php?title=Touched Under the notes section, it states, that the touched event will not fire if a brick is being cframed through another brick

0

Ah, So this is just not possible?

0

Well it is, but you might need to do it a different way. An example would be to use a script to find all the edges of each block, and from there use a Changed() event to find out whether they're touching whenever the part is moved. There's probably more efficient ways to do it, but this is just an example.

0

Oh, I see Thanks!

0

Np :P

1 answer

✓ Accepted answer

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

2 comments

0

Thanks this is what i wanted !

0

Is there any chance this script could be modified for it to work while cancollide is set to false?

This discussion is preserved as it was. New answers, comments, and votes are closed.