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.
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)?
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.
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