0

I have a table with dynamic keys and values, and I wanna loop thru it with a generic for loop, however, since the tables values are dynamic, how can I get the value of the dynamic variable? I hope this makes any sense. Let me show it with code to better explain.

Local weapons = {
    [GetHashKey('WEAPON_PISTOL')] = { [Strings['supp_name']] = GetHashKey('component_at_pi_supp_02'), [Strings['flash_name']] = GetHashKey('COMPONENT_AT_PI_FLSH'), [Strings['grip_name']] = nil, skin = GetHashKey('COMPONENT_PISTOL_VARMOD_LUXE') },
    [GetHashKey('WEAPON_PISTOL50')] = { [Strings['supp_name']] = GetHashKey('COMPONENT_AT_AR_SUPP_02'), [Strings['flash_name']]  = GetHashKey('COMPONENT_AT_PI_FLSH'), [Strings['grip_name']] = nil, skin = GetHashKey('COMPONENT_PISTOL50_VARMOD_LUXE') },
    [GetHashKey('WEAPON_COMBATPISTOL')] = { [Strings['supp_name']] = GetHashKey('COMPONENT_AT_PI_SUPP'), [Strings['flash_name']]  = GetHashKey('COMPONENT_AT_PI_FLSH'), [Strings['grip_name']] = nil, skin = nil },
    [GetHashKey('WEAPON_APPISTOL')] = { [Strings['supp_name']] = GetHashKey('COMPONENT_AT_PI_SUPP'), [Strings['flash_name']]  = GetHashKey('COMPONENT_AT_PI_FLSH'), [Strings['grip_name']] = nil, skin = GetHashKey('COMPONENT_APPISTOL_VARMOD_LUXE') },
    [GetHashKey('WEAPON_HEAVYPISTOL')] = { [Strings['supp_name']] = GetHashKey('COMPONENT_AT_PI_SUPP'), [Strings['flash_name']]  = GetHashKey('COMPONENT_AT_PI_FLSH'), [Strings['grip_name']] = nil, skin = GetHashKey('COMPONENT_HEAVYPISTOL_VARMOD_LUXE') },
    [GetHashKey('WEAPON_VINTAGEPISTOL')] = { [Strings['supp_name']] = GetHashKey('COMPONENT_AT_PI_SUPP'), [Strings['flash_name']]  = nil, [Strings['grip_name']] = nil, skin = nil },
    [GetHashKey('WEAPON_SMG')] = { [Strings['supp_name']] = GetHashKey('COMPONENT_AT_PI_SUPP'), [Strings['flash_name']]  = GetHashKey('COMPONENT_AT_AR_FLSH'), [Strings['grip_name']] = nil, skin = GetHashKey('COMPONENT_SMG_VARMOD_LUXE') },
    [GetHashKey('WEAPON_MICROSMG')] = { [Strings['supp_name']] = GetHashKey('COMPONENT_AT_AR_SUPP_02'), [Strings['flash_name']]  = GetHashKey('COMPONENT_AT_PI_FLSH'), [Strings['grip_name']] = nil, skin = GetHashKey('COMPONENT_MICROSMG_VARMOD_LUXE') },
    [GetHashKey('WEAPON_ASSAULTSMG')] = { [Strings['supp_name']] = GetHashKey('COMPONENT_AT_AR_SUPP_02'), [Strings['flash_name']]  = GetHashKey('COMPONENT_AT_AR_FLSH'), [Strings['grip_name']] = nil, skin = nil },
    [GetHashKey('WEAPON_ASSAULTRIFLE')] = { [Strings['supp_name']] = GetHashKey('COMPONENT_AT_AR_SUPP_02'), [Strings['flash_name']]  = GetHashKey('COMPONENT_AT_AR_FLSH'), [Strings['grip_name']] = GetHashKey('COMPONENT_AT_AR_AFGRIP'), skin = GetHashKey('COMPONENT_ASSAULTRIFLE_VARMOD_LUXE') },
    [GetHashKey('WEAPON_CARBINERIFLE')] = { [Strings['supp_name']] = GetHashKey('COMPONENT_AT_AR_SUPP'), [Strings['flash_name']]  = GetHashKey('COMPONENT_AT_AR_FLSH'), [Strings['grip_name']] = GetHashKey('COMPONENT_AT_AR_AFGRIP'), skin = GetHashKey('COMPONENT_CARBINERIFLE_VARMOD_LUXE') },
    [GetHashKey('WEAPON_ADVANCEDRIFLE')] = { [Strings['supp_name']] = GetHashKey('COMPONENT_AT_AR_SUPP'), [Strings['flash_name']]  = GetHashKey('COMPONENT_AT_AR_FLSH'), [Strings['grip_name']] = nil, skin = GetHashKey('COMPONENT_ADVANCEDRIFLE_VARMOD_LUXE') },
    [GetHashKey('WEAPON_SPECIALCARBINE')] = { [Strings['supp_name']] = GetHashKey('COMPONENT_AT_AR_SUPP_02'), [Strings['flash_name']]  = GetHashKey('COMPONENT_AT_AR_FLSH'), [Strings['grip_name']] = GetHashKey('COMPONENT_AT_AR_AFGRIP'), skin = nil },
    [GetHashKey('WEAPON_BULLPUPRIFLE')] = { [Strings['supp_name']] = GetHashKey('COMPONENT_AT_AR_SUPP'), [Strings['flash_name']]  = GetHashKey('COMPONENT_AT_AR_FLSH'), [Strings['grip_name']] = GetHashKey('COMPONENT_AT_AR_AFGRIP'), skin = nil },
    [GetHashKey('WEAPON_ASSAULTSHOTGUN')] = { [Strings['supp_name']] = GetHashKey('COMPONENT_AT_AR_SUPP'), [Strings['flash_name']]  = GetHashKey('COMPONENT_AT_AR_FLSH'), [Strings['grip_name']] = GetHashKey('COMPONENT_AT_AR_AFGRIP'), skin = nil },
    [GetHashKey('WEAPON_HEAVYSHOTGUN')] = { [Strings['supp_name']] = GetHashKey('COMPONENT_AT_AR_SUPP_02'), [Strings['flash_name']]  = GetHashKey('COMPONENT_AT_AR_FLSH'), [Strings['grip_name']] = GetHashKey('COMPONENT_AT_AR_AFGRIP'), skin = nil },
    [GetHashKey('WEAPON_BULLPUPSHOTGUN')] = { [Strings['supp_name']] = GetHashKey('COMPONENT_AT_AR_SUPP_02'), [Strings['flash_name']]  = GetHashKey('COMPONENT_AT_AR_FLSH'), [Strings['grip_name']] = GetHashKey('COMPONENT_AT_AR_AFGRIP'), skin = nil },
    [GetHashKey('WEAPON_PUMPSHOTGUN')] = { [Strings['supp_name']] = GetHashKey('COMPONENT_AT_SR_SUPP'), [Strings['flash_name']]  = GetHashKey('COMPONENT_AT_AR_FLSH'), [Strings['grip_name']] = nil, skin = nil },
    [GetHashKey('WEAPON_MARKSMANRIFLE')] = { [Strings['supp_name']] = GetHashKey('COMPONENT_AT_AR_SUPP'), [Strings['flash_name']]  = GetHashKey('COMPONENT_AT_AR_FLSH'), [Strings['grip_name']] = GetHashKey('COMPONENT_AT_AR_AFGRIP'), skin = nil },
    [GetHashKey('WEAPON_SNIPERRIFLE')] = { [Strings['supp_name']] = GetHashKey('COMPONENT_AT_AR_SUPP_02'), [Strings['flash_name']]  = nil, [Strings['grip_name']] = nil, skin = nil },
    [GetHashKey('WEAPON_COMBATPDW')] = { [Strings['supp_name']] = nil, [Strings['flash_name']]  = GetHashKey('COMPONENT_AT_AR_FLSH'), [Strings['grip_name']] = GetHashKey('COMPONENT_AT_AR_AFGRIP'), skin = nil }
}

local Strings = {
    ['removed_supp'] = 'You removed your suppressor',
    ['removed_grip'] = 'You removed your weapon grip',
    ['removed_flash'] = 'You removed your weapon flashlight',
    ['no_fit'] = '%s does not fit your current weapon',
    ['removed'] = 'You have removed your %s',
    ['used'] = 'You have used your %s',
    ['webhookMSG'] = '**Spiller Navn:** %s \n**ID:** %s \n**Tried to spawn:** %s',
    ['supp_name'] = 'suppressor',
    ['supp_Rname'] = 'Suppressor',
    ['supp_desc'] = 'Can be used to make your weapon silent when shooting.',
    ['flash_name'] = 'flashlight',
    ['flash_Rname'] = 'Flashlight',
    ['flash_desc'] = 'Can be attached to a weapon to give it a flashlight.',
    ['grip_name'] = 'grip',
    ['grip_Rname'] = 'Grip',
    ['grip_desc'] = 'Can be attached on a weapon to get a better grip.',
    ['choice_use'] = 'Use'
}

As you can see in the table, the values depend on the value from Strings table. So if I wanna do a for loop, and get the correct value how would I do it?

for k,v in pairs(weapons) do
   if k == GetHashKey('WEAPON_PISTOL') then
      print(v.<what here>) -- how do i get the v.Strings['supp_name']?
   end
end

I hope this makes any sense, if not, please ask in the comments.

Thanks, Ossie

1
  • indexing with a dot is only possibe if the table key is a valid Lua identifier. if you want to use anything else use square brackets. you can also use variables as table keys. please read the Lua manual Commented Jan 18, 2022 at 10:24

1 Answer 1

3

just use v[Strings['supp_name']]

Sign up to request clarification or add additional context in comments.

1 Comment

Yep thanks, figured that out just now :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.