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

Created Instance wont destroy NewInstances? "TextButton.LocalScript attempt to call a nil value?"

Asked by 3 years ago

It has something to do with line 17 - 20 with the error of "Players.snipperdiaper.PlayerGui. NewInstances!.TextButton.LocalScript:20: attempt to call a nil value"

local Button = script.Parent
local Spawning = false
Button.MouseButton1Click:Connect(function()

    if Spawning then
        while true do
            wait(0.2)
            local NewPart = Instance.new("Part")
            Spawning = true
            NewPart.Name = "NewDestruction"
            NewPart.Parent = workspace


        end


    else
        Spawning = false
        local DestroyP = game.Workspace:GetChildren("NewDestruction")
        DestroyP:Destroy()
    end
    end)

-Thanks in advance, snipperdiaper

2 answers

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

Fixed code:

local Button = script.Parent
local Spawning = false

Button.MouseButton1Click:Connect(function()
    if Spawning then
        while true do
            wait(0.2)
            local NewPart = Instance.new("Part")
            Spawning = true
            NewPart.Name = "NewDestruction"
            NewPart.Parent = workspace
        end
    else
      local DestroyP = workspace:GetChildren()
      for index, value in ipairs(DestroyP) do
         if value.Name == "NewDestruction" and value:IsA("Part") then
           value:Destroy() 
        end
     end
   end
end)

line 14: :GetChildren() will return a table specifically an array.

line 15: ipairs is used for arrays, and pairs is used for dictionaries.

line 16:A sanity check to check if value has a name "NewDestruction" and checks if it's a Part.

line 17: It'll run some code for each iteration of the array which is :Destroy().

I'm curious what you're trying to do because the while loop will loop forever yielding the current thread.

0
Okay so I was trying to do a loop while the spawning was true (which would keep spawning the bricks until it was false) which I tried to fix but it didnt work. snipperdiaper 120 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

The reason is because GetChildren() doesn't take any parameters. If you wanted to delete everything in a model, you could either delete the whole model, or iterate through the model's children. This is how you would do the second option.

local model = game.Workspace:FindFirstChild("NewDestruction")
if model then
    for i, part in pairs(model:GetChildren()) do
        part:Destroy()
    end
end

Hope this helps

0
pairs is used for dictionaries, and ipairs is used for arrays. :GetChildren() returns an array MarkedTomato 810 — 3y
0
He wants to destroy objects in the workspace that are named "NewDestruction" MarkedTomato 810 — 3y

Answer this question