local guns = {"Starter", "Rifle"} local gunVariables = { { cost = 0, level = 5 }, { cost = 200, level = 5 } } chosenClass.Changed:connect(function(newClass) gunLabel.Text = newClass for i, v in pairs(guns) do if v == newClass then for n, k in pairs(gunVariables[i]) do print(n, k) if n == level then print(n, k) levelLabel.Level.Text = k end end end end end)
I'm trying to iterate through the table of values. Each gun has its own level and cost. How can I get that to work in my code. I have if n == level then
to make sure it's got the right variable in the table, but it's not working. Please help
You should look into using a dictionary as well as better conventions. Regardless, when you do
for n, k in pairs(gunVariables[i]) do print(n, k) if n == level then
First, level should be a string, as the dictionary key is a string. Roblox only allows you to init the key without quotes for convenience. wiki Secondly You loop through gunVariables table in the order cost,level. So you should check if k == 'level'
rather than n. Most importantly since you know the order there is no reason to do this check in the first place.
Here's what I recommend your script looks like:
local guns = { Starter = {cost=0, level=5}, Rifle = {cost=200, level=5} } chosenClass.Changed:connect(function(newClass) gunLabel.Text = newClass local gunVars = guns[newClass] local cost, lvl = gunVars.cost, gunVars.level levelLabel.Level.Text = lvl end)