[UNSOLVED] Gun Cloning Many Times When It Should Only Clone Once

Hello,
I am working on a script that gives you a cloned weapon from serverstorage. However, it keeps cloning multiple times, when it shouldn’t. Any help would be appreciated.

Local script:

local frm = script.Parent
local enabled = false
local selected
for _, button in pairs(frm:GetChildren())do 
	if button:IsA("TextButton") then
		button.MouseButton1Click:Connect(function() 
			if enabled == false then 
				enabled = true 
		        selected = button.Name
				game:GetService('ReplicatedStorage').Gun:FireServer(selected, button.Name)
				frm.Parent.TextLabel.Text = selected
				wait(1) 
				enabled = false
			end
		end)
	end
end

Server script:


local gunevent = game:GetService('ReplicatedStorage').Gun.OnServerEvent:Connect(function(player, selected, name)
	for _, v in pairs(player.Backpack:GetChildren()) do
		if name == "AR-15" then
			if v.Name == "TSR-28" or v.Name == "TSR-29" then
				v:Destroy()
				local clone = game:GetService('ServerStorage').Guns:FindFirstChild('AR-15'):Clone()
				clone.Parent = player.Backpack
			else
				local clone = game:GetService('ServerStorage').Guns:FindFirstChild('AR-15'):Clone()
				clone.Parent = player.Backpack
			end
		end
	end
end)

You could do an if statement to check if the player already has the item or not.

I pinned down the problem, in my loop here it loops through the players backpack, meaning the gun clones how many times depending on how many tools are in the players backpack. Is there any way I can achieve the cloning once while still going through the players backpack?

for _, v in pairs(player.Backpack:GetChildren()) do

the problem is the else statement in server probably? How many items are in player backpack?

There are 9 items by default in the players backpack. I tried taking them out, but the gun wont clone then. As I said, its the loop thats causing it to clone 9 times.

precisely so check before cloning that the gun you are about to clone, does not exist in player backpack i.e.

if player.Backpack:FindFirstChild("AR-15") then continue end