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

Limit Backpack to One of the Same Tool?

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

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.

3 comments

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?

0

Yeah, this is good

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.

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