GUI Button Script Not working Properly

I’ve been working on a game that uses Titan Weapons (A weapon system) and I have a GUI scripted to give people weapons from each class (Primary and Secondary) and that script works fine but however when I reset, the guns go away so I had a script made that keeps their weapons. But, when people go into the GUI to select a new weapon the weapons do not replace and instead adds a new weapon and I don’t want this to happen. How would I fix the issue?

The GUI Button Script:

local plr = script.Parent.Parent.Parent.Parent.Parent

local PrimaryFolder = game.ReplicatedStorage.Primary
local SecondaryFolder = game.ReplicatedStorage.Secondary

local PrimaryButtons = script.Parent.Primary
local SecondaryButtons = script.Parent.Secondary

local Primary = nil
local Secondary = nil

for i,v in pairs (PrimaryButtons:GetChildren()) do
	v.MouseButton1Click:Connect(function()
		if Primary ~= nil then game.Debris:AddItem(Primary, 0) Primary = nil end
		wait()
		local weapon = PrimaryFolder:FindFirstChild(v.Name):Clone()
		weapon.Parent = plr.Backpack
		Primary = weapon
	end)
end

for i,v in pairs (SecondaryButtons:GetChildren()) do
	v.MouseButton1Click:Connect(function()
		if Secondary ~= nil then game.Debris:AddItem(Secondary, 0) Secondary = nil end
		wait()
		local weapon = SecondaryFolder:FindFirstChild(v.Name):Clone()
		weapon.Parent = plr.Backpack
		Secondary = weapon
	end)
end

The script that makes them keep their weapons

local tab = {}

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(chr)
		for i,v in pairs (tab) do
			if game.ReplicatedStorage.Primary:FindFirstChild(v) then game.ReplicatedStorage.Primary:FindFirstChild(v):Clone().Parent = plr.Backpack end
			if game.ReplicatedStorage.Secondary:FindFirstChild(v) then game.ReplicatedStorage.Secondary:FindFirstChild(v):Clone().Parent = plr.Backpack end
		end
		for i,v in pairs (tab) do
			table.remove(tab, i)
		end
		local hum = chr:WaitForChild("Humanoid")
		hum.Died:Connect(function()
			for i,v in pairs (plr.Backpack:GetChildren()) do
				if v:IsA("Tool") then table.insert(tab, v.Name) end
			end
			if chr:FindFirstChildWhichIsA("Tool") then
				table.insert(tab, chr:FindFirstChildWhichIsA("Tool").Name)
			end
		end)
	end)
end)

The guns are in a folder in ReplicatedStorage under Primary and Secondary for both classes on the GUI.
Screenshot_711

Clip of the problem:
https://cdn.discordapp.com/attachments/786135523370074112/786135621155815434/scriptissuee.mp4

Try turning the ‘ResetOnSpawn’ property off on the ScreenGui that your localscript is inside of.

Nope, it doesn’t work. It still adds another tool

You need to use remotes anyway. Cloning the weapons from a local script will not work on the server, instead you should fire a RemoteEvent and give the player their weapon from the server side.

Also, just use

local plr = game.Players.LocalPlayer

And also use remote events for the tools, as @gooey333 suggested.

Where would I put that? Not much of a scripter