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

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

Asked by 4 years ago
Edited 4 years ago

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:

1game.Players.PlayerAdded:connect(function(Player)
2    Player.Backpack.ChildAdded:connect(function(Child)
3         if not v == Child and v.Name == Child.Name then
4                Child:Destroy()
5            end
6        end
7    end)
8end)
1
what is v? SteamG00B 1633 — 4y

2 answers

Log in to vote
0
Answered by
uhi_o 417 Moderation Voter
4 years ago

You could try this

01game.Players.PlayerAdded:connect(function(Player)
02    Player.Backpack.ChildAdded:connect(function(Child)
03        for _, v in pairs(Player.Backpack:GetChildren()) do --Looping in backpack
04            local CurrentTool = v --Saving a copy somewhere
05            for _, tool in pairs(Player.Backpack:GetChildren()) do --Looping again but with the tool found
06                if tool.Name == CurrentTool.Name then --If we find a match
07                    tool:Destroy()
08                end
09            end
10        end
11    end)
12end)

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

0
thank you! it works! anneca313 17 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

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

Answer this question