How would I have it so a player can only have one of an item at a time?

I have a store UI working, but I want it so the player can only have one item, so it destroys everything in their backpack and clones the new tool back into the backpack.

The issue is I don’t know how to do this, as I never had this problem before. Even if this is super simple.

I’ve tried if statements, or even just trying to destroy the whole backpack, and just clone the other tools I don’t want to destroy back into the backpack. But, I don’t know a reasonable way to approach this.

Here is the server script:

local replicatedStorage = game:GetService("ReplicatedStorage")
local serverStorage = game:GetService("ServerStorage")
local weights = serverStorage.Weights
replicatedStorage.Remotes.Info.OnServerInvoke = function(player, item)
	return weights[item].Points.Value
end

replicatedStorage.Remotes.Sale.OnServerInvoke = function(player, item)
	local price = weights[item].Points.Value
	
	if player.leaderstats.Money.Value >= price then
		
		player.leaderstats.Money.Value = player.leaderstats.Money.Value - price
		player.StarterGear:Destroy()
		local tool = weights[item][item]:Clone()
		tool.Parent = player.StarterGear
		
		return true 
		
	else
		
		return false
	end
end

Anything helps! Thanks. :smile:

When you create a new tool, insert a StringValue into it that contains the item’s name as the value. Before parenting the new item to their player, check if there is one of theses StringValues that has the same item name. If there is, then delete it.

So you want a player to only ever have one tool in their backpack? Here’s how you can do that.

local function switchTool(player, newTool)
	-- Remove all objects from Backpack and StarterGear
	player.StarterGear:ClearAllChildren()
	player.Backpack:ClearAllChildren()
	
	-- If they have a tool equipped, remove that too
	local character = player.Character
	local humanoid = character and character:FindFirstChildOfClass("Humanoid")
	if humanoid then
		humanoid:UnequipTools()
	end
	
	-- Give them the new tool
	newTool:Clone().Parent = player.Backpack
	newTool:Clone().Parent = player.StarterGear
end

This will remove their existing tool and add a new one to their backpack.

12 Likes