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

How to remove everything in the players backpack?

Asked by
wjs3456 90
10 years ago

I want to make it so that it will remove all the things in a players backpack. Not really sure how to do it though.

for i,v in pairs(game.Players:GetPlayers()) do
            w = v.Backpack:GetChildren()
            for i = 1,#w do
            w[i]:remove()
            end
        end

It also looks like it only removes one of the items if that helps. Thanks

0
First, remove is depreciated. Destroy is preferred. That could be your problem, because I can't see anything else wrong. GoldenPhysics 474 — 10y
0
Depracated functions are not a problem. I just returned to Roblox after a lengthy absence, during which remove was deprecated, and I've been using it as I normally would with no issues. ipiano 120 — 10y

2 answers

Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
10 years ago

I don't see anything wrong with yours. It is removing all tools/hopperbins in everyones backpack though.. Try this.

for i,v in pairs(game.Players:GetChildren()) do --Get all children of players
    Backpack = v:FindFirstChild("Backpack",true)
    if Backpack ~= nil then  --Check if the child has a backpack
        c = Backpack:GetChildren() --Get the children of the backpack
        for i = 1,#c do
            c[i]:Destroy() --Use Destroy method instead
        end
    end
end)

Again, I don't know what was wrong with yours but I hope this fixes it.

Hope I Helped

+1

Ad
Log in to vote
0
Answered by
Lacryma 548 Moderation Voter
10 years ago

A better way to do this:

for _,v in pairs(game.Players:GetPlayers()) do
    v.Backpack:ClearAllChildren() 
end

ClearAllChildren

0
I somehow wandered into this code, thanks for it! I needed it for my criminal laser kill for my jail game! RobloxGameingStudios 145 — 5y

Answer this question