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 » Scripting Helpers
Home Search
 

Re: When to use local variable and not?

Previous Thread :: Next Thread 
colourcube is not online. colourcube
Joined: 17 Oct 2009
Total Posts: 508
01 Jan 2014 03:07 PM
What is the difference between a variable being local or not?
Report Abuse
person725 is not online. person725
Joined: 11 Oct 2009
Total Posts: 535
01 Jan 2014 03:08 PM
You can use a local variable at any time, but you should always use them in local scripts. But I use them in both.
Report Abuse
Wakarimasu is not online. Wakarimasu
Joined: 12 Nov 2013
Total Posts: 1448
01 Jan 2014 03:11 PM
A local variable can only be used in the section of the script that you call them in, such as a function like this

script.Parent.Equipped:connect(function()
local char = script.Parent.Parent
end)

That would make a variable char for the character who equipped the tool, and that variable can only be used inside the functions where the tool is equipped.

If you dont have 'local', you can use char anywhere in the script.
Report Abuse
VilleSlay is not online. VilleSlay
Joined: 14 May 2011
Total Posts: 1405
01 Jan 2014 03:11 PM
function test1()
local var = "hi"
print(var)
end

function test2()
local var = "goodbye"
print(var)
end

test1()
test2()

--Output:
hi
goodbye
--
Using local means it is only within that "arrow" if that makes it easier for you to understand.

"function" is one of these "arrows".
~~> Ville <~~
Report Abuse
kubuto2 is not online. kubuto2
Joined: 16 Jul 2008
Total Posts: 1275
01 Jan 2014 03:11 PM
Only use local variables inside blocks of code.

For Example

for i = 1, 10 do
local c = c + i--Because it is local and only used here, and never outside of the ---block
print(c)
end

c = c + 3 -- this would only equal 3 because it is not inside a block

_G.c = "This is a global variable scripts can now use this."
c = "This is a default variable, only this script can use it, but can be called through out the script"
local c = "This can only be used inside the blocks of code that you called it in."
Report Abuse
Wakarimasu is not online. Wakarimasu
Joined: 12 Nov 2013
Total Posts: 1448
01 Jan 2014 03:12 PM
I like literally just explained this
Report Abuse
Reinitialized is not online. Reinitialized
Joined: 11 Feb 2008
Total Posts: 688
01 Jan 2014 03:15 PM
@person725:
Actually, that is not a correct definition of what local variables are.

I may not have all the information correct, but what I do know is accurate.

Local variables, defined as:
local a = 1

stores the variable into something that isn't the environment. [What that is, I don't remember exactly] Because of this location, local variables tend to be faster, and don't touch the environment.

When you create a variable like this:
a = 1

You are actually storing the variable in the environment, which is like creating a variable in C++ or C#, or whatever your preferred language is.

By storing in the environment, it tends to take longer to access.

Also, when you create a local variable inside a function, you can't access it
outside of that function.

Example:

function hax()
local derp='iHax'
print(derp)
end

print(derp) :: Returns nil

So, I hope this helps. If you need anything, else me know. :#
Report Abuse
Reinitialized is not online. Reinitialized
Joined: 11 Feb 2008
Total Posts: 688
01 Jan 2014 03:16 PM
Oh my... The time it took me to type that compared to the responses... I have been out typed. e.e
Report Abuse
Wakarimasu is not online. Wakarimasu
Joined: 12 Nov 2013
Total Posts: 1448
01 Jan 2014 03:16 PM
Over-achievers.
Report Abuse
person725 is not online. person725
Joined: 11 Oct 2009
Total Posts: 535
01 Jan 2014 03:17 PM
Wak, get out. No one here appreciates your attitude, so what you already explained, people are going into further explanation. Is that a problem?
Report Abuse
Wakarimasu is not online. Wakarimasu
Joined: 12 Nov 2013
Total Posts: 1448
01 Jan 2014 03:18 PM
No one here appreciates your false information. Being used anywhere is the exact opposite of a local variable, now sit down brat.
Report Abuse
person725 is not online. person725
Joined: 11 Oct 2009
Total Posts: 535
01 Jan 2014 03:24 PM
You are very ignorant, your info was also false. You do not need to have a local variable and keep it in that chunk.

A local variable is a variable that can only be accessed by other things in the same scope. A variable is made local by adding the keyword local before the variable. It is true that local variables are obtained faster than normal variables. This is because they are integrated into the current environment they were created in.

Directly off the wiki. Get out newbag
Report Abuse
Wakarimasu is not online. Wakarimasu
Joined: 12 Nov 2013
Total Posts: 1448
01 Jan 2014 03:26 PM
Newbag? Are you from 4chan? Way to be original.

Also, I've played since 2007. And I still have a 2008 alt [Preston99]

Considering you joined in 2009, I'd consider you the new one.

Also, you're right, you dont NEED to have a local variable to keep in that chunk, but I was explaining how you CAN as an example.
Report Abuse
DrRandomous is not online. DrRandomous
Joined: 25 Mar 2011
Total Posts: 1702
01 Jan 2014 03:30 PM
Wak, shut up. How is he an over achiever for adding more information?
Report Abuse
Wakarimasu is not online. Wakarimasu
Joined: 12 Nov 2013
Total Posts: 1448
01 Jan 2014 03:32 PM
Some people can't tell the difference between sarcasm and insults.

And you guys called -me- the one with the attitude.
Report Abuse
person725 is not online. person725
Joined: 11 Oct 2009
Total Posts: 535
01 Jan 2014 03:33 PM
Wak doesnt know what he's talking about. He is new to lua and wanted to look smart.
Report Abuse
Wakarimasu is not online. Wakarimasu
Joined: 12 Nov 2013
Total Posts: 1448
01 Jan 2014 03:35 PM
New to lua? When someone asked how to do something to all players, you said "game.Players.Player1"

And then I went on to explain to use a for loop, so tell me how it is I'm the one new to lua.
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
01 Jan 2014 03:52 PM
Use a local variable as much as possible, period.

Though if you ever need to know if you need to use it: think to your self "am I editing this variable? would I need to create a variable that can be accessed outside this block?"
Report Abuse
person725 is not online. person725
Joined: 11 Oct 2009
Total Posts: 535
01 Jan 2014 03:54 PM
How do you already have 15k post? Last week you had 13k
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
01 Jan 2014 03:58 PM
Some admin is adding them for me =D Just kidding, I don't know
Report Abuse
person725 is not online. person725
Joined: 11 Oct 2009
Total Posts: 535
01 Jan 2014 04:00 PM
Lol, you know who I am right?
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
01 Jan 2014 04:04 PM
No, how would I if you are using a different account?
Report Abuse
person725 is not online. person725
Joined: 11 Oct 2009
Total Posts: 535
01 Jan 2014 04:06 PM
Possibly the last 3 numbers of my name??
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
01 Jan 2014 04:07 PM
Nope
Report Abuse
ToboboT is not online. ToboboT
Joined: 25 Jun 2011
Total Posts: 2385
01 Jan 2014 04:07 PM
@Wak

That Preston99, you said was you... he's not even in your friends list, and don't try to say that you don't want people to know that it is you when you came so nicely outright about here. Silly child...
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • 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