How to limit players backpack space or delete tools if the max is reached (This is for a FPS game using CE guns)

These are my 2 scripts im using for the tool giver if this helps at all…
Local Script V

local par = script.Parent
local remote = script.Parent.RemoteEvent

par.Tool1.MouseButton1Click:Connect(function()
	remote:FireServer("Tool1")
	par.Visible = true
end)

par.Tool2.MouseButton1Click:Connect(function()
	remote:FireServer("Tool2")
	par.Visible = true
end)

par.Tool3.MouseButton1Click:Connect(function()
	remote:FireServer("Tool3")
	par.Visible = true
end)

Script V

local par = script.Parent
local remote = par.RemoteEvent
local tools = par.Tools

remote.OnServerEvent:Connect(function(plr, info)
	if info == "Tool1" then
		local tool = tools["Barret M82"]:Clone()
		tool.Parent = plr.Backpack
	elseif info == "Tool2" then
		local tool = tools["Glock 19"]:Clone()
		tool.Parent = plr.Backpack
	elseif info == "Tool3" then
		local tool = tools.HK416:Cloneo()
		tool.Parent = plr.Backpack
	end
end)

[Video Example](

)

On your tool giver code, you can add a check to see how many tools are in the Backpack.

local par = script.Parent
local remote = par.RemoteEvent
local tools = par.Tools
local limit = 5

remote.OnServerEvent:Connect(function(plr, info)
	if #plr.Backpack:GetChildren() < limit then
		if info == "Tool1" then
			local tool = tools["Barret M82"]:Clone()
			tool.Parent = plr.Backpack
		elseif info == "Tool2" then
			local tool = tools["Glock 19"]:Clone()
			tool.Parent = plr.Backpack
		elseif info == "Tool3" then
			local tool = tools.HK416:Cloneo()
			tool.Parent = plr.Backpack
		end
	end
end)

Okay so this worked but I would like it to replace the tools in the hotbar. (example: you equip 2 glocks and want to replace it with a HK so you press the HK and one of the glocks deletes and the HK takes it position. Im guessing that would involve something along the lines (this is my terrible code to english translation) If local limit is met and “Tool2” then delete 1st hotbar slot and clone tool to plr.backpack

local par = script.Parent
local remote = par.RemoteEvent
local tools = par.Tools
local limit = 2


remote.OnServerEvent:Connect(function(plr, info)
	if #plr.Backpack:GetChildren() < limit then
		if info == "Tool1" then
			local tool = tools["Barret M82"]:Clone()
			tool.Parent = plr.Backpack
		elseif info == "Tool2" then
			local tool = tools["Glock 19"]:Clone()
			tool.Parent = plr.Backpack
		elseif info == "Tool3" then
			local tool = tools.HK416:Cloneo()
			tool.Parent = plr.Backpack
		end
	end
end)
1 Like

Try this code out, haven’t tested it out yet but theoretically this should work.

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)

I’m not entirely sure what you’re trying to achieve due to the fuzzy explanation but I believe you want older tools to be removed as opposed to newly added ones.

local Game = game
local Players = Game:GetService("Players")

local Items = {}

local function OnPlayerAdded(Player)
	local function OnCharacterAdded(Character)
		local function OnBackpackChildAdded(Child)
			if table.find(Items[Character], Child) then return end --Ignore if tool has already been seen.
			if not (Child:IsA("BackpackItem")) then return end
			table.insert(Items[Character], Child)
			if #Items[Character] < 6 then return end --Limit of 5 tools.
			local Item = table.remove(Items[Character], 1) --Destroy first tool.
			Item:Destroy()
		end

		Items[Character] = {}
		local Backpack = Player:FindFirstChildOfClass("Backpack") or Player:WaitForChild("Backpack", 10)
		if not Backpack then return end
		Backpack.ChildAdded:Connect(OnBackpackChildAdded)
		for _, Child in ipairs(Backpack:GetChildren()) do
			OnBackpackChildAdded(Child)
		end
	end

	local function OnCharacterRemoving(Character)
		Items[Character] = nil --Clear item cache when the character is removed.
	end

	Player.CharacterAdded:Connect(OnCharacterAdded)
	Player.CharacterRemoving:Connect(OnCharacterRemoving)
end

Players.PlayerAdded:Connect(OnPlayerAdded)

Thats exactly it just like in the video example