Trying to make a GUI Buttons that clear inventory

Can’t seem to make a GUI button clear the localplayer inventory

local player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
	player.Parent.Humanoid:UnequipTools()
end)

I know I am doing something wrong here, but what is it?
Thanks!

local player = game.Player.LocalPlayer

local button = <Button here>

button.MouseButton1Click:Connect(function()
   for _, tool in ipairs(player.Backpack:GetChildren()) do
      tool:Destroy()
   end
end)

If you want to remove all the tools from the player’s backpack, then you can try this:
We will need to loop through both, the player’s backpack and the player’s character to find any tools - This is because whenever the player equips a tool, the tool will be parented under the character. Otherwise, it will be parented under the player’s backpack.

local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer

local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local Backpack = LocalPlayer:WaitForChild("Backpack")

local button = <Button here>

Button.Activated:Connect(function()
	for _, child in ipairs(Backpack:GetChildren()) do
		if child:IsA("Tool") then
			child:Destroy()
		end
	end
	
	for _, child in ipairs(Character:GetChildren()) do
		if child:IsA("Tool") then
			child:Destroy()
		end
	end
end)

I also suggest doing this on the server because to my knowledge, if you try to remove tools from the player’s backpack on the client, it won’t be replicated to the server. That means the server and the other players will still see the tools in either the player’s backpack or in the player’s hand.

If you are gonna try to do this on the client, you can use remote events to get the player.

2 Likes

I suggest his method, I forgot to do the equipped tool.

1 Like

THANKS! That was really useful, you’re a genius.
I will now try to understand how this script works ha!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.