Attempt to index nil with Destroy

Hi im new with scripting. I tried a lot of things. Can someone help me with this eror?

  • ServerScriptService.reb:28: attempt to index nil with ‘Destroy’]
local remote = repStorage:FindFirstChild("Rebirth") 


remote.OnServerEvent:Connect(function(plr, x)
	local basic = plr.Items:FindFirstChild("Basicfedora")
	local blue = plr.Items:FindFirstChild("Bluefedora")
	local darkblue = plr.Items:FindFirstChild("Darkbluefedora")
	local golden = plr.Items:FindFirstChild("Goldenfedora")
	local mafia = plr.Items:FindFirstChild("Mafiafedora")
	local Purple = plr.Items:FindFirstChild("Purplefedora")
	local red = plr.Items:FindFirstChild("Redfedora")
	local starter = plr.Items:FindFirstChild("Starterfedora")
	local white = plr.Items:FindFirstChild("Whitefedora")
	local dark = plr.Items:FindFirstChild("Darkfedora")
	local basicl = plr.Backpack:FindFirstChild("Basicfedora")
	local bluel = plr.Backpack:FindFirstChild("Bluefedora")
	local darkbluel = plr.Backpack:FindFirstChild("Darkbluefedora")
	local goldenl = plr.Backpack:FindFirstChild("Goldenfedora")
	local mafial = plr.Backpack:FindFirstChild("Mafiafedora")
	local Purplel = plr.Backpack:FindFirstChild("Purplefedora")
	local redl = plr.Backpack:FindFirstChild("Redfedora")
	local starterl = plr.Backpack:FindFirstChild("Starterfedora")
	local whitel = plr.Backpack:FindFirstChild("Whitefedora")
	local darkl = plr.Backpack:FindFirstChild("Darkfedora")
	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"
		basic:Destroy()
		blue:Destroy()
		darkblue:Destroy()
		golden:Destroy()
		mafia:Destroy()
		Purple:Destroy()
		red:Destroy()
		starter:Destroy()
		white:Destroy()
		dark:Destroy()
		basicl:Destroy()
		bluel:Destroy()
		darkbluel:Destroy()
		goldenl:Destroy()
		mafial:Destroy()
		Purplel:Destroy()
		redl:Destroy()
		starterl:Destroy()
		whitel:Destroy()
		darkl:Destroy()

	end
end)


2 Likes

Your script looks fine, I don’t notice any errors.

Check these:
Make sure that there is an “Items” folder in the player.
Make sure that the hats are actually in the Folder.

If both exist, try commenting out the 28th line and check for errors.

Tips:
Instead of spamming plr.Items:FindFirstChild(), you just local:

local folder = plr:WaitForChild("Items")

And then use folder:FindFirstChild() for all the locals.
Also, if you are just using the variables once, what’s the point of localling them? You can do this:

folder:FindFirstChild():Destroy()

If the locals are all the children in the folder, use this:

local remote = ReplicatedStorage:FindFirstChild("Rebirth")

remote.OnServerEvent:Connect(function(plr, x)
    local folder = plr:WaitForChild("Items")
    local leaderstats = plr:WaitForChild("leaderstats")
    if leaderstats.Coins.Value >= 500000 * (x.Value + 1) then
        x.value = x.Value + 1
        leaderstats.Coins.Value = 0
        leaderstats.Energy.Value = 0
        leaderstats.Gems.Value = leaderstats.Gems.Value + 1000
        plr.ItemEquipped.Value = "None"
        for _,child in pairs(folder:GetChildren()) do
        child:Destroy()
    end
end)

Have a good day! :smiley:

2 Likes

i got another eror. Expected ‘(‘,’(‘ or string, got ‘end’

1 Like

Which line is it in?

30 characters

it is on line 18. Can you help?

You need to provide the new script for that. Could you?

local repStorage = game:GetService("ReplicatedStorage")
local remote = repStorage:FindFirstChild("Rebirth") 


remote.OnServerEvent:Connect(function(plr, x)
	local leaderstats = plr:WaitfoChild("leaderstats")
	local basic = plr:WaitForChild("Basicfedora,Bluefedora,Darkbluefedora,Goldenfedora,Mafiafedora,Purplefedora,Redfedora,Starterfedora,Whitefedora,Darkfedora")
	if leaderstats.Coins.Value >= 500000 * (x.Value + 1) then
		x.Value = x.Value + 1
	    leaderstats.Coins.Value =  0
		leaderstats.Energy.Value =  0
		leaderstats.Gems.Value = plr.leaderstats.Gems.Value + 1000
		plr.ItemEquipped.Value = "None"
		for _,child in pairs(basic:GetChilderen()) do
		child:Destroy
		
	end
end)

Well, Destroy() is a function, which means that you need to use parenthesis after calling it. All functions need to have parenthesis to call them. It doesn’t matter if you want to pass a variable to the function or not.

So you need to replace Destroy with Destroy().

Have a good day! :smiley:

1 Like

Thanks, I learned a lot today.

if it worked, you can mark this topic as solved.
Happy learning! :smiley:

i got a warning
Infinite yield possible on 'Players.OfficialAjoeb:WaitForChild(“Basicfedora,Bluefedora,Darkbluefedora,Goldenfedora,Mafiafedora,Purplefedora,Redfedora,Starterfedora,Whitefedora,Darkfedora”)

You can not use the WaitForChild() function for multiple items. What you have done is:
:WaitForChild(“Multiple,Items,Here”). Now, when you are executing this script, the system thinks that “Multiple,Items,Here” is one single item, and it begins searching for it in the specified destination, i.e. the folder named “Items” in your player. What you can do is set a table at the beginning of the script, and then run a loop for finding all the items with the name of the table’s contents, and destroying them.

Hope this helped!
Have a good day! :smiley:

Sorry if I ask dumb questions. I am new to scripting. How should I make a table?

local table = {
"item1",
"item 2"
}

This is how you define a table.
For more information, I suggest you read the API reference to Tables.

1 Like

When using FindFirstChild(), what I am assuming is that you don’t want it to error if the item doesn’t exist. You need to do a check to make sure it exists before destroying it, what FindFirstChild() does is that it finds the first child by name, and if there is no child with the name you provided, it will return nil, so if the part doesn’t exist, but you think it does exist, you are indexing “nil” with “Destroy”, which is what the error is about.

if plr.Backpack:FindFirstChild("Darkfedora") then
	plr.Backpack.Darkfedora:Destroy() -- if the part exists, destroy it
end
1 Like

This doesn’t work

local table = {

"Basicfedora",

"Bluefedora",

"Darkbluefedora",

"Goldenfedora",

"Mafiafedora",

"Purplefedora",

"Redfedora",

"Starterfedora",

"Whitefedora",

"Darkfedora"

}

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"

table:Destroy()

end

end)

I told you to read the API reference to tables, didn’t you?

It works, but what it does is it destroys the table in the script. What you have to do is:

local items = {
    "item1",
    "item2"
}
for i, child in pairs(plr:WaitForChild("Items"):GetChildren()) do
    if child.Name == items[i] then
        child:Destroy()
    end
end

Think about it in real life. Imagine you get a to-do list from your boss at work, and you are supposed to do the work. There are some items which you have already done. So, what you do is you go through the list, and strike off what you have done, not tear the to-do list, right?

That logic applies here too; you need to check if items of the table are there in the player, and then destroy them, not destroy the table itself.

This should work. Happy coding! :smiley:

1 Like

Thanks a lot for helping. Is this script good? Because it still doesnt work.

local items = {
"Basicfedora",
"Bluefedora",
"Darkbluefedora",
"Goldenfedora",
"Mafiafedora",
"Purplefedora",
"Redfedora",
"Starterfedora",
"Whitefedora",
"Darkfedora"
}

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"
		for i, child in pairs(plr:WaitForChild("Items"):GetChilderen()) do
			if child.Name == items[i] then
				child:Destroy()
			end
		end
	end
end)

Are you sure that the values stored in the table and the name of the children in the “Items” folder’s names match? Even the capitalisations? It’s weird because I just tested it and it worked for me.

yes im sure.Knipse