Here is what I have setup currently
local par = script.Parent
local remote = par.RemoteEvent
local tools = par.Tools
local limit = 2
local function DuplicatesExist(arr: {any}): {any}?
if not arr then
return
end
local hash = {}
local dups = {}
for _,v in ipairs(arr) do
if hash[v] then
if not dups[v] then
table.insert(dups, v)
end
else
hash[v] = true
end
end
return dups
end
local function Check(player: Player)
if not player then
return
end
local backpack = player.Backpack
local arr = backpack:GetChildren()
local check = DuplicatesExist(arr)
for i,v in ipairs(check) do
arr[v]:Destroy()
end
end
remote.OnServerEvent:Connect(function(plr, info)
if #plr.Backpack:GetChildren() < limit then
if info == "Tool1" then
Check(plr)
local tool = tools["Barret M82"]:Clone()
tool.Parent = plr.Backpack
elseif info == "Tool2" then
Check(plr)
local tool = tools["Glock 19"]:Clone()
tool.Parent = plr.Backpack
elseif info == "Tool3" then
Check(plr)
local tool = tools.HK416:Clone()
tool.Parent = plr.Backpack
end
end
end)