For cases like this, I typically create a reusable function that I run twice over. I obviously have vastly different use cases and don’t often or at all use the code below, but I’ll present it for the sake of answering the question. Essentially, it’s a conversion of your current code into a function that’s ran across two different containers.
I’d also clean up your table some. You’re holding unnecessary references to objects.
local backpack = player:WaitForChild("Backpack") -- Not inserted immediately
local character = player.Character or player.CharacterAdded:Wait()
local removableLaunchers = {"LauncherChicken", "LauncherDoge", "LauncherCat", "LauncherFish", "LauncherRodan", "LauncherDemo"}
local function purgeLaunchers(container)
for _, launcher in pairs(removableLaunchers) do
local tool = container:FindFirstChild(launcher)
if tool and tool:IsA("Tool") then
tool:Destroy()
end
end
end
purgeLaunchers(backpack)
purgeLaunchers(character)
In the instance that you simply want to remove all launchers outright, I’d forego the table and instead hanker towards string matching. If any tool has the name launcher in it, remove it. This simply changes the function a bit, the rest stays the same.
local backpack = player:WaitForChild("Backpack")
local character = player.Character or player.CharacterAdded:Wait()
local function purgeLaunchers(container)
for _, item in pairs(container:GetChildren()) do
if item:IsA("Tool") and item.Name:match("Launcher") then
item:Destroy()
end
end
end
purgeLaunchers(backpack)
purgeLaunchers(character)
And like so, you can get rid of equipped or unequipped launchers with the same code but different containers. If your aim is to remove all launchers, I prefer using the second code sample over the first because then you can get rid of all launchers without needing to manually update a table each time you add a new launcher to your game. Always go for options that are more scalable and automated.
You can also modify the purgeLaunchers function to pass a list of containers, to which the function will iterate through those containers and call the related code to remove them. I won’t show you how to do that, but it essentially just makes your code writing more of eye candy by doing:
purgeLaunchers({backpack, character})
-- purgeLaunchers{backpack, character} also works due to the way syntax is handled