How to Remove All Tools from Backpack on Teleport back to Lobby?

My teleport function works but players are retaining tools they collected from the Map.
How do I alter my script to remove all tools when returning to the lobby?
I’ve tried a few methods found in the forums and videos, but nothing works yet.
Any help greatly appreciated. I’m a novice to scripting.
Here’s what I have ATM

function teleportBack()
	local gear = game.Players.LocalPlayer.Backpack:GetChildren()
	local players = game.Players:GetPlayers()
	for i,v in pairs(players) do
		v.Character.HumanoidRootPart.CFrame = spawner.CFrame
		end
	for i,v in pairs(gear) do
		gear:Destroy()
		end
2 Likes
function teleportBack()
    local Players = game:GetService("Players")
    for _, Player in pairs(Players:GetPlayers()) do
        local Char = Player.Character or Player.CharactedAdded:Wait()
        Char:SetPrimaryPartCFrame(spawner.CFrame)
        local GearInPlayer = Player:FindFirstChild("Backpack")
        local GearInCharacter = Char:GetChildren()
        GearInPlayer:ClearAllChildren()
        for _, Tool in pairs(GearInCharacter) do
            if Tool:IsA("Tool") then
                Tool:Destroy()
            end
        end
    end
end)

I hope that works.

2 Likes

Rather than going through and individually destroying each object in the players backpack with :GetChildren() you could do:

Player:WaitForChild("Backpack"):ClearAllChildren() -- It removes all the children of the backpack, now if you have scripts stored in the backpack for some reason, you may want to stick to using the for loop to check if the item is a tool.
1 Like

I used this as a separate function (calling after teleport and before deleting map) but still didn’t work.
Suggestion?

function removeItems()
	local players = game.Players:GetPlayers()
	for i,v in pairs(players) do
		players:WaitForChild("Backpack"):ClearAllChildren()
	end
end
1 Like

I tried separating this out as a separate function but still didn’t work. Thoughts?

function removeItems()
    local Players = game:GetService("Players")
    for _, Player in pairs(Players:GetPlayers()) do
        local Char = Player.Character or Player.CharactedAdded:Wait()
        local GearInPlayer = Player:FindFirstChild("Backpack")
        local GearInCharacter = Char:GetChilden()
        GearInPlayer:ClearAllChildren()
        for _, Tool in pairs(GearInCharacter) do
            if Tool:IsA("Tool") then
                Tool:Destroy()
            end
        end
    end
1 Like

This latest code looks better than your first–
I assume this is a Script and not a LocalScript as your first stab suggested.
Are your players killing themselves?
Make sure you aren’t fighting with the system adding stuff to the backpacks.
You might want to Connect to the Backback.ChildAdded Event to test if things are being added.

1 Like

You have a typo. Char:GetChilden() should be Char:GetChildren()

function removeItems()
    local Players = game:GetService("Players")
    for _, Player in pairs(Players:GetPlayers()) do
        local Char = Player.Character or Player.CharactedAdded:Wait()
        local GearInPlayer = Player:FindFirstChild("Backpack")
        local GearInCharacter = Char:GetChildren()
        GearInPlayer:ClearAllChildren()
        for _, Tool in pairs(GearInCharacter) do
            if Tool:IsA("Tool") then
                Tool:Destroy()
            end
        end
    end
end
10 Likes

It’s a script (not local) in ServerScriptService.
The players that die works fine.
The players that survive are still the problem.
There are no starter packs and only items collected from the map are remaining back at the lobby.
Is there another way the system would be adding? Seems like it’s just not removing from players that survive.

THANKS!
That’s all it was.
Works great now!
I got that script from a copy / paste… great lesson to always read through!!!

1 Like