Still a little bit confused on what you’re asking.
If I’m understanding you correctly, you want to be able to more effectively have certain ability attributes associated with other ability attributes. As of right now, you have each attribute assigned to its own individual array with the same associated indices. For example, if you had a fireball ability, its cooldown value would be stored in an array specific to storing cooldown values (for all abilities) while its keybind would be stored in a separate array specific to storing keybind values (again for all abilities).
If this is your situation, then yes, there is a more effective way to achieve what you are trying to do.
My approach to this would be to use dictionaries, rather than arrays. Dictionaries are more dynamic in that keys are not limited to being integers.
Here’s an example of a basic dictionary:
local inventory = {
["Hearts"] = 5,
["Level"] = 1,
["Coins"] = 3000
}
As you can see, this syntax is similar to an array, but the keys of the dictionary (the parts to the left of the equals signs) are strings rather than integer values. As a result of this, we can index our dictionary as such:
print(inventory.Hearts) -- prints out 5
print(inventory["Coins"]) -- prints out 3000
--both syntaxes of indexing a dictionary are correct, but the first syntax will not work if the key has spaces in it
In your case, we can use dictionaries in the following manner:
local abilities = {}
for i, ability in pairs (cs:GetTagged("Ability")) do
local abilityModule = require(ability) -- I assume you meant require, unless this is a local function.
if not table.find(abilities, abilityModule.Name) then
abilities[abilityModule.Name] = { -- Dictionary within dictionary!
["Cooldown"] = abilityModule.Cooldown,
["Keybind"] = abilityModule.Keybind -- I assume you meant abilityModule instead of abilityKB in your original code. I don't see how you could have gotten abilityKB.Keybind because it is an array and does not have a Keybind field.
}
end
end
Here is an explanation of the above code. Firstly, the abilities table is created. Then the cs:GetTagged("Ability") table is looped through, associating the ability’s name as the key for the pair and a dictionary mapping the cooldown and keybind as the value. It might be a bit complicated, so here is what the abilities table would look like after you pass two example abilities through the loop. Let these two example abilities be fireball and ice.
local abilities = {
["Fireball"] = {
["Cooldown"] = 0.5,
["Keybind"] = "Z"
},
["Ice"] = {
["Cooldown"] = 1,
["Keybind"] = "X"
}
}
As you can see, the respective cooldowns and keybinds of each ability are associated with the name of the ability. So, if you wanted to retrieve the cooldown of ice, for example:
print(abilities.Ice.Cooldown) -- prints 1
Hopefully this helps. Also, note some of the changes I made to your direct code. From the context you provided, the changes I made make intuitive sense, but you may have other parts of the code that you didn’t show here that make everything all correct. I also was not able to thoroughly test the code I sent here for errors because I don’t have access to your modules (obviously), so be warned that they might not be completely correct and you may have to tinker with the code to get it to work.
Let me know if you have any further questions.
Here are some links that you might find helpful related to your problem: