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