How do I connect the values of two related tables

I want to be able to connect the tables ‘abilityKB’ and ‘abilityMB’ so that I can put a wait debounce for the amount of time corresponding to the ability’s move break, which is the time needed to wait until you can use another ability. I would reference the ability move break through checking the user’s input checking all the keys in the abilityKB table and then having something to match the ability key-bind to its correct ability move break.

For example, let’s say a Fireball ability is bound to ‘Z’, then it would check the table for if there’s an ability for the ‘Z’ key bind, from there it would connect the ‘Z’ key to the move break of .5 seconds in the other table. This would put a debounce on, in this case, my punch tool.

I have tried searching up a solution to this, but I don’t think I can get the right search wording right because I don’t really know what I’m looking for, so I decided asking you knowledgable peoples.

If you guys also know a more effective way to achieve what I am doing, then that would be great as well.


– tables code

are you asking how to merge two tables?

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:

2 Likes

This was the perfect solution to my problem. Also, I know its kind of weird but the misspelt built-in functions like require and tostring is because I put a variable on them so that it preloads it. I heard it was good to do, but I have found that many don’t really recognize/use it so I do not think that it matters much. Also, the abilityKB thing was an error, thanks for the help!

By the way, if anybody is using @xendatro 's code then where it says ability[abilityModules.Name, it is supposed to be the dictionary abilitiesabilities[abilityModules.Name]

1 Like

Good catch! Thanks for letting me know.

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