Player's backpack problem

Hi! I have this inventory script that only allows one tool to be equipped at a time. Here is the gui script:


		item.SelectButton.MouseButton1Click:Connect(function()
			local price = tool.Price.Value
			local coins = player.leaderstats.Coins.Value
			if item == SelectedTrail then
				item.SelectButton.Text = "Equip"

				SelectedTrail = nil
			else
				if player.OwnedTools:FindFirstChild(tool.Name)  then
					item.SelectButton.Text = "Unequip" 
				else
					item.SelectButton.Text = "Unequip"
				end
				if SelectedTrail then
					SelectedTrail.SelectButton.Text = "Equip"
				end

				SelectedTrail = item
			end

			TrailSelectedRE:FireServer(tool)
		end)
	end

It fires this event which triggers this:


ToolSelectedRE.OnServerEvent:Connect(function(plr, tool)
	if not tool or typeof(tool) ~= "Instance" then return end -- we still want this here just in case

	local ownedTool = plr.OwnedTools:FindFirstChild(tool.Name)




	if not ownedTool then
		print(plr.Name," is buying the ",tool.Name," tool.")

		local price = tool.Price.Value
		local coins = plr.leaderstats.Coins

		if price <= coins.Value then
			coins.Value -= price

			local newTool = tool:Clone() do
				newTool.Parent = plr.OwnedTools



			end
		end
	elseif ownedTool then
		if plr.Backpack:FindFirstChild(tool.Name) == nil then
			if plr.Backpack:FindFirstChildOfClass('Tool') then
				plr.Backpack:FindFirstChildOfClass('Tool'):Destroy()
			end
			print(plr.Name," equipped ",tool.Name," tool.")

			local new = tool:Clone()
			new.Parent = plr.Backpack

		elseif plr.Backpack:FindFirstChild(tool.Name) then
			print("triggered")

			local deleted = plr.Backpack:WaitForChild(tool.Name)
			deleted:Destroy()

		end
	end
end)

The problem is, if you equip the tool you have in your backpack and then equip another tool, it will bypass the one tool check and the player can access two tools in their inventory. How do I fix this?

I tried removing specific parts of the first script but got no luck. I don’t know which script to fix the issue though.

Hi! Are you making a custom inventory/backpack system, or are you using the one Roblox provides?

This is a custom one that I made.

Alright. I would suggest a variable or object value to store the player’s current equipped tool.

If you’re using a variable, you’ll need to do so in a local script, which might look something like this:

local equippedTool = nil
-- Your existing code

TrailSelectedRE:FireServer(tool, equippedTool)
equippedTool = tool

Then, on the server, when the remote event is triggered you’ll need to actually delete the tool:

ToolSelectedRE.OnServerEvent:Connect(function(plr, toolToEquip, toolToDelete)
-- Elsewhere in your script:

	if toolToDelete ~= nil then
		toolToDelete:Destroy()
	end
end

Alternatively, you could use an object value. You’ll probably want to create this value inside the player object (not character), or wherever makes sense for your game. You’ll need two things. First, to create the value (server script):

game.Players.PlayerAdded:Connect(function(plr)
	local equipped = Instance.new("ObjectValue",plr)
	equipped.Name = "EquippedTool"
end

Then, in your existing server script, you can just find plr.EquippedTool.Value (or wherever you’re storing the value) and, if the value isn’t nil, delete the tool. Remember, you’ll also need to update this value with the new tool being equipped!

Hope this helps!

I tried a simpler version of this but it doesn’t work. The problem is, the player HAS to have the tool in their inventory equipped to bypass it. Is there a way I can force the tool to unequip and then delete it from the players inventory?

I had to use Humanoid:UnequipTools() and it worked.