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

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

Asked by 4 years ago

I have a GUI and an image button that is once pressed, it will give the player a "Shotgun" 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 there are multiple guns in the game like an AK and a sniper so it wouldn't be convenient to do a tool:destroy() for every gun in the game anyways here's the code

script.Parent.MouseButton1Click:Connect(function() --Click detector
        game.ReplicatedStorage.Shotguns.Shotgun:Clone().Parent =
         game.Players.LocalPlayer.Backpack --Shotgun Giver
        game.ReplicatedStorage.Pistols.Pistol3:Clone().Parent =
         game.Players.LocalPlayer.Backpack --Pistol Giver
        script.Parent.Parent.Visible = false --Sets the frame status to invisble
        game.SoundService.Barret_Equip.Playing = true --Plays reload sound effect
    end) --Note this is a *local script*

3 answers

Log in to vote
0
Answered by
Elyzzia 1294 Moderation Voter
4 years ago
Edited 4 years ago

you can just use the ClearAllChildren function to get rid of every tool in the backpack, like so

game.Players.LocalPlayer.Backpack:ClearAllChildren()

however, the player might also have a tool equipped, so if a tool is in their character, you'll need to destroy that too

local toolInCharacter = game.Players.LocalPlayer.Character:FindFirstChildWhichIsA("Tool")
if toolInCharacter then -- make sure it exists before destroying it
    toolInCharacter:Destroy()
end

in your script that would look like this

local Players = game:GetService("Players") -- it's good practice to use GetService instead of directly indexing the game
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SoundService = game:GetService("SoundService")
local LocalPlayer = Players.LocalPlayer -- you reference the localplayer a lot, so it's good to use a variable instead

script.Parent.MouseButton1Click:Connect(function() --Click detector
    -- clear tools
    LocalPlayer.Backpack:ClearAllChildren()
    local toolInCharacter = LocalPlayer.Character:FindFirstChildWhichIsA("Tool")
    if toolInCharacter then -- make sure it exists before destroying it
        toolInCharacter:Destroy()
    end
    -- clone the new guns
    ReplicatedStorage.Shotguns.Shotgun:Clone().Parent = LocalPlayer.Backpack
    ReplicatedStorage.Pistols.Pistol3:Clone().Parent = LocalPlayer.Backpack
    script.Parent.Parent.Visible = false
    SoundService.Barret_Equip:Play() -- in most cases you should use the :Play function instead of the .Playing property
end)
0
Omg TYSM Ziannea, you just answered my problem, I wanted this answer for so long. and again I thank you for your comment Smartics 25 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

I'll try and answer this. Basically you will need to get the children of your backpack, like so:

ItemsInBackPack = game.Players.LocalPlayer.Backpack:GetChildren()

Then you will need to destroy the children:

ItemsInBackPack = game.Players.LocalPlayer.Backpack:GetChildren()
ItemsInBackPack:Destroy()
-- You don't need to use variables for the backpack's children, just making it more clear.

It's quite simple, here is your full code:

script.Parent.MouseButton1Click:Connect(function() --Click detector
    ItemsInBackPack = game.Players.LocalPlayer.Backpack:GetChildren()
    ItemsInBackPack:Destroy()

        game.ReplicatedStorage.Shotguns.Shotgun:Clone().Parent =
         game.Players.LocalPlayer.Backpack --Shotgun Giver
        game.ReplicatedStorage.Pistols.Pistol3:Clone().Parent =
         game.Players.LocalPlayer.Backpack --Pistol Giver
        script.Parent.Parent.Visible = false --Sets the frame status to invisble
        game.SoundService.Barret_Equip.Playing = true --Plays reload sound effect
    end) --Note this is a *local script*

I am not sure if this will work on a localscript, but you will have to try for yourself, happy to help!

If you have any other things you need add me on Discord: Haunted#5196

0
I send a friend request to your discord check that out! Smartics 25 — 4y
Log in to vote
0
Answered by 4 years ago

Use the :ClearAllChildren function.

script.Parent.MouseButton1Click:Connect(function() --Click detector
    game.Players.LocalPlayer.Backpack:ClearAllChildren()

        game.ReplicatedStorage.Shotguns.Shotgun:Clone().Parent =
         game.Players.LocalPlayer.Backpack --Shotgun Giver
        game.ReplicatedStorage.Pistols.Pistol3:Clone().Parent =
         game.Players.LocalPlayer.Backpack --Pistol Giver
        script.Parent.Parent.Visible = false --Sets the frame status to invisble
        game.SoundService.Barret_Equip.Playing = true --Plays reload sound effect
    end) --Note this is a *local script*
0
Omg tysm, Your script Worked!!, But weapons that are in the player hands don't get removed! you have a solution for that??? Smartics 25 — 4y

Answer this question