Locating tables with name

I’ve been working on this system of scanning down the value and locating if “Name” == “Name” then Value from the Table

program

local Weapons = {

-- PRIMARY

["Shotgun"] = "Shotgun",

-- BACKUP

["PixelGun"] = "PixelGun",

-- MELEE

["CombatKnife"] = "CombatKnife",

-- SPECIAL

["SniperRifle"] = "SniperRifle",

-- PREMIUM

["SignalPistol"] = "SignalPistol",

}

print("Shotgun" == Weapons)

I just made this script up, and I know it probably sucks that you can’t put exactly "Shotgun" == Weapons, and because of lots of things. Maybe there is a better way, but this should be fine for now.

local Tables = {
	Weapons = {
	
		-- PRIMARY

		["Shotgun"] = "Shotgun",
	
		-- BACKUP
	
		["PixelGun"] = "PixelGun",
	
	
		["CombatKnife"] = "CombatKnife",
	
		-- SPECIAL
	
		["SniperRifle"] = "SniperRifle",
	
		-- PREMIUM
	
		["SignalPistol"] = "SignalPistol",

	}
}	

local function locateTable(value)
	for i,v in pairs(Tables) do
		for i2,v2 in pairs(v) do
			if i2 == value then -- Or v2 (I don't know what you're trying to do)
				return v
			end
		end
	end
	return nil
end

print(locateTable("Shotgun") == Tables.Weapons)

I don’t understand what your trying to do. Are you trying to get the value of a key?

Normally you perform a nil check like so:

print(Weapons["Shotgun"] ~= nil) -- true if exists within the table/dictionary

I don’t believe a for loop will be necessary for @Ioveczour

1 Like

Yes, I’m trying to get that, like if “Shotgun” == “Shotgun” (in the table and gets value) then “Result”

All you need to do is Weapon["Shotgun"] to access the value.

I only did it like that, because there may be another table with the same index but a different value.

Like the name not a true or false output.

@Ryanington Oh. The name of the table? If you’re getting the name of the table, try this.

local Tables = {
	Weapons = {
	
		-- PRIMARY

		["Shotgun"] = "Shotgun",
	
		-- BACKUP
	
		["PixelGun"] = "PixelGun",
	
	
		["CombatKnife"] = "CombatKnife",
	
		-- SPECIAL
	
		["SniperRifle"] = "SniperRifle",
	
		-- PREMIUM
	
		["SignalPistol"] = "SignalPistol",

	}
}	

local function locateTable(value)
	for i,v in pairs(Tables) do
		for i2,v2 in pairs(v) do
			if i2 == value then -- Or v2 (I don't know what you're trying to do)
				return {i,v}
			end
		end
	end
	return nil
end

print(locateTable("Shotgun")[1]) -- Prints "Weapons" | [1] = Name, [2] = Table

The title literally says Locating tables with name, so don’t blame me if the script is wrong.

1 Like

The problem with this is your trying to print the dictionary, if you want to access the value (assuming they key is the same as the name of the weapon) you need to access the value in the dictionary with that key, Weapons[Weapon Name Here].

1 Like