Learning LUA, got a question

so I’ve been trying to learn LUA lately and im making slow but somewhat acceptable progress considering its my first scripting language ever.

but I decided to gauge my progress by taking a simple wallhack and optimizing it to show item names and class and eventually player model color that changes depending on their job (if dark rp). but I’m running into a problem

first of all, I’m avoiding functions as much as possible here because I just get caught up in them a lot and confuse arguments with the value that calls them up. so even though it’s somewhat embarrassing, do you have any methods to help you remember how a function is set up in defining it and calling it? like what format would it take to define what it’s call value is? would it be



--to define
function hello (x)
     x-6
end

--to call
hello(2)

--output
-4


so if that’s somewhat oddly worded sorry I’m on a phone right now

final question:

in order to add the text information of an object I use ‘draw.DrawText’
this works (or should) but in order to call it I have to have the x and y coordinates of where it is displayed. I want it to show below the object so I just subtract from the y coordinate, but I’m confused in the fact that Entity:GetPos returns a vector as x,y. I need it to return ONLY x and then in another call ONLY y. I took a guess and tried Entity:GetPos.X but to no avail. anyone know how to get only one coordinate? I’ve considered just using GetPos and having the values placed on a table to be called when needed in the arguments of DrawText, but I’m new still and have no idea how this is done

again, sorry for any errors or confusion. I’m on a phone and I’ll fix problems I see when I get home. if you need to see the part of code im working on let me know and I’ll put it up

you’re doing pretty good from the looks of it, you’re just a little off. as far as your first project goes, I personally wouldn’t recommend working on a wallhack because a couple features from some of the better ones may be hard for a beginner to catch on to, and the majority of wallhacks that have already been made are poorly coded, and it will be hard to find good examples for you to learn from. I don’t really understand your question about functions, are you saying you forget what each function takes and what it gives back? and for your final question, in order to get a value from a table (vectors are essentially tables) it’s
[lua]Entity:GetPos().x[/lua]
but, all vectors in lua are 3 dimensional (so the values you would get wouldn’t be what you’re expecting in this case). use Vector:ToScreen to turn a 3D vector into a 2D table with x and y.

In regards to your first question, I tried my best to understand it but just say if this isn’t what you meant. It’s important to label your functions and arguments well to make sure you remember them.
[lua]
local function minusSix(number) – Name the function and arguments logically. E.g. minusSix (from) number and then it returns the result.
number = number - 6 – The result
return number – Returns the number
end
print(minusSix(10)) – This calls the print function with whatever is returned from the minusSix function. For example it does 10 - 6 and then prints the answer.
– Outputs 4
[/lua]

NOTE: just if it helps any, I’m indexing all the props to be displayed on a table and they’re called up by their value. something along these lines (assume the table’s values and keys are already inserted):



for k,v in pairs(props) do
    if v:IsValid then
        --[all the stuff about HUD paint, can't remember it rn]--
        draw.DrawText(v:GetClass(), v:GetPos.X(), v:GetPos.Y()-10,(0,0,255,0))
    end
end


sorry if this isn’t syntactically accurate, I wrote this off the top of my head from memory w/o reference, but this is generally how it works

[editline]4th November 2014[/editline]

Thanks for the reply. You answered it just fine, I was a bit confused on where things went when defining a function and how it was set up when calling one. Now I understand that I place the ‘name’ of my function after ‘function’ and the name of the input that is modified goes in the parenthesis, then the stuff the function does to that input goes after and is closed. After that I can call that function just by entering the name I gave it with an input in the parenthesis.

[editline]4th November 2014[/editline]

Hey, thanks for the reply. My question about functions was answered and I checked over your suggestion. I’ll test it when I get home but in the mean time, how would that function be used? would it just be put without arguments or would i need to do something like this:



for k, v in pairs(prop) do 
    if v:IsValid then
        --paint stuff again
        draw.DrawText(v:GetClass, v:GetPos:ToScreen(), (0,0,255,0))
    end
end


or would I have to somehow separate the GetPos part to separate x and y arguments?

You have to separate it and GetPos is a function so you need brackets.



local posToScreen = v:GetPos():ToScreen() -- calling this twice so might as well cache it.
local x,y = posToScreen.x, posToScreen.y
-- then your draw thing


oh I see. I got it now, will test when I get to a computer. Thank you all so much for the help.