Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 4 years ago

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

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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.

0
Omg Thank you! I never though about that idea tysm Smartics 25 — 4y
Ad
Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

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
0
It's called a "for i" loop but yeah, you have the right idea. matiss112233 258 — 4y
0
It's not that efficient. The better is to check if the player has the tool equipped or in the backpack. SilentsReplacement 468 — 4y

Answer this question