generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Local Variables?

Previous Thread :: Next Thread 
AdministrativeFellow is not online. AdministrativeFellow
Joined: 07 Mar 2017
Total Posts: 72
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 is not online. 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
ExtremeBuilder15 is online. ExtremeBuilder15
Joined: 01 May 2012
Total Posts: 3176
12 Jul 2017 08:53 PM
I'm bad at explaining, read this: wiki.roblox.com/index.php?title=Scope

Report Abuse
AdministrativeFellow is not online. AdministrativeFellow
Joined: 07 Mar 2017
Total Posts: 72
12 Jul 2017 09:42 PM
So your saying local variables can only be obtained by calling it from a function?
Report Abuse
BaiYuni is not online. 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 is not online. 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
AdministrativeFellow is not online. AdministrativeFellow
Joined: 07 Mar 2017
Total Posts: 72
12 Jul 2017 10:47 PM
Still dont get it. Can you try to dumb it down as much as possible.
Report Abuse
Disillusions is not online. Disillusions
Joined: 10 Jul 2011
Total Posts: 6365
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
AdministrativeFellow is not online. AdministrativeFellow
Joined: 07 Mar 2017
Total Posts: 72
13 Jul 2017 03:39 PM
Ok, I understand. But whats the point using it?
Report Abuse
SquidyBroh is not online. SquidyBroh
Joined: 08 Jul 2015
Total Posts: 221
13 Jul 2017 03:42 PM
Local variables aren't visible at fenv? (Using getfenv())
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image