Return to Main Page

Variables

Page white text.png Description:This is an introduction to the lua language in general and is a recommended read if you are a beginner.
link=User:Dim1xs Author:Dim1xs
Calendar.png Created:September 29th 2023

Defining variables

Variables let your script hold information. Using a variable you can put aside some data for later and then call it back whenever you want. Variables themselves don't have a type assigned to them and they can hold absolutely anything.

Variable can store: string, float, int, bool etc. A variable can be created by doing this:

myName = "Dmitry"
               
Or in such variants:

myName = "Dmitry"
myHeight = "173"
isWearingGlasses = false
               
And you can return this data (excluding boolean), or change it:

myName = "Dmitry"
myHeight = "173"
isWearingGlasses = false

print( "Your name is:"..myName)
print( "Your height is:"..myHeight)
if isWearingGlasses == false then
   print( "Do you wear glasses?: No")
else
   print( "Do you wear glasses?: Yes")
end
               

Running the script

Now, run your script, and if you did everything right, the console will show you this:
Your name is:Dmitry
Your height is:173
Do you wear glasses?: No
               

Changing the created variables

You can change already created Var's like that:

myName = "Dmitry"
myHeight = "173"
isWearingGlasses = false
isNerd = nil

print( "Your name is:"..myName)
print( "Your height is:"..myHeight)
if isWearingGlasses == false then
   print( "Do you wear glasses?: No")
   isNerd = false
else
   print( "Do you wear glasses?: Yes")
   isNerd = true
end
               

Combining the strings

You can combine the strings with 2 ways.

First way is simpliest, by putting , between strings:

print( "Your name is: Dmitry" , "Your height is: 173")
               
Output:

Your name is: Dmitry Your height is: 173
               
The second way can be done like this (we did it already above):

myName = "Dmitry"
myHeight = "173"

print( "Your name is:"..myName)
print( "Your height is:"..myHeight)
               
Output:

Your name is:Dmitry
Your height is:173
               
And you can combine them:

myName = "Dmitry"
myHeight = "173"

print( "Your name is:"..myName , "Your height is:"..myHeight )
               
Output:

Your name is:Dmitry  Your height is:173
               

Thank you for reading this tutorial, see ya later!

JOIN HL2GMED DISCORD SERVER!