Duplicate entries when using multiple k,v in pairs

Hi all,

I got something to fix today. So I have these pairs statements:



	for _,v in pairs(LocalPlayer().PocketData) do 
		for k, weapon in pairs( LocalPlayer().PocketData ) do
			if weapon.weapon == v.weapon then
				-- Create DPanels for DIconLayout
			end
		end
	end

But when I have 2 of the same weapon in my table it shows 4 of them instead of 2:

So this is my table ( fetched from sql when player joins and put into table )( This is the PocketData Table ):



1:
		steamid	=	STEAM_0:1:28792222
		weapon	=	m9k_m3
		id	=	18
2:
		steamid	=	STEAM_0:1:28792222
		weapon	=	m9k_m3
		id	=	19


Then this is my DPanels which are now duplicated for some reason when there is only meant to be 2 weapons ( from table ):

http://s13.postimg.org/5wu27prhz/snippy.png

When I click on Drop ( a command I made using DermaMenu() ) It removes only one from the table. This means that there is a fault with my displaying code above

So how do I resolve this problem of duplicating items?

Thanks greatly,
Computer600

What is v.weapon, a string? “m9k_m3”? if so that’s what’s doing it. You need to give them unique identifiers and compare them using that

Yes, v.weapon is a string and is “m9k_m3”. How would I go about comparing them? Could I use the id that is already in the table maybe?

why are you nesting loops that go through the same table?

That’s so I can check whether the player has the weapon and if so show it. If you know a better way please tell me. I always like to improve my code to the best I can

That isn’t what it’s doing at all. Get rid of one of the for loops.

Try switching ‘weapon.weapon’ and ‘v.weapon’ in your if statement to ‘weapon.id’ and ‘v.id’ and see what happens.

[editline]20th May 2014[/editline]

But yeah as these guys said, that loop isn’t doing what you want - so you don’t need my fix at all if you remove one of those loops.



        local plyWeps = LocalPlayer():GetWeapons();
	for k, weapon in pairs( LocalPlayer().PocketData ) do
		if table.HasValue(plyWeps, weapon.weapon) then
			-- Create DPanels for DIconLayout
		end
	end


That’s for current weapons they have holding, if you want just their pocket, remove the plyWeps table and table.HasValue

If you are checking if they have that weapon in their pocket, I don’t get it? If they have that weapon, then it’s in PocketData right? so all you need to do is



	for k, weapon in pairs( LocalPlayer().PocketData ) do
		-- Create DPanels for DIconLayout
	end


I don’t think that would work though since weapon.weapon is a string while player weapons are entities

You can use the GetClass() function

Yeah you might have to rewrite it a bit though:



local plyweps = LocalPlayer():GetWeapons();
for k, plywep in pairs( plyweps ) do
	for j, weapon in pairs( LocalPlayer().PocketData ) do
		if plywep:GetClass()==weapon.weapon then
			-- Create DPanels for DIconLayout
		end
	end
end