Attempt to index nil with Destroy

I clearly had mentioned that the above code was not for you to copy-paste, it was a pseudo-code.

I tried this to stop the loop. still doesnt work.

for i, v in ipairs(folder:GetChildren())do
	if table.find(testTable, v.Name)then
		v:Destroy() 
		wait(1.5)
			break
end

That would make the loop run only once, wait for 1.5 seconds and finally break. That’s not what you are supposed to do.

And this?

for i, v in ipairs(folder:GetChildren())do
	local toggle = false
	if table.find(testTable, v.Name)then
		v:Destroy() 
		if v:Destroy()then
			break
end

You don’t have to do if v:Destroy() then break, as it would break the loop. As I said before, it would run the loop only once. What you do is replace those two lines with v:Destroy().
Also, add the end blocks wherever necessary.

1 Like

I am confused.Can you send the script with an explanation?

Here it is

local items = { -- we store a dictionary of all the items we need
	folder1 = { -- name the keys the name of the folders, we will store an array for the items
		"item1", -- put all your names you want to destroy here
		"item2"
	},
	
	folder2 = { -- same as above
		"item3",
		"item4"
	}
}

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RebirthEvent = ReplicatedStorage:WaitForChild("Rebirth")

RebirthEvent.OnServerEvent:Connect(function(plr, x)
	if plr.leaderstats.Coins.Value >= 500000 * (x.Value + 1) then
		x.Value = x.Value + 1
		for FolderName, Names in pairs(items) do -- we loop through all the keys in the dictionary
			for i, v in ipairs(plr[FolderName]:GetChildren()) do -- loop through the children, note that the keys in the dictionary must be the name of the folder to work
				if table.find(Names, v.Name) then -- see if the name exists in the table
					v:Destroy() -- if it does, destroy it
				end
			end
		end
	end
end)

** Does the loop stop after the destroy?**

No, the loop won’t stop after destroy, it will loop through everything defined and then stops. The only time a loop won’t stop is if the condition is either always true or never nil.

while true do
	-- the loop will never stop because the condition is always true
end

while 1 == 1 do
	-- same as above
end

while wait(0.1) do
	-- this loop never stops because "wait" returns two values, which are never "nil"
end

Thanks it works! There is another problem. What if the player equips the tool. How do I make it destroyed too.

Search for the tools in the character, you can store the items in a table (look at my edit for a better solution)

local tools = {
	-- your items here
}

for i, v in ipairs(plr.Character:GetChildren()) do -- loop through the character's items
	if table.find(tools, v.Name) then -- see if the name exists in the table
		v:Destroy() -- if it does, destroy it
	end
end

for i, v in ipairs(plr.Backpack:GetChildren()) do -- same as above, but we loop through the backpack, so it destroys tools when the player hasn't equipped them
	if table.find(tools, v.Name) then
		v:Destroy()
	end
end

Edit: Here is a better solution, loop through the items in the table, then find if it exists in either the backpack or the player’s character, takes less lines of code and you only need one loop instead of 2, however, keep the table the tools are in separate from the table the other items are in, since the tool isn’t necessarily a descendant of the player

local tools = {
	-- your items here
}

for i, v in ipairs(tools) do -- loop through the items in the table
	local tool = plr.Backpack:FindFirstChild(v.Name) or plr.Character:FindFirstChild(v.Name) -- find the tool, it is either in the backpack or the player's character
	if tool then
		tool:Destroy() -- if the tool was found, destroy it
	end
end
1 Like