✓ Accepted answer
Well, that's because you told it to.
According to line 10, the ClickDetector is in the script's parent. In the for loop, you said to destroy script.Parent's children, which includes the ClickDetector. Telling the script to destroy its creator.
It's oddly poetic.
You should be telling it to destroy the block. You didn't specify where it is, so until you correct me, I'm assuming it's in script.Parent.
Also, it's out of order. The function should include the for loop, not the other way around:
function click(clicker)
for i,v in pairs(script.Parent:GetChildren()) do
if v:IsA("Part") then
clicker.Character.Torso.Anchored = true
wait(2)
v:Destroy()
clicker.Character.Torso.Anchored = false
end
end
end
script.Parent.ClickDetector.MouseClick:connect(click)
EDIT: Alright, turns out what I said in the chat was a completely different thing. Good news, though, what you want done can be achieved, if you follow these simple instructions:
- Inside of the model with the parts in it, make a script.
Doesn't matter what it's named. That script's contents should be:
local model = script.Parent
local deletescript = script.Parent.Delete
for i, v in pairs(script.Parent:GetChildren()) do
if v:IsA("Part") then
local click = Instance.new("ClickDetector", v)
local delscript = deletescript:Clone()
delscript.Parent = v
delscript.Disabled = false
end
end
- Next, make another script inside of the model. Make sure this script is disabled, and that its name is "Delete". This one's contents:
local click = script.Parent:WaitForChild("ClickDetector")
click.MouseClick:connect(function(player)
player.Character.Torso.Anchored = true
wait(2)
script.Parent:Destroy()
player.Character.Torso.Anchored = false
end)
This should give you the effect you desire. Hope this helped.
1 comment
Alright, answer edited. Next time, try to be a bit more clear about your goal.