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 
HyperProduct is not online. HyperProduct
Joined: 11 Nov 2012
Total Posts: 3362
21 Aug 2016 05:44 PM
I understand that it's best to use local variables whenever possible, but I'm a little confused on if you should use them if they're at the top of the script. Since the variable wouldn't be in a function or anything, wouldn't it be able to be accessed from anywhere? Does making it local make a difference?

I'm mostly asking this because when you don't make a variable local when it's in a function, it gets a blue underline and tells you to consider making it local. If making it local when it's not in a function makes a difference, why doesn't it get underlined?


Report Abuse
thedailyblarg is not online. thedailyblarg
Joined: 26 Feb 2012
Total Posts: 5506
21 Aug 2016 05:46 PM
Local variables are only available after they are declared and their value will stay the same depending on what scope you use it in

local moop = "hi"

function set(text)
local moop = text
print(moop)
end

set("Hello")

print(moop)


--any time you declare a local variable, it will only be usable in the area that it is inside
Report Abuse
2eggnog is not online. 2eggnog
Joined: 08 Nov 2008
Total Posts: 1351
21 Aug 2016 05:47 PM
Local variables are faster to read/write and it's harder for exploiters to steal the values of local variables.


Report Abuse
HyperProduct is not online. HyperProduct
Joined: 11 Nov 2012
Total Posts: 3362
21 Aug 2016 05:53 PM
I was talking about performance:
http://wiki.roblox.com/index.php?title=Performance#Use_Local_and_avoid_globals

If I were to make a simple script like this:

num = 5

function add(x)
return x+1
end

print(add(num))


Would making 'num' local be better, or not make a difference at all?


Report Abuse
thedailyblarg is not online. thedailyblarg
Joined: 26 Feb 2012
Total Posts: 5506
21 Aug 2016 05:54 PM
would have no difference unless you used something that called num beforehand

and it will be easier to change via exploits
Report Abuse
HyperProduct is not online. HyperProduct
Joined: 11 Nov 2012
Total Posts: 3362
21 Aug 2016 05:55 PM
Great, thanks!


Report Abuse
2eggnog is not online. 2eggnog
Joined: 08 Nov 2008
Total Posts: 1351
21 Aug 2016 05:59 PM
It would make a difference: It'd be slightly faster.


Report Abuse
platin_m is not online. platin_m
Joined: 25 Jul 2016
Total Posts: 169
21 Aug 2016 06:03 PM
@2eggnog

the difference isn't important though? as long as you place the variable before your chunk of code (that uses that particular variable), nothing is going to change.
Report Abuse
2eggnog is not online. 2eggnog
Joined: 08 Nov 2008
Total Posts: 1351
21 Aug 2016 06:06 PM
The difference CAN be important. In situations where performance matters, such as math-intensive calculations, there can end up being a noticeable difference. Just use local variables.


Report Abuse
platin_m is not online. platin_m
Joined: 25 Jul 2016
Total Posts: 169
21 Aug 2016 06:08 PM
@2eggnog

i mean, if someone that performance-hungry really needs that small inch of speed, sure.
Report Abuse
2eggnog is not online. 2eggnog
Joined: 08 Nov 2008
Total Posts: 1351
21 Aug 2016 06:16 PM
It's more than a small inch of speed. There are plenty of situations your code could easily run twice as fast using local over global variables.

Using local variables can only help you.


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