How to get the value from a table using a string?

Greetings! So I need some help regarding this topic.

So, I have a player attribute called “Loadout”, it’s value is a string which is the player’s loadout name.

I have a ModuleScript which contains all the tables and values for each loadout. What I need is a way to get the loadout specifically using the name.

What solutions have I used?

I tried to use a function which contains a for loop which loops through the classes table and finds the value. Which then returns value. Problem is, it doesn’t work :skull:

function classes.returnClass(name)
	
	for index, value in pairs(classes) do
		if value == name then
			-- found
			return value
		end
	end
	
end

Here’s my code:

ModuleScript:

local players = game:GetService("Players")

local serverstorage = game:GetService("ServerStorage")

local classes = {
	
	--// LOADOUT FOR A TEAM
	
	["Red"] = {
		
		
		
		
		["Rifleman"] = {
			
			["LName"] = "Rifleman"; -- name of loadout
			
			["PlrCount"] = 18; -- max player limit
			
			["CurrentPlr"] = 0; -- current player count
			
		};
		
		
		

		["Recon"] = {
			
			["LName"] = "Recon";
			
			["PlrCount"] = 9;
			
			["CurrentPlr"] = 0;
			
		}
	};
	
	--// LOADOUT FOR B TEAM
	
	["Blue"] = {
		
		
		
		
		["Assault"] = {
			
			["LName"] = "Assault";
			
			["PlrCount"] = 18;
			
			["CurrentPlr"] = 0;
			
		};
		
		
		
		
		
		["Sniper"] = {
			
			["LName"] = "Sniper";
			
			["PlrCount"] = 9;
			
			["CurrentPlr"] = 0;
			
		}
	}
	
}

function classes.createLoadoutFolder(loadout, team)
	
	local loadoutfolder = Instance.new("Folder")
	loadoutfolder.Name = loadout.Name
	loadoutfolder.Parent = serverstorage.Tools[team.Name]
	
	return loadoutfolder
	
end

function classes.updatePlr(loadout, team, value)
	if value == true then
		-- positive player
		print(loadout.LName, "positive")
		classes[team.Name][loadout.LName].CurrentPlr += 1
	elseif value == false then
		-- negative player
		print(loadout.LName, "negative")
		classes[team.Name][loadout.LName].CurrentPlr -= 1
	end
end



function classes.returnClass(name)
	
	for index, value in pairs(classes) do
		if value == name then
			-- found
			return value
		end
	end
	
end

return classes

And (part) of the script itself:

	local char = player.Character
	
	local player_loadout = player:GetAttribute("Loadout")
	
	if player_loadout == "None" then
		player:SetAttribute("Loadout", loadout.LName)
	else
		print("fired")
		
		player:SetAttribute("Loadout", loadout.LName)
		
		--// I need to get the loadout class from the module using
-- the player's attribute, so I can put the loadout param in .updatePlr.
		
		loadoutmodule.updatePlr(loadout, team, false)
	end
	
	loadoutmodule.updatePlr(loadout, team, true)

I need to get the loadout class from the module using
the player’s attribute, so I can put the loadout param in .updatePlr.

If you’re looking for the class “Rifleman” for example, then it wouldn’t be returned because it’s nested within another table while your for loop structure only goes through the first level of one. On top of that, with the way you made your table, “Rifleman” is an index name, not a value.

Edit — try this:

function classes.returnClass(name)

	for index, team in pairs(classes) do
		if typeof(team) == "function" then continue end -- include this because you have functions within your classes table
		for key, value in pairs(team) do
			if key == name then
				-- found
				return value
			end
		end
	end

end
1 Like

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