Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
3

When should i use Local or Not local functions or variables?

Asked by 6 years ago

SO WHEN DO I USE local or not local functions or var

does local var run's faster or non local!?

Should i use:

1part = script.Parent
2function touch(hum)
3hum = hum.Parent:FindFirstChild("Humanoid")
4    if hum~= nil then
5    print(10)
6end
7end
8 
9part.Touched:connect(touch)

OR

1local part = script.Parent
2function touch(hum)
3hum = hum.Parent:FindFirstChild("Humanoid")
4    if hum~= nil then
5    print(10)
6end
7end
8 
9part.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.

0
Local functions are indeed would be slightly faster. Basically, you need to use Global when you want to reach that function in different places, and use Local when you only need to use that in that place. I'll send you an example. superalp1111 662 — 6y
2
Locals can be reached globally if they're declared in the global scope. That's why it's sort of redundant to use globals unless an awfully specific situation calls for it. ScriptGuider 5640 — 6y

3 answers

Log in to vote
4
Answered by
Avigant 2374 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

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:

1function name()
2 
3end

Is really just syntactic sugar / sugar syntax for this code:

1name = function()
2 
3end

The syntax for a "local function" is similar. This code:

1local function name()
2 
3end

Is sugar syntax for this code:

1local name
2name = function()
3 
4end

You may be wondering why it isn't sugar syntax for this code instead:

1local name = function()
2 
3end

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:

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:

1local 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().

0
Really good post! Please don't be nitpicky about :connect and :Connect. Griffi0n 315 — 6y
1
It's better to inform people about :Connect since nobody knows what roblox might do about it. radusavin366 617 — 6y
1
It's not a matter of "nitpickyness," you should not be using deprecated methods, period. Don't use connect. ScriptGuider 5640 — 6y
0
I do use :Connect it... You know what I sound so hypocritical right now. Griffi0n 315 — 6y
Ad
Log in to vote
0
Answered by
Griffi0n 315 Moderation Voter
6 years ago
Edited 6 years ago

The local keyword has NOTHING to do with local scripts. Done.

For your uses here you will need:

1local part = script.Parent
2local function touch(hum)
3    local hum = hum.Parent:FindFirstChild("Humanoid")
4    if hum then
5        print(10)
6    end
7end
8 
9part.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:

01local part = script.Parent
02local 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
11end
12 
13part.Touched:connect(touch)

Does the same thing as:

01local part = script.Parent
02local storedHum
03local function touch(hum)
04    if storedHum then
05        print(9)
06    end
07    hum = hum.Parent:FindFirstChild("Humanoid")
08if hum then
09        storedHum = hum
10        print(10)
11    end
12end
13 
14part.Touched:connect(touch)

^ This is the better practice.

With functions using the local function fn() will allow recursion so no exceptions there.

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

WRITING OVER MY COMMENT

Here's an example of where you should use local and where you should use global

---Global Example

1lol = true --Some freaking condition
2if lol then
3    function GlobalFunction(Str)
4        print(Str)
5    end
6end
7 
8GlobalFunction("Lol")

---Local Example

1lol = true --Some freaking condition
2if lol then
3    local function LocalFunction(Str)
4        print(Str)
5    end
6    LocalFunction("Lol")
7end

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

0
You don't have to define lol just do if true then Griffi0n 315 — 6y

Answer this question