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

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

Asked by
trecept 367 Moderation Voter
6 years ago

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?

0
Are you talking about ClassName or relative Name? DeceptiveCaster 3761 — 6y
0
More details on your script please, you might need a debounce. royee354 129 — 6y

1 answer

Log in to vote
1
Answered by 6 years ago

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)
0
If there are two Instances named Hammer it will delete them both DeceptiveCaster 3761 — 6y
Ad

Answer this question