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

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

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.

2 comments

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.

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.

3 answers

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

4 comments

0

Really good post! Please don't be nitpicky about :connect and :Connect.

1

It's better to inform people about :Connect since nobody knows what roblox might do about it.

1

It's not a matter of "nitpickyness," you should not be using deprecated methods, period. Don't use connect.

0

I do use :Connect it... You know what I sound so hypocritical right now.

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.

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

1 comment

0

You don't have to define lol just do if true then

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