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

How to detect if two tools are in the backpack (and starter gear)?

Asked by 8 years ago

To expand on the question, I want to find if same weapons are in the backpack (eg. two guns) and destroy all but one of them. Example of destroying a weapon (but doesn't detect if there are two or more of the same weapons in the Backpack/Startergear):

if player.Backpack:FindFirstChild("M4A4")
        player.Backpack.M4A4:Destroy();
end

1 answer

Log in to vote
1
Answered by 8 years ago

You first would need to get all of the children, then check for duplicates or store them in a list e.g.

local tmpTable = {} -- store items names

for I,v in pairs(player.Backpack:GetChildren()) do --  loop through all items found
    if  tmpTable [v.Name] ~= nil then -- checks if item exists in the list
        print("a duplicate has been found") -- found item e.g. a duplicate
        v:Destroy() -- deletes tool 
    else
        tmpTable [v.Name]  = "a"  -- we don't need to use the value we are only using the key
        print("item added to list") 
    end 
end

The code above is just an example, I have not tested this in studio.

Hope this helps.

0
Thanks! Just a note for anyone using the script, it remove the weapon after death (for me at least) coolan28 25 — 8y
0
Forgot to add but this script realys of the tool name being different so if there a two tools with the same name but each tool is different then one will be deleted. User#5423 17 — 8y
Ad

Answer this question