Is there easier way to get rid of a player tools

Basically im trying to remove the character, but it keeps the tools in the backpack. i tried doing a GetChildren but if you hold out a tool it doesnt delete till you unequip it

Is there a better way to get rid of the players tools then im what im currently doing

Code
-- Holds the main stuff for the game --
local Players = game.Players

Players.CharacterAutoLoads = false
local MenuGui = game.StarterGui.Main

if game.StarterGui:FindFirstChild("Main") then
	MenuGui.Parent = game.ReplicatedStorage
end
--                                   --

function OnSpawnIn(Gui, Player, Character)
	
end

function AddGui(Player)
	local gui = MenuGui:Clone()
	gui.Parent = Player.PlayerGui
end

function PlayerDied(Player)
	local BackPack = Player.Backpack:GetChildren()
	wait(2)
	Player.Character:Remove()
	
	for i, v in pairs(BackPack) do
		v:Destroy()
	end
	
	Player.PlayerGui.Main.Menu.Visible = true
end

function AddedCharacter(Character, Player)
	OnSpawnIn(Player.PlayerGui.Main, Player, Character)
	Player.PlayerGui.Main.Menu.Visible = false
	
	Character.Humanoid.Died:Connect(function()
		PlayerDied(Player)
	end)
end

function AddedPlayer(Player)
	AddGui(Player)
	
	Player.CharacterAdded:Connect(function(Character)
		AddedCharacter(Character, Player)
	end)
end

Players.PlayerAdded:Connect(AddedPlayer)


You could try:

function PlayerDied(Player)
	local BackPack = Player.Backpack:GetChildren()
        local character = Player.Character
	
	for i, v in pairs(BackPack) do
		v:Destroy()
	end

        for i, v in pairs(character:GetChildren()) do
        if v.ClassName == "Tool" then
        v:Destroy()
        end
	
	Player.PlayerGui.Main.Menu.Visible = true
end

Sorry the formatting is kinda messed up.

1 Like

Just use ClearAllChildren and check the character for a tool.

function PlayerDied(Player)
	Player.Backpack:ClearAllChildren()
	local chartool = Player.Character:FindFirstChildOfClass("Tool")
	
	if chartool then chartool:Destroy() end
	task.wait(2)
	Player.Character:Remove()
	
	Player.PlayerGui.Main.Menu.Visible = true
end
2 Likes

This is a better solution than mine, so go with this one. It essentially does the same thing, but it is more efficient.

1 Like