OrbTanT Posted December 4, 2016 Share Posted December 4, 2016 Help me, I'm kinda bad that tables, but how can I get the total result of number that are in table for example. local lucre = {1500, 2500, 1000, 5000} I tried to loop in the lucre table, plus it returns each separate value as 1500, 2500, how can I retrieve the table data and add them up before passing through the outputChatBox Link to comment
Savannah Posted December 4, 2016 Share Posted December 4, 2016 (edited) You can count them up this way: local total = 0 for _,v in pairs(lucre) do total = total + v end Edited December 4, 2016 by Savannah Link to comment
OrbTanT Posted December 4, 2016 Author Share Posted December 4, 2016 (edited) Almost did work, but it was returning the sum in the outputChatBox until it arrived in the result, I wanted the outputChatBox to return only the total result, already added local lucre = {1500, 2500, 1000, 5000} local total = 0 function loopLucre(player, comandName) for _,v in pairs(lucre) do total = total + v outputChatBox("My lucre is "..tostring(total), player, 255, 255, 0) end end addCommandHandler("lucre", loopLucre) he returned: My lucre is 1500 My lucre is 4000 My lucre is 5000 My lucre is 10000 I wanted it to just come back: My lucre is 10000 Edited December 4, 2016 by Shinigami Link to comment
Savannah Posted December 4, 2016 Share Posted December 4, 2016 (edited) Just move the outputChatBox line after the loop. You use the loop purely for counting here, you can output after. local lucre = {1500, 2500, 1000, 5000} local total = 0 function loopLucre(player, comandName) for _,v in pairs(lucre) do total = total + v end outputChatBox("My lucre is "..tostring(total), player, 255, 255, 0) end addCommandHandler("lucre", loopLucre) Edited December 4, 2016 by Savannah Link to comment
OrbTanT Posted December 4, 2016 Author Share Posted December 4, 2016 Works perfectly, thanks, now I can finish property system 1 Link to comment
Savannah Posted December 4, 2016 Share Posted December 4, 2016 Also one small thing, you might want to move the local total = 0 variable into the function above the loop, so the total gets reset everything you execute the command. Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now