Jump to content

[Help] Lua tables


Recommended Posts

Posted

I have a problem with lua tables.
I don't understand how I can get info from tables. For example. I have a db with zones and when resource starting I put all info in lua table

turfElement = {}

function onGangzonesLoad()
dbQuery(
		function(qh)
			local result = dbPoll(qh, -1)
				if result then
					for idZone,v in ipairs(result) do
					local turfCol = createColRectangle(v['gX'], v['gY'], v['gSizeX'], v['gSizeY'])
					local turfArea = createRadarArea(v['gX'], v['gY'], v['gtSizeX'], v['gtSizeY'], v['r'], v['g'], v['b'], 90)
					turfElement[idZone] = {turfCol, turfArea, idZone}
				end
			end
		end
	, db, "SELECT * FROM gangzones")
end
addEventHandler("onResourceStart", resourceRoot, onGangzonesLoad)

But when I want to get info, I have error

for idZone,v in pairs(turfElement) do
	outputChatBox("" .. v.turfArea["gX"] .. "", player, 255, 255, 255)
end
attempt to index field 'turfArea' <a nil value>

What's wrong?

 

  • Moderators
Posted
v.turfArea

Is nil.

 


Normally you would inspect your table structure first:

iprint(turfElement)

 

 


 

Save everything first.

turfElement[idZone] = {turfCol = turfCol, turfArea = turfArea, idZone = idZone,
gX = v.gX, gY = v.gY, gtSizeX = v.gtSizeX, gtSizeY = v.gtSizeY
}

 

Then this might work.

for idZone,v in pairs(turfElement) do
	outputChatBox("" .. v.gX .. "", player, 255, 255, 255)
end

 

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

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...