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

How to check if two of the same object is added into something?

I'm using a ChildAdded function to try and check if an item that is already there is added so there is two of the same thing, e.g two of the same hammers or bricks or any object that is duplicated. How can I see, when a child is added to this object, if the child already exists so I can destroy it so there is not two of the same thing inside?

2 comments

0

Are you talking about ClassName or relative Name?

0

More details on your script please, you might need a debounce.

1 answer

✓ Accepted answer

Let's say the tool you're using is in ReplicatedStorage that can be cloned to a Player's Backpack. In that case:

local RS = game:GetService("ReplicatedStorage")
local Tool = RS:FindFirstChild("Hammer", false) -- Or RS:FindFirstChild("Part")
script.Parent.MouseClick:Connect(function() -- I'm assuming you're using a ClickDetector or something
	local plr = game.Players:GetPlayerFromCharacter(script.Parent)
	if plr then
		if not plr.Backpack:FindFirstChild("Hammer") then
			Tool:Clone().Parent = plr.Backpack
		end
		if plr.Backpack:FindFirstChild("Hammer") then
			local PlrTool = plr.Backpack:FindFirstChild("Hammer")
			PlrTool:Destroy()
		end
	end
end)

1 comment

0

If there are two Instances named Hammer it will delete them both

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