Attempt to index nil with Destroy

i found a eror: GetChilderen is not a valid member of Folder

You wrote the wrong spelling of GetChildren().
Also, the code I sent you above is probability-based, so it won’t always work, that’s what I have found out.

My new test script
function tableHasKey(table,key) --Copy this function to the top of your script.
    return table[key] ~= nil
end

local folder = Instance.new("Folder")
local test1 = Instance.new("StringValue")
test1.Name = "Test1"
test1.Parent = folder

local test2 = Instance.new("StringValue")
test2.Name = "Test2"
test2.Parent = folder

local testTable = {
	["Test1"] = "test1", --Follow the same format while adding more stuff to the table.
	["Test2"] = "test2" --The text after the "=" can be anything because it's just to not cause errors.
}

while wait(5) do

	for i,v in pairs(folder:GetChildren()) do
		if tableHasKey(testTable,v.Name) then --change the "testTable" to your table's name.
			print("It works!") -- Remove this and the else block too and just replace it with v:Destroy()
		else
			print("It doesn't work.")
		end
	end

end

Feel free to use my above code and alter it as required.

P.S. I have tested it, so there should not be any errors.

This is my script. No errors. It still doesn’t work.


function tableHasKey(table,key) --Copy this function to the top of your script.
    return table[key] ~= nil
end

local folder = Instance.new("Folder")
local test1 = Instance.new("StringValue")
test1.Name = "Basicfedora"
test1.Parent = folder

local test2 = Instance.new("StringValue")
test2.Name = "Bluefedora"
test2.Parent = folder

local testTable = {
	["Basicfedora"] = "Basic", --Follow the same format while adding more stuff to the table.
	["Bluefedora"] = "Blue" --The text after the "=" can be anything because it's just to not cause errors.
}
local repStorage = game:GetService("ReplicatedStorage")
local remote = repStorage:FindFirstChild("Rebirth") 


remote.OnServerEvent:Connect(function(plr, x)
	if plr.leaderstats.Coins.Value >= 500000 * (x.Value + 1) then
		x.Value = x.Value + 1
	    plr.leaderstats.Coins.Value =  0
		plr.leaderstats.Energy.Value =  0
		plr.leaderstats.Gems.Value = plr.leaderstats.Gems.Value + 1000
		plr.ItemEquipped.Value = "None"
		while wait(5) do
for i,v in pairs(folder:GetChildren()) do
		if tableHasKey(testTable,v.Name) then --change the "testTable" to your table's name.
			v:Destroy()
		end
	end
   end
end
end)

Check if the Basicfedora and the Bluefedora have been destroyed.

Just a suggestion, instead of

local remote = repStorage:FindFirstChild("Rebirth")

do

local remote = repStorage:WaitForChild("Rebirth") 

or

local remote = repStorage.Rebirth

Also, instead of using a dictionary

local testTable = {
	["Basicfedora"] = "Basic",
	["Bluefedora"] = "Blue"
}

Use an array, and then, you can use the table.find() function to see if it exists

local testTable = {
	"Basicfedora",
	"Bluefedora"
}

And when destroying them

for i, v in ipairs(folder:GetChildren()) do -- use ipairs instead of pairs, pairs is good if you have string keys or "nil" in your table, but in this case, ipairs will be better
	if table.find(testTable, v.Name) then
		v:Destroy()
	end
end

Another thing,

if plr.leaderstats.Coins.Value >= 500000 * (x.Value + 1) then

In this line, it checks if the leaderstats are at a value, but, if the condition isn’t met, any code between the then and the end for the if statement won’t run, are you sure the condition is met and the script executes those lines of code?

I also have similar names with me tools are they alo getting destroyed?

No they are still not destroyed.

They won’t be destroyed unless they have the same names and they are direct children of the folder

Okay thanks. I’ll test it tomorrow.

@FastKnight401 Thanks soo much!! it works. Is there a way to do it with 2 folders?

And can I destroy it once? instead of loop destroy.

You can mark it as solved. I’m glad I was a part of the discussion! :smiley:

Yes, you can do it for two different folders; you need to make two different tables, and run for loop for each one of them.

Destroying all at once is never possible; even if you spammed:

item1:Destroy()
item2:Destroy()
item3:Destroy()
item4:Destroy()
item5:Destroy()

It is clearly because the script goes to the next line only after it finishes the previous line.
For loop also does the same thing, but it makes your code more legible and easy to understand. Therefore, a for loop, in this case is the best choice.

Okay but it is possible. To stop the loop? Because I have an Item shop. And if I buy a new tool it destroys.

Well, you can add a boolValue to check if the loop should run or not.
Here’s my pseudo-code.

local toggle = false --Locals the boolValue
for loop
    if not toggle then --Checks if the boolValue is false
        print("The loop will run here") --Your code (The code here will only run if the boolValue is false)
end

newTool.Added:Connect(function() --However you add your new tool inside the player
    toggle = true --Sets the boolValue to true, which means that the for loop will no more run.
end)

Hope this helps. :smiley:

Actually, you should avoid

thing:WaitForChild()

if you aren’t sure the object doesn’t exist, as far as I know. You can do this instead:

local var = thing.otherThing or thing:WaitForChild(otherThing)

which will only WaitForChild if thing.otherThing is nil or false, the first of which being the case you want to use WaitForChild as a fallback for.

This eliminates unnecessary yields and is what I personally use in my code. You can even write a GetChild helper function to shorten that down if you like.

You wouldn’t have to do that, :FindFirstChild() either gives the requested child or returns nil, avoiding errors.

i got a error. expected ‘in’ when parsing for loop, got ‘if’

I guess you wrote:

for i,v if pairs()

And the right one is:

for i,v in pairs()

Yes, but so long as thing does not equal nil the aforementioned statement works fine; simply run that statement to get thing before you run it for its children.

FindFirstChild as far as I know is just useful to avoid property name overlaps and largely does the same thing as parent.child for most use cases. I’ve only used it to locate items in Workspace without a reference if I’m not sure where the items will be, due to other scripts doing things.

1 Like

its fixed. I got another error. unknown global ‘newTool’