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
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.