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: Simple(ish) variables question

Previous Thread :: Next Thread 
Snixxus123 is not online. Snixxus123
Joined: 31 Mar 2011
Total Posts: 125
26 Jul 2017 12:55 PM
What's the difference between, say...

brick = script.Parent

and

local brick = script.Parent


Does the 'local' variable make that variable only usable for the script it's define in or something?
Report Abuse
Lord_Narwhal is not online. Lord_Narwhal
Joined: 05 May 2012
Total Posts: 3242
26 Jul 2017 12:57 PM
the difference is

brick = script.Parent

function doStuff()
brick = something
end

this will work

local brick = script.Parent

function doStuff()
brick = something
end

this will not work

local vars only exist within the scope that they're set in


Report Abuse
Julian_Orteil is not online. Julian_Orteil
Joined: 14 Aug 2012
Total Posts: 968
26 Jul 2017 01:03 PM
When using "local" it is protecting the variable. Meaning:

If I do this:

brick.Transparency = 0.5
brick = script.Parent

The transparency will be applied because the variable is public. The parser knows what "brick" is in this instance so therefore, it applies the transparency.

However, if I do:

brick.Transparency = 0.5
local brick = script.Parent

The transparency will not be applied and the script will error saying "brick" is non-existent.

Also, "local" can be used to make variables function/iterator protected.

Like so:

function Func1()
local brick = script.Parent
end

Func1()

brick.Transparency = 0.5


The script will error out saying brick doesn't exist because "local" makes brick only accessible within the function and sub-functions.

local can also be applied to functions.

Func1()
local function Func1()
print("hello")
end

This will error out because "Func1()" is called before the function is created.

Another thing is that not applying "local" to variables within functions/iterators also makes them public; however, calling "brick" before the function will error out because "brick" isn't created until the function is called.


Sometimes, it's the people no one imagines anything of, who do the things that no one can imagine.
Report Abuse
LaeMVP is online. LaeMVP
Joined: 24 Jun 2013
Total Posts: 4416
26 Jul 2017 01:08 PM
tldr, it has something to do with scope
Report Abuse
Tynezz is not online. Tynezz
Joined: 28 Apr 2014
Total Posts: 4945
26 Jul 2017 01:09 PM
local are like house rules, they only apply to your house nobody else
laws apply to everyone

laws='dont murder'

function house1()
local house1rule='no phones'
print(house1rule) --> no phones
print(laws) --> dont murder
end

function house2()
local house2rule='no music'
print(house2rule) --> no music
print(laws) --> dont murder
print(house1rule) --> nil, their house rules dont apply to us
end

house1()
house2()
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