How make inventory system not allow equip two same item

hi developers so i was try do inventory system so i search on yt and i found how make my inventory when i complete i found there problem is i wont allow player equip 2 same tool like if player have already apple he cant equip another apple but if player want equip banana it will allow player and i want do it value inside tool not same tool name that all i was want

local dss = game:GetService("DataStoreService")
local ds = dss:GetDataStore("InventoryData")
function saveData(p)
	
	local inventory = {}
	
	for i, value in pairs(p.Inventory:GetChildren()) do
		
		inventory[value.Name] = value.Value
	end
	
	ds:SetAsync(p.UserId, inventory)
end


game.Players.PlayerAdded:Connect(function(p)
	
	local invFolder = Instance.new("Folder", p)
	invFolder.Name = "Inventory"
	
	for i, item in pairs(game.ReplicatedStorage.Items:GetChildren()) do
		
		local newValue = Instance.new("IntValue")
		newValue.Name = item.Name
		newValue.Value = 0
		newValue.Parent = invFolder
		local rank = item.rank
		rank.Name = "rank"
		rank.Parent = newValue
		local about = item.about
		about.Name = "about"
		about.Parent = newValue
	end
	
	
	p.CharacterAdded:Connect(function(c)
		
		c.Humanoid.Touched:Connect(function(hit)
			
			if hit:FindFirstChild("ITEM") then
				p.PlayerGui.ScreenGui.TextLabel.Visible = true
				p.PlayerGui.ScreenGui.TextLabel.Text = p.PlayerGui.ScreenGui.TextLabel.Text .. hit.Name
				invFolder[hit.Name].Value += 1
				
				hit:Destroy()
				wait(.5)
				p.PlayerGui.ScreenGui.TextLabel.Visible = false
				p.PlayerGui.ScreenGui.TextLabel.Text = "New Item: "
			end
		end)
	end)
	
	
	local invData = ds:GetAsync(p.UserId) or {}
	
	for itemName, itemCount in pairs(invData) do
		--print(itemName,itemCount)
		invFolder[itemName].Value = itemCount
		
	end
end)


game.ReplicatedStorage.Inventory.OnServerEvent:Connect(function(p, instruction, item)
	
	if instruction == "equip" and p.Inventory:FindFirstChild(item) and p.Inventory[item].Value > 0 then
		
		p.Inventory[item].Value -= 1
		
		local newItem = game.ReplicatedStorage.Items[item]:Clone()
		newItem.Parent = p.Backpack
	end
end)


game.Players.PlayerRemoving:Connect(saveData)

game:BindToClose(function()
	for i, p in pairs(game.Players:GetPlayers()) do
		saveData(p)
	end
end)

i search on youtube but still i don’t find any solve for this problem

I would make a folder that constains boolvalues with the items’ name, and set it to false default. Upon taking the item, I would look for its name inside of the folder with pairs loop, then if the v.Value is false, then I would insert the item and set the value to true. When dropping, just making the value false.

Hi, how can we help you today?

can you show me how make something like that i have try make one but not work

create the int value and name the intvalue the item name itself and if player owns duplicates just add +1 to the intvalue

example of logic

load players data

create a folder named playerstats and put all the items in that folder

check if the item exists in the players inventory if so add +1 to the intvalue if not so create new intvalue

so after many try it not work this is the script i made can you know where is the problem

local dss = game:GetService("DataStoreService")
local ds = dss:GetDataStore("InventoryData")
function saveData(p)
	
	local inventory = {}
	
	for i, value in pairs(p.Inventory:GetChildren()) do
		
		inventory[value.Name] = value.Value
	end
	
	ds:SetAsync(p.UserId, inventory)
end


game.Players.PlayerAdded:Connect(function(p)
	
	local invFolder = Instance.new("Folder", p)
	invFolder.Name = "Inventory"
	
	for i, item in pairs(game.ReplicatedStorage.Items:GetChildren()) do
		
		local newValue = Instance.new("IntValue")
		newValue.Name = item.Name
		newValue.Value = 0
		newValue.Parent = invFolder
		local rank = item.rank
		rank.Name = "rank"
		rank.Parent = newValue
		local about = item.about
		about.Name = "about"
		about.Parent = newValue
	end
	
	
	p.CharacterAdded:Connect(function(c)
		
		c.Humanoid.Touched:Connect(function(hit)
			
			if hit:FindFirstChild("ITEM") then
				p.PlayerGui.ScreenGui.TextLabel.Visible = true
				p.PlayerGui.ScreenGui.TextLabel.Text = p.PlayerGui.ScreenGui.TextLabel.Text .. hit.Name
				invFolder[hit.Name].Value += 1
				
				hit:Destroy()
				wait(.5)
				p.PlayerGui.ScreenGui.TextLabel.Visible = false
				p.PlayerGui.ScreenGui.TextLabel.Text = "New Item: "
			end
		end)
	end)
	
	
	local invData = ds:GetAsync(p.UserId) or {}
	
	for itemName, itemCount in pairs(invData) do
		--print(itemName,itemCount)
		invFolder[itemName].Value = itemCount
		
	end
end)


game.ReplicatedStorage.Inventory.OnServerEvent:Connect(function(p, instruction, item)

	if instruction == "equip" and p.Inventory:FindFirstChild(item) and p.Inventory[item].Value > 0 then

		-- Check if the player already has an equipped item with the same "gggggg" value
		local hasSameValue = false
		for _, equippedItem in pairs(p.Backpack:GetChildren()) do
			if equippedItem:FindFirstChild("gggggg") and equippedItem.gggggg.Value == p.Inventory[item]:FindFirstChild("gggggg").Value then
				hasSameValue = true
				break
			end
		end

		-- If the player does not have an equipped item with the same "gggggg" value, equip the item
		if not hasSameValue then
			p.Inventory[item].Value -= 1

			local newItem = game.ReplicatedStorage.Items[item]:Clone()
			newItem.Parent = p.Backpack
		end
	end
end)


no one know how solve that problem

I’m working on it, I’ll be done soon.

1 Like

I made with a proximityPrompt, but you can use clickdetector / whatever you want, just change the line if.

script.Parent.Triggered:Connect(function(player)
	local char = player.Character
	local item = script.Parent.Parent
	local name = item.Parent.Name
	local backPack = player.Backpack:FindFirstChild(name)
	local inSide = char:FindFirstChild(name)
	if not backPack and not inSide then --If these are both true, the tool will be picked up
		item.Parent.Parent = char
		item.ProximityPrompt.Enabled = false
	end
end)

So it checks if the proxy was triggered, and if, it takes some basic variables like the character, the player, the item (parent of proxy in this case), the name of the tool, then it goes to the player’s backpack, where the tools are stored, and the inventory, the currently equipped item. The script looks for the name of this tool in both of them, what it means is it looks for the same name of a tool we are trying to take. If it doesn’t find a tool called banana in my case in the inventory (equipped) and inside of the backpack (not equipped), it means we don’t have the item yet, so we can take it.

You can make it with a value or etc, my system relies on the name of the tool. If you want to make a gui for this, you can use the line where the item gets picked up, aka if not backPack and not inSide, in that case the item will be picked up and you can a gui frame instance for that or anything basically.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.