for i,v in pairs(script.Parent:GetChildren()) do function click(clicker) clicker.Character.Torso.Anchored = true wait(2) v:Destroy() clicker.Character.Torso.Anchored = false end end script.Parent.ClickDetector.MouseClick:connect(click)
this is supposed to destroy a block after 2 seconds. instead, it destroys my click detector. the anchored parts work fine. its just that the click detector gets destroyed instead of the part i clicked
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 -- Check if the child is a block. clicker.Character.Torso.Anchored = true wait(2) v:Destroy() -- Destroy the block. If it isn't in script.Parent, you're using the for loop incorrectly. 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:
local model = script.Parent local deletescript = script.Parent.Delete -- You'll make this later. for i, v in pairs(script.Parent:GetChildren()) do -- Loop through the model.. if v:IsA("Part") then -- Check if the child is a part.. local click = Instance.new("ClickDetector", v) -- Make a clickDetector in each part. local delscript = deletescript:Clone() -- Clone the deletion script to each part. delscript.Parent = v -- Set the parent.. delscript.Disabled = false -- Enable the deletion script. end end
local click = script.Parent:WaitForChild("ClickDetector") -- Wait for the ClickDetector to exist click.MouseClick:connect(function(player) -- You already know what all of this does. 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.