Calling a name of the table

Hey,

so battle of me and F tables continues.

I would like to be able to call a name of the table to which I’m gonna be close to. I can’t quite figure out how to do that exactly, without adding an extra line of something like Label value inside the config, as I see it redundant in this case.

Here’s the concept of it

Config = {}

Config.TestingShit = {
    ["test1"] = {
		Position = vector3(976.6364,70.2947,115.1641),
		DrawDistance = 50,
		State = false,
	},
	['test2'] = {
		Position = vector3(685.29, 569.65, 156.28),
		DrawDistance = 50,
		State = false,
	}
}
Citizen.CreateThread(function()
	while true do
		Wait(500)
		local pos = GetEntityCoords(PlayerPedId())   
		for k,v in pairs(Config.TestingShit) do
				local distance = #(pos - v.Position)				
				local DrawDistance
				
				if v.DrawDistance == nil then 
					DrawDistance = 500
				else
					DrawDistance = v.DrawDistance
				end
				
				if distance < DrawDistance then
					if not v.State then
						v.State = true
						print(Config.TestingShit..' is visible') -- here's where I want to get the name of the whole table, such as "test1" or "test2"
					end
				else
					if v.State then
						v.State = false
						--print(Config.TestingShit..' is INvisible')
					end
				end				
			
		end
	end
end)

I’ll appreciate any advice on this, thank you!

Hi,

When iterating using k,v in pairs, k represents the key in the array, and v the value

for k,v in pairs(table) do
  print(k,v)
end

So for you it would be :

Citizen.CreateThread(function()
	while true do
		Wait(500)
		local pos = GetEntityCoords(PlayerPedId())   
		for k,v in pairs(Config.TestingShit) do				
				if v.DrawDistance == nil then 
					v.DrawDistance = 500
				end
				
				if #(pos - v.Position) < v.DrawDistance then
					if not v.State then
						v.State = true
						print(k, 'is visible')
					end
				else
					if v.State then
						v.State = false
						--print(Config.TestingShit..' is INvisible')
					end
				end				
			
		end
	end
end)

I also rearranged your code :wink:

1 Like

Thank you!