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.
Clip of the problem:
https://cdn.discordapp.com/attachments/786135523370074112/786135621155815434/scriptissuee.mp4