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*
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)
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
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*