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 5 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:

part = script.Parent
function touch(hum)
hum = hum.Parent:FindFirstChild("Humanoid")
    if hum~= nil then
    print(10)
end
end

part.Touched:connect(touch)

OR

local part = script.Parent
function touch(hum)
hum = hum.Parent:FindFirstChild("Humanoid")
    if hum~= nil then
    print(10)
end
end

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.

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 — 5y
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 — 5y

3 answers

Log in to vote
4
Answered by
Avigant 2374 Moderation Voter Community Moderator
5 years ago
Edited 5 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:

function name()

end

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

name = function()

end

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

local function name()

end

Is sugar syntax for this code:

local name
name = function()

end

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

local name = function()

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:

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:

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

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

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

For your uses here you will need:

local part = script.Parent
local function touch(hum)
    local hum = hum.Parent:FindFirstChild("Humanoid")
    if hum then
        print(10)
    end
end

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:

local part = script.Parent
local function touch(hum)
    if storedHum then
        print(9)
    end
    hum = hum.Parent:FindFirstChild("Humanoid")
    if hum then
        storedHum = hum
        print(10)
    end
end

part.Touched:connect(touch)

Does the same thing as:

local part = script.Parent
local storedHum
local function touch(hum)
    if storedHum then
        print(9)
    end
    hum = hum.Parent:FindFirstChild("Humanoid")
if hum then
        storedHum = hum
        print(10)
    end
end

part.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 5 years ago
Edited 5 years ago

WRITING OVER MY COMMENT

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

---Global Example

lol = true --Some freaking condition
if lol then
    function GlobalFunction(Str)
        print(Str)
    end
end

GlobalFunction("Lol")

---Local Example

lol = true --Some freaking condition
if lol then
    local function LocalFunction(Str)
        print(Str)
    end
    LocalFunction("Lol")
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

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

Answer this question