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.
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.