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

help with i,v in pairs?

Asked by
theCJarmy7 1293 Moderation Voter
8 years ago
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

0
Alright, answer edited. Next time, try to be a bit more clear about your goal. Pyrondon 2089 — 8y

1 answer

Log in to vote
2
Answered by
Pyrondon 2089 Game Jam Winner Moderation Voter Community Moderator
8 years ago

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:

  • 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 -- 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
  • 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") -- 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.

0
well now it just deletes a random block inside the model, and it never unanchors me theCJarmy7 1293 — 8y
0
Well, what is the block you're trying to destroy? I said you didn't specify. Pyrondon 2089 — 8y
0
the block im trying to destroy is whatever block i click theCJarmy7 1293 — 8y
Ad

Answer this question