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

Limit Backpack to One of the Same Tool?

Asked by 8 years ago

So I am stuck on this problem. I am trying to make it so a player only has one of "iSword" and will never have a duplicate of that tool. Code:

while true do
    plyr = game.Players:GetChildren()
    if plyr.Backpack:findFirstChild("iSword") ~= nil then

    end
end

Please help! Thanks.

1 answer

Log in to vote
0
Answered by
Sparker22 190
8 years ago

A loop is pretty inefficient for this kind of thing.

So, a Player's tools are stored in their backpack. So we can use the ChildAdded event. It looks like you're using a server script so I will attach the event to all players, but i'd recommend doing it on the client if you can.

game.Players.PlayerAdded:connect(function(Player)
    Player.Backpack.ChildAdded:connect(function(Child)
        for i,v in pairs(Player.Backpack:GetChildren()) do
            if not v == Child and v.Name == Child.Name then
                Child:Destroy()
            end
        end
    end)
end)

When a player enters a game it attaches a ChildAdded event to their backpack. This event fires when a item gets put into the backpack, so a tool will trigger it. This triggers a loop that will check the names of each item in the Backpack and if the Child's name matches any tool then the Child is deleted.

0
Oh my gosh! This is so helpful! Is there anyway to make it so they can one of Child but any further duplicates will be deleted? intrance 50 — 8y
0
Yeah, this is good User#5978 25 — 8y
0
This should prevent duplicates from the start. When a tool is added to the Backpack, it checks all other tools against that tool. If a duplicate exists then the new tool is deleted so duplicates should never happen. Sparker22 190 — 8y
Ad

Answer this question