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

How do you clear a player's backpack/inventory completely?

I have a GUI and an image button that is once pressed, it will give the player an "AR" and a "Pistol", So the players don't abuse it and get infinite guns, How could I Make it so that it clears the player's backpack completely and then gives the player the two guns, I can't just say tool:destroy()

because I have multiple guns in the game so it wouldn't be convenient to do a remove a gun for every single gun in the game here's the code

script.Parent.MouseButton1Click:Connect(function() --Click detector
	game.Lighting.AssualtRifles.AR:Clone().Parent =
	 game.Players.LocalPlayer.Backpack --Ar Giver
	game.Lighting.Pistols.Pistol:Clone().Parent =
	 game.Players.LocalPlayer.Backpack --Pistol Giver
end)

2 answers

✓ Accepted answer

Easy to do! No need to delete the tool and give them again! You can check if the player has the item equipped or in the backpack before parenting the tools into the player's backpack!

local Player = game.Players.LocalPlayer

local BackPack = Player:WaitForChild("Backpack")

script.Parent.MouseButton1Click:Connect(function() --Click detector
 
if BackPack:FindFirstChild("AR") or BackPack:FindFirstChild("Pistol") then
      return -- Doesn't give the tool if it's in the backpack

elseif Player.Character:FindFirstChild("AR") or Player.Character:FindFirstChild("Pistol") then
      return -- Doesn't give the tool if equipped
end

    
  game.Lighting.AssualtRifles.AR:Clone().Parent = BackPack --Ar Giver
  game.Lighting.Pistols.Pistol:Clone().Parent = BackPack --Pistol Giver

end)

Also store your tools in ReplicatedStorage and note that the tool will only show up to the player, not other players since it's local.

1 comment

0

Omg Thank you! I never though about that idea tysm

You can delete all tool by For i loop (i think that is what it called):

local PlayerBackPack = -- Backpack
for i,v in pairs(PlayerBackPack:GetChildren()) do
	v:Destroy()
end

2 comments

0

It's called a "for i" loop but yeah, you have the right idea.

0

It's not that efficient. The better is to check if the player has the tool equipped or in the backpack.

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