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

How do I make this function only delete bricks that touched it?

Asked by
262187 45
9 years ago

I have a function which is suppose to delete a specific named brick if it touches the brick which contains the script, But it deletes all bricks containing that name in the workspace and I just need the function to delete the one brick that touched it. Please help!

Here's the script:

1function onTouched()
2c = game.Workspace:GetChildren()
3for i = 1, #c do
4if c[i].Name == "BuildingBrick" then c[i].Parent = nil end -- How can I make it so it will only delete the brick that touched it and not all bricks named "BuildingBrick"
5end
6end
7script.Parent.Touched:connect(onTouched)

1 answer

Log in to vote
2
Answered by
drew1017 330 Moderation Voter
9 years ago

2 things:

Setting parent to nil is the equivelant of the Remove method from alot earlier and it's very unstable. Use :Destroy() for removing things instead.

function onTouched(name) <-- The ''name'' variable for Touched functions is a reference to the part that touched the brick and thus triggered the function. You can also use this to make the code more compact.

1function onTouched(hit)
2if hit.Name == 'BuildingBrick' then
3hit:Destroy()
4end
5end
6script.Parent.Touched:connect(onTouched)
0
Thanks! 262187 45 — 9y
Ad

Answer this question