Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to limit 1 of each tool in a player's backpack?

Hi, i need help on how to fix this script. I have been making a game that has a shop. And some items go off sale. I am working on the game and sometimes, when the item goes off sale, i have 2 of the same tool. I tried making this script and put it in server script service. And it did not work. Can anyone help me?

here's the script:

game.Players.PlayerAdded:connect(function(Player)
    Player.Backpack.ChildAdded:connect(function(Child)
         if not v == Child and v.Name == Child.Name then
                Child:Destroy()
            end
        end
    end)
end)

1 comment

1

what is v?

2 answers

✓ Accepted answer

You could try this

game.Players.PlayerAdded:connect(function(Player)
    Player.Backpack.ChildAdded:connect(function(Child)
        for _, v in pairs(Player.Backpack:GetChildren()) do --Looping in backpack
            local CurrentTool = v --Saving a copy somewhere
            for _, tool in pairs(Player.Backpack:GetChildren()) do --Looping again but with the tool found
                if tool.Name == CurrentTool.Name then --If we find a match 
                    tool:Destroy()
                end
            end
        end
    end)
end)

I haven't tried it on studio but it should work properly.

1 comment

0

thank you! it works!

You should use an if Player.Backpack:FindFirstChild("ToolName") then something happening.

This discussion is preserved as it was. New answers, comments, and votes are closed.