:Destroy() doesn't work

local AllTools = {
	StressBall = ShopTools["Stress Ball"],
	CheeseSteak = ShopTools["Cheese Steak"],
	BaconHairDrink = ShopTools["Bacon Hair Drink"],
	Hammer = ShopTools.Hammer,
	FriendlyTeddy = ShopTools["Friendly Teddy"],
	Prime = ShopTools.Prime,
}


function Equip(tool, toolName)
	if OwnedTools:FindFirstChild(toolName) then
		print("Is in OwnedTools") -- prints
		
		for i, v in pairs(Character:GetChildren()) do
			if table.find(AllTools, v) then
				v:Destroy()
			else
				print("v = nil") -- this prints, but it isnt nil??
			end
		end
		
		for i, v in pairs(LocalPlayer.Backpack:GetChildren()) do
			if table.find(AllTools, v) then
				v:Destroy()
			else
				print("v = nil") -- this prints, but it isnt nil??
			end
		end
	
		print("Passed For Loops") -- prints
		local toolClone = tool:Clone() -- prints
		toolClone.Parent = LocalPlayer.Backpack
		print("Cloned") -- prints
	end
end

I’m trying to destroy any tools that might be inside of the character/backpack before the player equips another.

But the destroy() doesn’t work, and instead it prints v = nil (my print) but the tool is not nil, it gets cloned into my backpack so it shouldn’t be nil.

Any help is appreciated

2 Likes

Where are you calling it in the localscript or a server sided script?

1 Like

Why use :Destroy() when you could just equip and unequip the tools using Humanoid:EquipTool(ToolInstance) and Humanoid:UnequipTools() (unless if that is not what you are trying to achieve)?

2 Likes

It’s a local script located inside a gui

The main idea of this script is to have only 1 inventory slot, and have all the tools that the player purchased in a folder. So every time the player would press a text button, the Equip() function fires.

(hope that clarifies things)

Looking at your code again, I believe you should be looking for the Tool names when using table.find, and also change the AllTools table to have strings of the names of each tool (if you are using that table for other purposes, just create another table for the strings).

Try using this updated code:

local AllToolsNames = {
	"Stress Ball",
	"Cheese Steak",
	"Bacon Hair Drink",
	"Hammer",
	"Friendly Teddy",
	"Prime"
}


function Equip(tool, toolName)
	if OwnedTools:FindFirstChild(toolName) then
		print("Is in OwnedTools") -- prins

		for i, v in pairs(Character:GetChildren()) do
			if v:IsA("Tool") and table.find(AllToolsNames, v.Name) then
				v:Destroy()
			else
				print("v = nil") -- this prints, but it isnt nil??
			end
		end

		for i, v in pairs(LocalPlayer.Backpack:GetChildren()) do
			if v:IsA("Tool") and table.find(AllToolsNames, v.Name) then
				v:Destroy()
			else
				print("v = nil") -- this prints, but it isnt nil??
			end
		end

		print("Passed For Loops") -- prints
		local toolClone = tool:Clone() -- prints
		toolClone.Parent = LocalPlayer.Backpack
		print("Cloned") -- prints
	end
end
1 Like

wow, that was fast!

it works perfectly, thank you!

1 Like

Oops i am sorry i didnt realise you replied i hope you enjoy the solution that @BabyNinjaTime gave you! :slight_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.