I hear people say all the time, while i'm watchin them script, that Local is not needed but they want it there. WHY? What is its purpose???
What is the purpose of keyword "Local"?
2 answers
Hi BossScorp82103,
I don't have much time to give you a long answer, so I am going to get straight to the point of what local does. Local allows you to make your functions or variables 'local' meaning that it is only used in that specific scope of the script. Making variables local is much better than allowing them to be global because it causes less processing for the program when the program only has to look at a small number of variables.(Basically, it can reduce lag) Anyway, I hope I helped and have a nice day/night.
Thanks,
Best regards,
KingLoneCat
Local is a variable just for that script but there something that you might get confused about. Code Below shows you what should your code look like.
local Print_Text = 'I'm Rocking the bus.'
print(Print_Text)
The code would print I'm Rocking the bus. in the output but if you put it like this.
print(Print_Text)
local Print_Text = 'I'm Rocking the bus.'
It would not work since the variable is is after the use of it so it couldn't be pulled from the variable. You can also have global variables witch you can create by just not adding the local. Global variable can be used over bunch of scripts.
So you can have local:
- Strings
- Numbers
- Bool Values
- Pieces of Code
Global you can use:
- Strings
- Numbers
- Bool Values
- Pieces of Code
- Patterns (Used For String Manipulation)
1 Patterns
2 string.byte
3 string.char
4 string.dump
5 string.find
6 string.format
7 string.len
8 string.lower
9 string.match
10 string.rep
11 string.reverse
12 string.sub
13 string.upper
14 string.gmatch
15 string.gsub
Roblox Lua, there is a third type of variable called a shared variable. Shared variables are variables that are visible to every script (that is, they are shared between them). Some examples of a shared variable include print, game, or Instance. A list of all shared variables can be found at the Function Dump.
Remember that there are some major differences between normal Lua and Roblox Lua.
- In normal Lua, these variables are considered global variables, because they are completely visible in the global scope.
In Roblox Lua, these variables have been separated from the global scope because Roblox uses multiple scripts, each having their own environment. In Roblox Lua, these variables cannot be modified for security reasons.
This discussion is preserved as it was. New answers, comments, and votes are closed.
