SO WHEN DO I USE local or not local functions or var
does local var run's faster or non local!?
Should i use:
1 | part = script.Parent |
2 | function touch(hum) |
3 | hum = hum.Parent:FindFirstChild( "Humanoid" ) |
4 | if hum~ = nil then |
5 | print ( 10 ) |
6 | end |
7 | end |
8 |
9 | part.Touched:connect(touch) |
OR
1 | local part = script.Parent |
2 | function touch(hum) |
3 | hum = hum.Parent:FindFirstChild( "Humanoid" ) |
4 | if hum~ = nil then |
5 | print ( 10 ) |
6 | end |
7 | end |
8 |
9 | part.Touched:connect(touch) |
should i use local scripts when affecting the player?
should i use local functions when i use remote events?
I am really confused so i need a lot help.
Remember that there is no concept of a "local function".
All functions are anonymous in Lua, which means they have no names. Remember that this code:
1 | function name() |
2 |
3 | end |
Is really just syntactic sugar / sugar syntax for this code:
1 | name = function () |
2 |
3 | end |
The syntax for a "local function" is similar. This code:
1 | local function name() |
2 |
3 | end |
Is sugar syntax for this code:
1 | local name |
2 | name = function () |
3 |
4 | end |
You may be wondering why it isn't sugar syntax for this code instead:
1 | local name = function () |
2 |
3 | end |
That would be because in the above example, we wouldn't be able to make recursive function calls.
As for local variables, they are only slightly faster to access. It's almost entirely irrelevant in most situations. Remember, micro-optimization is a waste of time. However, prefixing a variable declaration with local
in all scopes allows you to easily see the difference between defining a variable for the first time (or shadowing it) and redefining it.
Consider:
1 | x = 10 |
We're not sure if we're redefining the x
variable as easily. In this example, we can more easily tell that we're defining it for the first time:
1 | local x = 10 |
For this reason, I recommend prefixing all variable declarations with local
.
Additionally, RBXScriptSignal:connect()
with a lowercase c is deprecated, switch to RBXScriptSignal:Connect()
.
The local
keyword has NOTHING to do with local scripts. Done.
For your uses here you will need:
1 | local part = script.Parent |
2 | local function touch(hum) |
3 | local hum = hum.Parent:FindFirstChild( "Humanoid" ) |
4 | if hum then |
5 | print ( 10 ) |
6 | end |
7 | end |
8 |
9 | part.Touched:connect(touch) |
Usually you should always use local
on variables or functions. (except when dealing with environment manipulation).
You CAN not put local
in front of a variable declaration if you want it to be global however it's not a good practice.
Example:
01 | local part = script.Parent |
02 | local function touch(hum) |
03 | if storedHum then |
04 | print ( 9 ) |
05 | end |
06 | hum = hum.Parent:FindFirstChild( "Humanoid" ) |
07 | if hum then |
08 | storedHum = hum |
09 | print ( 10 ) |
10 | end |
11 | end |
12 |
13 | part.Touched:connect(touch) |
Does the same thing as:
01 | local part = script.Parent |
02 | local storedHum |
03 | local function touch(hum) |
04 | if storedHum then |
05 | print ( 9 ) |
06 | end |
07 | hum = hum.Parent:FindFirstChild( "Humanoid" ) |
08 | if hum then |
09 | storedHum = hum |
10 | print ( 10 ) |
11 | end |
12 | end |
13 |
14 | part.Touched:connect(touch) |
^ This is the better practice.
With functions using the local function fn()
will allow recursion so no exceptions there.
WRITING OVER MY COMMENT
Here's an example of where you should use local and where you should use global
---Global Example
1 | lol = true --Some freaking condition |
2 | if lol then |
3 | function GlobalFunction(Str) |
4 | print (Str) |
5 | end |
6 | end |
7 |
8 | GlobalFunction( "Lol" ) |
---Local Example
1 | lol = true --Some freaking condition |
2 | if lol then |
3 | local function LocalFunction(Str) |
4 | print (Str) |
5 | end |
6 | LocalFunction( "Lol" ) |
7 | end |
As you can see, I can call a globalfunction from outside of condition that I wrote in a condition. But in LocalFunction example, I couldn't be able to use it outside of condition, after that last end