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 vs Global

Previous Thread :: Next Thread 
Mmoniot is not online. Mmoniot
Joined: 12 Mar 2012
Total Posts: 85
18 Jan 2015 05:25 PM
If you were to write something like;

Bin = script.Parent

at the TOP of the script(outside of a function), then what is the precise difference from writing;

local Bin = script.Parent

They seem to work the same, but I do not know if one is better
Report Abuse
ForeverDev is not online. ForeverDev
Joined: 04 Oct 2008
Total Posts: 13300
18 Jan 2015 05:25 PM
i mean

they're pretty much the same in that case

but i always use local, because it looks neater
Report Abuse
chimmihc is not online. chimmihc
Joined: 01 Sep 2014
Total Posts: 17143
18 Jan 2015 05:26 PM
dont always use local, there are some times when you dont want to
Report Abuse
ForeverDev is not online. ForeverDev
Joined: 04 Oct 2008
Total Posts: 13300
18 Jan 2015 05:26 PM
@chim

no

you can ALWAYS use local
Report Abuse
verbalAKspray is not online. verbalAKspray
Joined: 09 Jul 2014
Total Posts: 23685
18 Jan 2015 05:27 PM
local is like an inside joke

anyone outside of the function that u make it in

wont get the joke and will break ur script in retaliation
Report Abuse
amanda is not online. amanda
Joined: 21 Nov 2006
Total Posts: 5925
18 Jan 2015 05:29 PM
If you ever want to use the word Bin again.

For example, if you wanted a script you could put into any part

to be

part = script.Parent

it wouldn't work for more than one because part only gets defined once

but if you did

local part = script.Parent

you could do that in as many scripts as you want because it is local to the script
Report Abuse
chimmihc is not online. chimmihc
Joined: 01 Sep 2014
Total Posts: 17143
18 Jan 2015 05:30 PM
nope dumb@$$

---
local chim = "CHIMMIHC"

function nope()
if chim == "CHIMMIHC" then
local chim = "hi"
end
print(chim)
end

nope()

--> CHIMMIHC


----

local chim = "CHIMMIHC"

function nope()
if chim == "CHIMMIHC" then
chim = "hi"
end
print(chim)
end

nope()

--> hi


Report Abuse
chimmihc is not online. chimmihc
Joined: 01 Sep 2014
Total Posts: 17143
18 Jan 2015 05:31 PM
that was for ForeverDev
Report Abuse
ForeverDev is not online. ForeverDev
Joined: 04 Oct 2008
Total Posts: 13300
18 Jan 2015 05:33 PM
@chim

there is always a way around it, just don't name it the same thing, lol

local test = "var"

function nope()
local thing;
if test == "var" then
thing = "dumbass"
end
print(thing or "test isn't equal to var!")
end

nope()


and i didn't mention that your script changes the original chim on line one ;0
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
18 Jan 2015 05:33 PM
chim, you do realize there is a difference between declaring a global variable and just re-setting a local/global variable.

local x = 5;
x = 10;

There, x = 10 will set the local variable x to 10, not define a global one.
Your example doesn't help the argument at all.
Report Abuse
chimmihc is not online. chimmihc
Joined: 01 Sep 2014
Total Posts: 17143
18 Jan 2015 05:34 PM
yes
Report Abuse
Goulstem is not online. Goulstem
Joined: 04 Jul 2012
Total Posts: 7177
18 Jan 2015 05:44 PM
Local variables are 'local' to the scope that you set them in.

local derp = 0
--This would be local to the outer scope so it doesn't really matter in this scenario.

local derp = 0

function hi()
local other = derp + 1
print(other) --> 1
end

print(other) --> nil

--'other' is local to the scope of the function, if you attempt to use the variable outside the scope it will return nil.
Report Abuse
Mmoniot is not online. Mmoniot
Joined: 12 Mar 2012
Total Posts: 85
18 Jan 2015 06:41 PM
I understand local variables, I just want to know if it makes any difference setting a variable to local outside of a function
Report Abuse
Goulstem is not online. Goulstem
Joined: 04 Jul 2012
Total Posts: 7177
18 Jan 2015 06:59 PM
No, not really.
Report Abuse
joelsdarkside22 is not online. joelsdarkside22
Joined: 18 Mar 2009
Total Posts: 1131
18 Jan 2015 07:01 PM
Local cannot be accessed out of a scope.
Report Abuse
LucasLua is not online. LucasLua
Joined: 18 Jun 2008
Total Posts: 7386
18 Jan 2015 07:02 PM
I can't tell if chim is trolling or missing the point entirely...
Report Abuse
joelsdarkside22 is not online. joelsdarkside22
Joined: 18 Mar 2009
Total Posts: 1131
18 Jan 2015 07:03 PM
@Lucas
Chim is a beginner at scripting.
Report Abuse
Goulstem is not online. Goulstem
Joined: 04 Jul 2012
Total Posts: 7177
18 Jan 2015 07:09 PM
@joel

No he's not. He's like intermediate level, probably better than you.
Report Abuse
joelsdarkside22 is not online. joelsdarkside22
Joined: 18 Mar 2009
Total Posts: 1131
18 Jan 2015 07:14 PM
ah goulstem why do you have me all of a sudden, we used 2 b frends
Report Abuse
Voiliax is not online. Voiliax
Joined: 05 Nov 2009
Total Posts: 15554
18 Jan 2015 07:32 PM
Always use local variables, it's better and overall more efficient. When you set/access a global variable, you're actually setting/accessing a table index, the environment table. Whilst local variables behave differently and does not require as much performance compared to global variables.


If you want a local variable to behave like a "global" variable, set it as an upvalue. Upvalue only means an external local value. A local variable that is set at the script's scope.

e.g.:

local lol = "Hi"
local lol2

do
 lol2 = "Hi2" -- lol2 is still a local variaible at the script's scope (an upvalue)
end

print(lol2)
Report Abuse
BossfightX is not online. BossfightX
Joined: 06 Feb 2010
Total Posts: 2962
18 Jan 2015 08:23 PM
http://wiki.roblox.com/index.php?title=Local_variable


Wow guys, wow!
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