How to detect if there's a duplicate item in Backpack?

That won’t work, as it’ll only check ONE item.

Sorry, but do you know what’s RunService?

Ok then.

for i,v in pairs(player.Backpack:GetChildren()) do
if v.Name == “ToolName” then

end
end

Still won’t work, it would just exclude one item from the backpack.

I’ve been trying to learn about it, but some of it is kinda confusing,

Alright then what is he asking? Instead of telling me what not to do tell me what he wants

It’s like a while loop, but more safer and efficient.

I do have the script, but ITV might be confusing to understand.

You could use the .ChildAdded event and check if the item already exists in the backpack or on your character.

He clearly said it’s a serverscript… Runservice works on client

Have you read properly? Only RunService.RenderStepped works on client only. The others can work on server script.

So basically your saying

while game:GetService(“RunService”).Heartbeat:Wait() do

end

To check for a duplicate item is actually quite simple the first thing I would do is to simply put the names of all the items in a table but ill just make it a nice function to make your life easier and so its easy to read.

local checkDuplicates = function(player)
	local tempItemTable = {}
	
	for _,v in pairs(player.Backpack:GetChildren()) do
		if v:IsA("Tool") then    			
			if table.find(tempItemTable, v.Name) then -- now we just check if there is another occurence of an item of the same name twice with a table.find
				print("Duplicate item found")
				
				-- now you just add the rest of your logic for it a duplicate is four
			end

			tempItemTable[#tempItemTable + 1] = v.Name -- inserts the name of the item into the table

		end
	end
end
4 Likes

Yes, this is for constantly check to make sure all players don’t have duplicated tools.

1 Like

Makes sense, but why the function in a variable? I’m still learning how to script.

Functions are kind of a variable it depends how you go about it but there are two types of function assignment just as there are two types of variable assignment and their difference is the amount of memory being allocated for that variable. That is the difference when you learn A global variable vs. A local variable it’s just like doing

x = 13 -- this will use more memory since its a global
local x = 13 -- this will use less since its a local

I didn’t need to put the 5 characters local infront of my function declaration but it just makes the code more efficient. It’s sort of a complicated topic to grasp but just letting you know for the future.

Ohh, I get it now, I’m gonna try if it works.

So, this

local function hello()

end

is not the same as

local helloFunction = function()

end

???

There is no difference between those two, you are both assigning them as a local function.

Can refer to : What is the difference between a local function and a normal function? - #3 by XAXA

for any other issues

1 Like

Also, you can create a connection with the event Backpack.ChildAdded:Connect() and inside the parentheses, put the function there.

1 Like