Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What am I doing wrong with my for loop and table?

Asked by 7 years ago
Edited 7 years ago

Okay. Just a disclaimer, I'm still trying to figure out LUA syntax again after leaving it for C++ and vb.net (those are both a lot of fun, highly encourage learning them.) But I really wanted to come back to LUA and see if I can't get where I am with LUA like I am with vb.net and C++.

Edit I really missed the mark with this snippet... I'm going to try to rebuild it from scratch, if you think you can help me, go ahead, but there are evidently a lot more problems than just starting the for loop on line 7.

stocks = {KNG = 250, OCT = 1500, SHL = 2000, SNY = 4350, DEL = 8750, MCR = 25000}

function newValue(stock)
        stock = stock + math.random(-(2*stock), (2*stock))
end

for each in stocks do
    newValue(each)
    print(each)
end

print(stocks)

The error says Workspace.priceVal:7: attempt to call a table value

1 answer

Log in to vote
0
Answered by 7 years ago

Problem:

Your syntax for the pairs method is all messed up and also, if you want to just print what each of the values are when you call the function you would do the following. However, if you want to do something additional just comment below and tell me what else you want it to do and I will try my best to help you. I am going to comment through my script and will explain it the best I can...

Here is my script:

stocks = {KNG = 250, OCT = 1500, SHL = 2000, SNY = 4350, DEL = 8750, MCR = 25000} -- Your table which you set up. (Hopefully no explaination needed seeing as you set it up)

function newValue(stock) -- Your function with the parameter of 'stock'
        stock = stock + math.random(-(2*stock), (2*stock)) -- Your mathematical equation/solution
        return stock -- This returns whatever stock equals after the calculations.
end -- End for function above.

for _, each in pairs(stocks) do  -- This is where you might not know what to do. There are several ways to do the pairs method however, the easiest way to remember them is as two values which pretain to the table which is in the parenthesis.
    print(newValue(each)) -- This prints whatever the function up there returns. 'each' being the value. It gets the value from the table and then does the equation with that number and then returns it and prints it here.
end -- This end is for the pairs above. 

-- I saw that you printed the table out here. Which I have no idea what you were trying to accomplish there, however, if you wanted to print all the values inside of that table at the top you had to use the pairs method.... Just like the above one... 

Here is a website where you can learn more about the pairs method.

I hope I helped in one way or another and thank you for reading this answer. If you have any questions feel free to post a comment below.

~~ KingLoneCat

0
I assumed you were trying to get the numbers from the table use them with the equation in your function and then print each of them. Which the above script does. If you want to know anything else just let me know... KingLoneCat 2642 — 7y
Ad

Answer this question