Jump to content

Table Result


OrbTanT

Recommended Posts

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

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 by Shinigami
Link to comment

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 by Savannah
Link to comment

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...