Create a For k, v in pairs value

Hello, i want to know how i create a value for a for k, v in pairs loop.

So it should look like this:

for k, v in pairs(MyValue) d
    -- Code
end

And if i can use that with sql

I assume you don’t really understand lua much. I would really recommend you just watch some lua basics first.
Here are a few series/channels to watch:
Omni Ed - [Gmod] Lua Tutorial 1: General Setup - YouTube
CODE BLUE - [GMOD LUA] Ep. 1 Client, Server And Shared - YouTube
DanFMN - Fastest Way to Make a Garry's Mod Server - 2021 - Windows - SteamCMD - YouTube
Threebow - Garry's Mod Lua Tutorial | Part 1: Addon Structure, Variables, Functions, Hooks - YouTube
TheCodingBeast - DarkRP 2.5 Coding : Episode 1 : #1 Basic HUD - YouTube

As for your question, a for k, v in pairs loop takes a table as the argument and will run the code within for each value in the table. You can use it with any table so if you get a table outputted from SQL then yes you can.

3 Likes

You can do a lot of cool things with this.

for k, v in pairs(player.GetAll()) do
    v:ChatPrint("Hello!") -- Output to all players the word Hello!
end


for k, v in pairs(player.GetAll()) do
	if ply:Team() == TEAM_CITIZEN then
   	        v:ChatPrint("Hello!") -- Output to all players with the only CITIZEN profession, the word Hello!
	end
end

First of all thx that you gave me all this links. But i think i haven’t meantiont my error i get

[ERROR] gamemodes/mygamemode/gamemode/gmfiles/cl/my_menu.lua:63: bad argument #1 to 'pairs' (table expected, got nil)
  1. pairs - [C]:-1
   2. OpenMyMenu - gamemodes/mygamemode/gamemode/gmfiles/cl/my_menu.lua:63
    3. v - gamemodes/mygamemode/gamemode/gmfiles/cl/my_gm_key_binds.lua:37
     4. unknown - lua/includes/modules/hook.lua:84

Here is that what i got.
The data =

local data = sql.Query( "SELECT * FROM my_gm_data ")

And the Code from line 63

for k, v in pairs(data) do

I think you should make a habit of reading the documentation more often, to understand the errors you may find.

Like @Florian4016 you should try to understand the error, if you read the error it says “bad argument #1 to ‘pairs’ (table expected, got nil)”, it expects a table but it got nil, that means that the data variable you used as an argument is nil. So the SQL query isn’t returning any data. You can replace line 63 with this instead:

for k, v in pairs(data or {}) do

This means that even if the query doesn’t return any data it still won’t cause any errors.

I’m a bit late to the party but In this case, your query doesn’t return any data so make sure to check if your value names are correct or if there is actually any data in your database table. You should probably remove the extra whitespace behind “my_gm_data” as well, I’m not sure if that can break a query if not then it’s still good practice.

Another thing that could be the problem is that the query returns the data after you try to loop through it because queries take some time to run and get/set data but your code doesn’t wait for that. I’m not sure about the default SQL functions but with sqloo for example, there is an “onSuccess()” function callback that runs when the query finished running (duh). So you should look into that.