Do I need to use locals?

is it ok to not use lots of locals / none? alot of times when scripting i dont use locals just because personally its easier to read (i know strange)
for example

script.Parent.Parent.Humanoid:LoadAnimation(game:GetService("ServerStorage").Storage.coreAnimations.ToolApply):Play()
1 Like

I will just assume that you are trying to ask is “Do I need to use variables?”

Well, if you are not planning to store it and use it later, and you can read it, then sure

2 Likes

I mean technically you don’t need variables. However, your code will become unorganized and you will lack certain optimizations.

In that short scenario you wrote, you can put ServerStorage in a variable.

This will:

  1. Make the code more organized.
  2. Allow you to easily reuse the service.
  3. Reduce the calls on the method :GetService() for optimizations (even if it is very insignificant).
1 Like

I’ll keep it sweet and short
Variables are ways for you to reference paths or objects with shorter words. Variables are very good in improving overall performance of your script and keeps everything readable.However, you need to know when to use it and when to not. Ex;
Use variables when you are going to reference an object that will not change throughout the code

local humr = plr.Character.HumanoidRootPart — Here I can use a variable as the hmr will not change.

You should not use variables when something is constantly changing throughout your script. Variables store the object or the value of something of that instant only

local value = workspace:GetAttribute(“Parts”) — This not a good usage of variables as parts will constantly be added to workspace with scripts, it’s better to just use the whole thing as you get the updated value when you use it instead of the variable 
1 Like

Besides the points others have brought up about organization, using variables instead of indexing every time is also a lot better for performance.

1 Like

This is wrong, once Character is gone and respawns humr will become nil unless you redefine it.

2 Likes