|
| 12 Jul 2017 08:49 PM |
Why do we use local variable, and what is an example?
-Please explain it as simple as possible. |
|
|
| Report Abuse |
|
|
BaiYuni
|
  |
| Joined: 09 Oct 2009 |
| Total Posts: 2861 |
|
|
| 12 Jul 2017 08:52 PM |
For scopes and I believe it runs faster?
http://wiki.roblox.com/index.php?title=Variable
Ex:
local boolVal = true
A local variable that exists within a certain scope can't be referred to from an outer scope.
function prntMsg() local message = "Message" print(message) --Would only print message when the function is called. end
prntMsg() --Would print 'Message'
print(message) --The variable doesn't exist because it only exists locally inside of the function. |
|
|
| Report Abuse |
|
|
|
| 12 Jul 2017 08:53 PM |
I'm bad at explaining, read this: wiki.roblox.com/index.php?title=Scope
|
|
|
| Report Abuse |
|
|
|
| 12 Jul 2017 09:42 PM |
| So your saying local variables can only be obtained by calling it from a function? |
|
|
| Report Abuse |
|
|
BaiYuni
|
  |
| Joined: 09 Oct 2009 |
| Total Posts: 2861 |
|
|
| 12 Jul 2017 09:48 PM |
No.
This would be an example from a Touched script that a person may create:
local part = script.Parent
part.Touched:connect(function (obj) local player = game.Players:GetPlayerFromCharacter(obj.Parent) if player then print(player.Name) end end)
print(player.Name)
Inside the Touched function, there is a print line 'print(player.Name)'. This will print the players name.
Outside of the function, I tried to call the print line again 'print(player.Name)', but 'player' does not exist. This is because it is local to the scope within the Touched function. |
|
|
| Report Abuse |
|
|
snowparks
|
  |
| Joined: 10 Apr 2017 |
| Total Posts: 64 |
|
|
| 12 Jul 2017 09:49 PM |
lets say u wanted to run the same function 7 times and it requires a variable each time
we dont need to define 7 different variables for each function, each one is specific to that iteration
|
|
|
| Report Abuse |
|
|
|
| 12 Jul 2017 10:47 PM |
| Still dont get it. Can you try to dumb it down as much as possible. |
|
|
| Report Abuse |
|
|
|
| 12 Jul 2017 11:01 PM |
Local variables are contained within a "box" (i guess boxes are more relatable?)
A box can be
function() end -- do
end -- if condition then
end -- while condition do
end -- repeat
until condition --
You can only access variables in the box that "you're" in
For example
do local variable = 5 --creating a variable in this box print(variable) --printing the variable above from this box end
print(variable) --this will print nil, since you are outside the box
Variables are accessible from any lower boxes as well (as that box is inside the other box
do local variable = 5 --creating a variable in this box print(variable) --printing the variable above from this box do --a box inside a box print(variable) --this will print '5', since this box is inside the other box local variable = 10 print(variable) --this will print '10', since you set the variable to 10 inside this box end print(variable) --this will print '5', not '10', as the variable was set to 10 inside the inner box, and "you" are not inside that inner box do variable = 8 --notice how there is no 'local' print(variable) --this will print '8' end print(variable) --this will also print '8', since 'local' defines the variable as inside a box, removing it means its accessible from outside it's box, up to whichever box it is set as local lastly (in this case, it is set at the beginning of the first box) end
|
|
|
| Report Abuse |
|
|
|
| 13 Jul 2017 03:39 PM |
| Ok, I understand. But whats the point using it? |
|
|
| Report Abuse |
|
|
|
| 13 Jul 2017 03:42 PM |
| Local variables aren't visible at fenv? (Using getfenv()) |
|
|
| Report Abuse |
|
|