Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Wait so what does the local function do?

Ok so usually at the beginning of codes on the first line, it would usually start with something like this

local ……=…………………(……)

What does that mean? Also I know this is a very beginner thing but I just never got around to learning what this means (I started like a month ago)

5 comments

0

You mean local variables.They are basically the same as globals, the only real difference is that local variables may be a bit faster than variables without local

0

Local functions are not the same as local variables. Local functions are functions that run in its own scope

1

Local variables also run in their own scope

0

yea, i meant to say that too.

1 answer

✓ Accepted answer

Hey Typical,

Local allows things to be defined in a different scope. In programming, a scope defines accessibility. Everything in your script is inside of a scope, and the variables which you address all depend on the scope in which they are.

Local limits the scope of a variable and makes it only be defined in that local scope. While not declaring a variable as local will allow it to be accessible through the entire script. It is standard practice to declare pretty much all variables locally, as it can help preserve memory, and allows you to use this idea of scope as a feature.

####Example: Local Variables

local a = 3

function sum(b, c) --Will return the sum of a, b, and c. a is in the bigger scope
	return a + b + c
end

function subtract(b) --Will return the difference between a and b
	return b-a
end

print(a + b + c) -- Will give you an error, something along the lines of performing arithmetic on nil

###Now here, b and c are only within the scope of the function sum. Even though they are not defined as local, all parameters are within the scope of their respective functions.

###However, a is defined within the scope of the entire function. That is why it can be used both within sum and subtract.

###Essentially, local functions follow a similar rule:

####Example: Local Functions

local function sum(a, b, c) -- Returns the sum of a, b, and c
	local function subtract(a, b) --Returns the difference between b and a.
		return b-a
	end

	subtract(2,1) -- Will run with no errors and returns -1

	return a + b + c
end

sum(1,2,3) -- Returns 6.

subtract(2, 1) -- Will error because subtract isn't a function defined in the bigger scope, but rather one defined within the scope of the function called sum.

###Above is just an example of using local functions and both of the local functions are defined within their own scope. Keep in mind, the function sum can be accessed anywhere within the script, including within itself. That is more of the idea of recursion.

##Here you can learn more about scope.

Best regards,

Idealist Developer

5 comments

0

Wait how do you know what to ‘define’ when doing that? Also what does scope mean? Sorry I’m kinda a beginner

0

Oh wait nvm I just reread the part about scope

0

Also you have to do this everytime you make a script right?

0

Also one last question when do you add the local thingy because I would usually see a line of code for the local thingy right in the middle of some scripts. So when do you know to use local

0

It's not required to use it. Some people use it to for  good practice or for code to run a little  faster and for other reasons. The only time you really need to use it is if you want the code to only run within its own scope.

This discussion is preserved as it was. New answers, comments, and votes are closed.