1. Is there any good gmod lua tutorials around? The ones on the wiki are abit.. Bland. I got to http://maurits.tv/data/garrysmod/wik...he_for_in_loop and then when I got to the abit about k and v I just got lost.
The next two letters, k and v, are the names of the variables in the loop. If you remember, in the for loop we had one variable, "i". "i" was a number that was increased each time the loop run. In the for in loop, "k" is the slot the loop is on, and v is what's inside of the slot. Take a look back at our code:
I just got lost.

2. I suggest http://www.lua.org/pil/ for learning the basic's of lua

when I got to the abit about k and v I just got lost.
I just got lost.

3. Is there any good gmod lua tutorials around? The ones on the wiki are abit.. Bland. I got to http://maurits.tv/data/garrysmod/wik...he_for_in_loop and then when I got to the abit about k and v I just got lost.
I just got lost.

Do you know how a table is contructed?
tbl = {}
tbl[key] = "value"

For example:
```tbl[1] = "foo"
tbl[2] = "bar"
for k, v in pairs(tbl) do --for key, value in pairs
Msg(v .. " ")
end

for k, v in pairs(tbl) do
Msg(k .. " ")
end
```
> foo bar
> 1 2
Lua Helper x 1 (list)

4. I get the whole tables bit. Just didn't understand where k and V came into it. So V is displaying what is stored in that table entry and K is for the numbers that are stored in that table? So like

```
tbl[1] = "My"
tbl[2] = "Name"
tbl[3] = "is"
tbl[4] = "Neddy"

for k, v in pairs(tbl) do --for key, value in pairs
Msg(v .. " ")
end

for k, v in pairs(tbl) do
Msg(k .. " ")
end
```

> My Name is Neddy
> 1 2 3 4

?
Agree x 3 (list)

5. I get the whole tables bit. Just didn't understand where k and V came into it. So V is displaying what is stored in that table entry and K is for the numbers that are stored in that table? So like

```
tbl[1] = "My"
tbl[2] = "Name"
tbl[3] = "is"
tbl[4] = "Neddy"

for k, v in pairs(tbl) do --for key, value in pairs
Msg(v .. " ")
end

for k, v in pairs(tbl) do
Msg(k .. " ")
end
```

> My Name is Neddy
> 1 2 3 4

?
k stands for key, which is the index of the table
v stands for value, which is the value stored at that index

doing a for you can make the k and v what ever you want like if you were doing players you could do

```
for useless, ply in pairs(player.GetAll()) do
ply:ChatPrint("Hi!!")
end

```

i called the first useless because i'm not using it, sometimes people use _ as the first if they aren't using the key, and the second I called ply because it's a player object.

You pretty much got the idea with the example you posted back, just thought i'd fill in some more info.
Lua Helper x 2 (list)

6. The key CAN be a non-numeric value, but is set to a numeric if it's set automatic.
You can do:
```tbl["Name"] = "John"
tbl["Age"] = 12

for key, value in pairs(tbl) do
print(k .. " - " .. v)
end
```
> Name - John
> Age - 12
Agree x 1 (list)

7. Ah. Well for one, I don't have the screen clicker enabled. That's probably why you can't rotate with the mouse. I'd remove the "gui.EnableScreenClicker(true)" line and limit the player's range some other way.
I've done this
```function GM:CreateMove(cmd)
if LocalPlayer():Alive() then
gui.EnableScreenClicker(true)
local mx,my = gui.MousePos()
local pos = LocalPlayer():GetShootPos():ToScreen()
pos = Vector(pos.x,pos.y,0)

local ang = (Vector(mx,my,0)-pos):Angle()

cmd:SetViewAngles(ang)
end
end
```

That's working (a bit wrong as its back to front) but It's not exactly what I want. What I'm wanting is a top-down view where the player rotates when you move your mouse around but the camera is static and won't rotate (So you can aim in different directions around you.) It needs to actually aim there as-well tho so flash light and weapons work.

I'm guessing I shouldn't use SetViewAngles for this, but what else is there?

8. I'm coming from C++ and i have a question. I'm looking at Lua for C Programmers in the Garry's Mod wiki but i cant find any answer to my question.

For example

```local meta = FindMetaTable("Player")

function meta:WowFunction(command)
self:ConCommand(command)
self:ChatPrint("Wow function ran the console command: " .. tostring(command))
end

ply = LocalPlayer()
ply:WowFunction()
```

is the colon means the same thing as Unary Scope Resolution Operator in C++ ( :: ) ?

9. I'm coming from C++ and i have a question. I'm looking at Lua for C Programmers in the Garry's Mod wiki but i cant find any answer to my question.

For example

```local meta = FindMetaTable("Player")

function meta:WowFunction(command)
self:ConCommand(command)
self:ChatPrint("Wow function ran the console command: " .. tostring(command))
end

ply = LocalPlayer()
ply:WowFunction()
```

is the colon means the same thing as Unary Scope Resolution Operator in C++ ( :: ) ?
I don't know c++, but I think yes.
That code should work, except you didn't parse anything as argument 1 on line 9 to ply:WowFunction()

10. obj:function() is syntactic sugar for obj.function(obj). In declarations it's syntactic sugar for obj.function(self)
Useful x 1 (list)

11. I've done this
```function GM:CreateMove(cmd)
if LocalPlayer():Alive() then
gui.EnableScreenClicker(true)
local mx,my = gui.MousePos()
local pos = LocalPlayer():GetShootPos():ToScreen()
pos = Vector(pos.x,pos.y,0)

local ang = (Vector(mx,my,0)-pos):Angle()

cmd:SetViewAngles(ang)
end
end
```

That's working (a bit wrong as its back to front) but It's not exactly what I want. What I'm wanting is a top-down view where the player rotates when you move your mouse around but the camera is static and won't rotate (So you can aim in different directions around you.) It needs to actually aim there as-well tho so flash light and weapons work.

I'm guessing I shouldn't use SetViewAngles for this, but what else is there?
Didn't explain that well but basically is there a function to set the players rotation? (So he faces the mouse all the time) and the camera can stay static facing down and not rotate.

12. I am looking for a way to make a hud that i can move with a derma panel slider control that accords to the y and x axis.

13. I'm coming from C++ and i have a question. I'm looking at Lua for C Programmers in the Garry's Mod wiki but i cant find any answer to my question.

For example

```local meta = FindMetaTable("Player")

function meta:WowFunction(command)
self:ConCommand(command)
self:ChatPrint("Wow function ran the console command: " .. tostring(command))
end

ply = LocalPlayer()
ply:WowFunction()
```

is the colon means the same thing as Unary Scope Resolution Operator in C++ ( :: ) ?
It's somewhat similar. In C++ it's used to access a class/namespace function (I believe, I'm still learning C++). In Lua, you are passing meta data as the first arg in the function. Like this:

```local meta = FindMetaTable("Player");
```

You have now made a variable storing meta data for the Player metatable locally.

You can do
```function meta:WowFunc( command );
```

or you can do

```function meta.WowFunc(player_object, command);
```

Like GranPC said, it's just a fancier/cleaner way of doing things.

14. I personally prefer to do _R.Player:WowFunc
Agree x 5 (list)

15. Didn't explain that well but basically is there a function to set the players rotation? (So he faces the mouse all the time) and the camera can stay static facing down and not rotate.
Player.SetEyeAngles can be used to modify a Player's current facing. Calling it clientside wouldn't do anything other than compensation, though, so you'll probably want to call it somewhere in your shared.lua. The problem would be finding a way to transfer the angle between the player and the cursor into it.

I recommend calling it somewhere during Gamemode.Move because this runs constantly and it's relevant.

16. I personally prefer to do _R.Player:WowFunc
FindMetaTable("Player") basically returns _R.Player, it's no different. It just looks different.
Agree x 1 (list)

17. Anyone know of a hacky way to find out if an entity is on fire clientside? I know entity.IsOnFire is shared in GMod 13 but I kinda need it now.

18. ```function _R.Entity:IsOnFire()
for k,v in pairs(ents.FindByClass("entityflame")) do
if (v:GetParent() == self) then
return true
end
end
return false
end
```
Useful x 1 (list)

19. Hi, Im a newfag to lua, and I've recently been accepted into a HL2RP development team for a really nice and popular community I play in. Anyways, here's the deal, Im supposed to create a script that plays random metrocop radio chatter whenever a metrocop(s) is near. The chatter basically isnt the metrocop speaking, its just "imaginary" metrocops reporting in stuff and etc. Actually just like you hear combine chatter in HL2. Should I do this by making a function which then would be called automatically upon server start? And how? Help would be very appreciated.

Piciul
Lua King x 2 (list)

20. FindMetaTable("Player") basically returns _R.Player, it's no different. It just looks different.
yeah, just my preference.

21. Hi, Im a newfag to lua, and I've recently been accepted into a HL2RP development team for a really nice and popular community I play in. Anyways, here's the deal, Im supposed to create a script that plays random metrocop radio chatter whenever a metrocop(s) is near. The chatter basically isnt the metrocop speaking, its just "imaginary" metrocops reporting in stuff and etc. Actually just like you hear combine chatter in HL2. Should I do this by making a function which then would be called automatically upon server start? And how? Help would be very appreciated.

Piciul
So wait, you were accepted to the development team without knowing Lua?
Funny x 3 Agree x 1 (list)

22. So wait, you were accepted to the development team without knowing Lua?
Oh, I do know little bits of it. Just not the advanced stuff, I just wanted to edit my post saying I found out half the shit I listed above, right now I only need help on how to make it play only when CPs are near. And how not to make it play for players who are out of range. You're right though, my post sure does express that I dont know shit about lua.
Dumb x 1 Optimistic x 1 Lua King x 1 (list)

23. Use Entity.FindInSphere and EmitSound. You should probably use a timer or the Think hook along with setting some sort of next play time kind of variable for the player to know when to emit the sound.
Lua King x 1 (list)

24. I am looking for a way to make a hud that i can move with a derma panel slider control that accords to the y and x axis.

Should I use Convars for this?

25. Yes, it's also how most STools handle their properties.

26. Yes, it's also how most STools handle their properties.
I tried that and the only way it updates is if I reload the script.

27. Is the position of the HUD being updated based on the convar?

28. Is the position of the HUD being updated based on the convar?
```CreateClientConVar("xpos", "0", true, false)
CreateClientConVar("ypos", "0", true, false)
local xpos = GetConVarNumber("xpos")
local DermaPanel = vgui.Create( "DFrame" )
DermaPanel:SetPos( 50,50 )
DermaPanel:SetSize( 200, 250 )
DermaPanel:SetTitle( "Testing Derma Stuff" )
DermaPanel:SetVisible( true )
DermaPanel:SetDraggable( true )
DermaPanel:ShowCloseButton( true )
DermaPanel:MakePopup()

local NumSlider = vgui.Create( "DNumSlider", DermaPanel )
NumSlider:SetPos( 25,50 )
NumSlider:SetWide( 150 )
NumSlider:SetText( "Max Props" )
NumSlider:SetMin( 0 ) -- Minimum number of the slider
NumSlider:SetMax( ScrH() ) -- Maximum number of the slider
NumSlider:SetDecimals( 0 ) -- Sets a decimal. Zero means it's a whole number
NumSlider:SetConVar( "xpos" ) -- Set the convar
end)
function HUDPAINT()
if POINTSHOP.Config.DisplayPoints then
local text = "Points: " .. LocalPlayer():PS_GetPoints()
if LocalPlayer():Alive() then
draw.WordBox( 4, xpos,100, text, "ScoreboardText", Color(0,0,0,b), Color(255,255,255,a))
draw.WordBox( 4, 10,10, "Shop[F4]", "ScoreboardText", Color(0,0,0,shopalpha), Color(255,255,255,shoptxtalpha))
end
points = LocalPlayer():PS_GetPoints()
end)
end
end
```

29. that sets it to xpos when it's created, use GetConVarNumber( "xpos" ) in the draw code

30. How am I able to tell whether or not the player is looking at a door ?

31. How am I able to tell whether or not the player is looking at a door ?
```function _R.Player:LookingAtDoor()
local door=self:GetEyeTraceNoCursor().Entity
return door:GetClass()=="prop_door_rotating" -- Something like this
end
```

32. Get the door entity and check what class it is.

Code:
`lua_run print( string.find(player.GetByID(1):GetEyeTrace().Entity:GetClass(), "door") )`

33. that sets it to xpos when it's created, use GetConVarNumber( "xpos" ) in the draw code
local xpos = GetConVarNumber("xpos")

isnt that the same thing?

34. How do I spawn an entity in a function, say I want to have an NPC drop an entity item when killed ?

35. Use GM:OnNPCKilled() and ents.Create() for spawning the weapon.

36. local xpos = GetConVarNumber("xpos")

isnt that the same thing?
You're only setting xpos when the script first runs. You have to set it every time your HUD is drawn.

37. You're only setting xpos when the script first runs. You have to set it every time your HUD is drawn.
Care to explain how?

38. ```CreateClientConVar("xpos", "0", true, false)
CreateClientConVar("ypos", "0", true, false)
local DermaPanel = vgui.Create( "DFrame" )
DermaPanel:SetPos( 50,50 )
DermaPanel:SetSize( 200, 250 )
DermaPanel:SetTitle( "Testing Derma Stuff" )
DermaPanel:SetVisible( true )
DermaPanel:SetDraggable( true )
DermaPanel:ShowCloseButton( true )
DermaPanel:MakePopup()

local NumSlider = vgui.Create( "DNumSlider", DermaPanel )
NumSlider:SetPos( 25,50 )
NumSlider:SetWide( 150 )
NumSlider:SetText( "Max Props" )
NumSlider:SetMin( 0 ) -- Minimum number of the slider
NumSlider:SetMax( ScrH() ) -- Maximum number of the slider
NumSlider:SetDecimals( 0 ) -- Sets a decimal. Zero means it's a whole number
NumSlider:SetConVar( "xpos" ) -- Set the convar
end)
function HUDPAINT()
if POINTSHOP.Config.DisplayPoints then
local text = "Points: " .. LocalPlayer():PS_GetPoints()
local xpos = GetConVarNumber("xpos")  -- by moving it here
if LocalPlayer():Alive() then
draw.WordBox( 4, xpos,100, text, "ScoreboardText", Color(0,0,0,b), Color(255,255,255,a))
draw.WordBox( 4, 10,10, "Shop[F4]", "ScoreboardText", Color(0,0,0,shopalpha), Color(255,255,255,shoptxtalpha))
end
points = LocalPlayer():PS_GetPoints()
end)
end
end
```

39. Since you're opening the script, it will seem like the HUD is working. It's only being called one.
You have to hook the function to the HUDPaint hook.

40. Is there a list of methods available for various things?
Like a list of things you can do to a player in your code, and variables of a player you can change?
Also, when i'm writing scripts for darkrp, how do i know where to put them? Like, if i wanted to make a script execute when a player types a command, where would i put that code?