How do you check what the player is holding from a GUI?

So what I’m trying to do is make it so when a player pressed a gui button okay, it removes any duplicates named “Weapon” so basically what I mean is it gets the player when it has the player it checks the startergear and removes any tool named “weapon” it checks the players backpack and removes any tool named “weapon”.

What I want it to do is find the player in the workspace who clicked the GUI button and destroy any tool named “Weapon”. This bug is where I can’t get any duplicates, right but when I hold the tool and click the gui button it breaks because the tool isn’t in the backpack anymore. It’s in my hand, and it can’t find the tool to destroy it.

Here’s the script:

script.Parent.MouseButton1Down:connect(function()
if Player:findFirstChild("leaderstats") then
if Player.leaderstats[script.Parent.Currency.Value].Value>=tonumber(script.Parent.Cost.Value) then
Player.leaderstats[script.Parent.Currency.Value].Value=Player.leaderstats[script.Parent.Currency.Value].Value-tonumber(script.Parent.Cost.Value)

a = Player.Backpack:GetChildren()
for i=1,#a do 
if a[i].Name == "Weapon" then
a[i]:remove()
end
end

a2 = Player.StarterGear:GetChildren()
for i=1,#a2 do 
if a2[i].Name == "Weapon" then
a2[i]:remove()
end
end

script.Parent.Weapon:Clone().Parent=Player.Backpack
script.Parent.Weapon:Clone().Parent=Player.StarterGear
print("works")
end
end
end)```

If I understand you right, you’re trying to have a button that removes duplicated items in the players equipped, backpack, and startergear through a button.

what you have is on the right track, and it could be simplified a tad more

script.Parent.MouseButton1Down:Connect(function()
     if Player:FindFirstChild("leaderstats") then
          if Player.leaderstats[script.Parent.Currency.Value].Value >= script.Parent.Cost.Value then
               Player.leaderstats[script.Parent.Currency.Value].Value -= script.Parent.Cost.Value 
              
               for _,item in pairs(Player.Backpack:GetChildren()) do
                    if item.Name == "Weapon" then
                         item:Destroy()
                    end
               end

               for _,item in pairs(Player.StarterGear:GetChildren()) do
                    if item.Name == "Weapon" then
                         item:Destroy()
                    end
               end

               if Player.Character:FindFirstChild("Weapon") then
                    Player.Character:FindFirstChild("Weapon"):Destroy()
               end

               script.Parent.Weapon:Clone().Parent = Player.Backpack
               script.Parent.Weapon:Clone().Parent = Player.StarterGear
          end
     end
end)

also dont use :remove() as it’s deprecated you should use :Destroy() in new work.

oh my god ur a lifesaver literally I did not know u can just reference the player, then get the character in the workspace that makes no sense, sorry I’m new to scripting but it works!!!

I figured you already had the player referenced as you had in your script.

I was trying to get the player that clicked it then do game.workspace:FindFirstChild(player)
then if name == “Weapon” then delete but this makes it so much easier that’s what this script does right, anything that’s in the player model in workspace that is named “Weapon” it deletes, right?

is it possible to do a boolvalue named “Tier” or like any value and check if the tool has that tier name it’ll delete it instead of the tools name?

wait here’s a issue… I have a gun in the starterpack but when the player upgrades their tier and replaces that gun it’ll just add the gun back because it’s in the starterpack how would I fix that?

Rather than put the gun in starter pack how about when the player joins the game then just clone the gun from storage?

yeah but the problem is when the player upgrades their gun I want it to remove any gun named weapon and just have the gun they bought, but when they rejoin or reset the starter gear that is in the starter pack gets added to their inventory again and i have no way of knowing how to prevent this so it only shows up to new players inventories