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

Why do scripters redeclare keywords in their scripts? [closed]

Asked by
Z_DC 104
5 years ago

I've looked at many scripts written by others before and I always see:

local unpack = unpack
local print = print
local next = next

and so on...

Why do they do this?

0
Check out eLunate's answer on this very topic: https://scriptinghelpers.org/questions/36768/what-does-local-script-script-actually-do#39220. Also do keep in mind those are variables, not keywords. Keywords are words like for, true, if, function, etc. User#19524 175 — 5y

Locked by User#24403

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

3 answers

Log in to vote
4
Answered by
Zafirua 1348 Badge of Merit Moderation Voter
5 years ago

The term you are looking for is called Coaching.

Global Variables are around 10x slower than local variables when you try to access them. Coaching is completely unnecessary unless you are doing something that could require it. Although, I doubt you would need anything that would require.

It is important to note that we do not assign local to variables for the sake of the performance. We assign it because it is a good programming style as well as it avoids name collisions. Many people think that if I just do local print = print, it will automatically become 10x faster. At this moment, we are looking at milliseconds (? Citation Needed) or even less. I don't think this is usually an issue unless you need that milliseconds. This again, can be easily countered by using a better algorithms and/or a better data structures.

In conclusion, coaching is really unnecessary but a choice if you do need it.

0
for the performance freaks xd User#19524 175 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

Since this question is more opinion-based (I'm pretty sure) - I'm responding with an answer instead of a comment.

Personally, I never do this. Especially redefining it as exactly what it is. I can understand it if you do something like

local p = print

and you use print a lot in your code - but I don't quite get the need to do what you've mentioned.

0
That's not why they do it, neither is this question opinion based User#19524 175 — 5y
0
@incapaz - Ahhhh, gotcha. I said it was opinion-based because I figured it was just a time-saving thing - but that post is super informative. Thank you! SummerEquinox 643 — 5y
Log in to vote
0
Answered by
Mr_Unlucky 1085 Moderation Voter
5 years ago

I do it mostly to save time. I know a lot of scripters who do this.

--[[
     Let's assume you want a part's Cframe changed. But you're in a hurry. You can make a 
     variable equal to Cframe.new to save time.
--]]
local cf = CFrame.new
workspace.Part.CFrame = cf(0,0,0)

I dont really do this anymore, but keep in mind it works.

0
That's not why they do it User#19524 175 — 5y