Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Removing a Tool From Player Backpack??

How can i get it to remove only 3 tools i want it to remove

for i,v in pairs(game.Players:GetChildren()) do --Get all children of players
    Backpack = v:FindFirstChild("Backpack",true)
    if Backpack ~= nil then  --Check if the child has a backpack
        c = Backpack:GetChildren() --Get the children of the backpack
        for i = 1,#c do
            c[i]:Destroy() --Use Destroy method instead
        end
    end
end)

1 answer

✓ Accepted answer

Here You are. EXAMPLE 1: You want to remove only some tools, defined by names It will remove "ToolName1" and "ToolName2" from BackPack

-- SETTINGS
local toremove = {"ToolName1", "ToolName2"} -- NAMES OF TOOLS TO REMOVE
local players = game.Players:GetChildren() -- HERE PLACE ARRAY HOLDING PLAYERS OR PLAYER INSTANCE WHICH WILL LOSE HIS TOOLS
-- SCRIPT
function RemoveFromBackpack(bp)
	for _, i in pairs(toremove) do
		if bp:FindFirstChild(i) then
			bp[i]:Destroy()
		end
	end
end

if type(players) == "table" then
	for _, i in pairs(players) do
		if i:FindFirstChild("Backpack") then
			RemoveFromBackpack(i.Backpack)
		end
	end
else
	if players:FindFirstChild("Backpack") then
		RemoveFromBackpack(players.Backpack)
	end
end

EXAMPLE 2: You want only to remove some count of tools It will remove 3 tools from backpack

-- SETTINGS
local count = 3 -- HOW MUCH TOOLS WILL BE REMOVED
local players = game.Players:GetChildren() -- HERE PLACE ARRAY HOLDING PLAYERS OR PLAYER INSTANCE WHICH WILL LOSE HIS TOOLS
-- SCRIPT
function RemoveFromBackpack(bp)
	for i = 1, count, 1 do
		if bp:GetChildren()[i] ~= nil then
			bp:GetChildren()[i]:Destroy()
	end
end

if type(players) == "table" then
	for _, i in pairs(players) do
		if i:FindFirstChild("Backpack") then
			RemoveFromBackpack(i.Backpack)
		end
	end
else
	if players:FindFirstChild("Backpack") then
		RemoveFromBackpack(players.Backpack)
	end
end

This discussion is preserved as it was. New answers, comments, and votes are closed.