I'm developing a roblox game and I only want the player to hold one item at a time. Whenever I use this code it clears all the items currently deselected:
player.Backpack:ClearAllChildren().
However, if the player is holding an item it won't clear that item!
Anyone know a solution?
COMMUNITY QUESTION
How can I clear all the inventory of a player, including the selected item?
2 answers
The thing is, the item you are holding does not show up in the backpack, it shows up in your character, so you can use this to clear the tool they are holding.
local holdingtool = player.Character:FindFirstChildOfClass("Tool")
holdingtool:Destroy()
This script, once in a part, when touched, deletes all tools from the backpack and that of the player has in hand.
local part = script.Parent
local debounce = false
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)
part.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") and debounce == false then
for i,v in pairs(char:GetChildren()) do
if v.ClassName == "Tool" then
v:Destroy()
end
end
for i,v in pairs(player.Backpack:GetChildren()) do
if v:IsA("Tool") then
v:Destroy()
debounce = true
end
end
end
end)
end)
end)
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
player.Character:FindFirstChildOfClass("tool"):Destroy()