I still see new scripters not being able to variables at the correct time. Here’s an example code of what I mean.
game.Workspace.Model.Value = game.Workspace.Model.Value + game.Workspace.Model.Value / 5
I usually see this kind of code in a scripting support server.
When to use variables
You should be using variables when you have to index/make this thing more than once.
local Stamina = 100 local Vector0 = Vector3.new() -- made a reference to it since creating a new vector every frame isn't the smartest thing to do game:GetService("RunService").Heartbeat:Connect(function() Stamina += HumanoidMoveDir ~= Vector0 and 5 or -2.5 -- assuming we had a reference to the humanoid and stuff end)
I was scrolling through the scripting support part of the devforum and found a post with code that cached(made a variable for) workspace. That is weird since referencing workspace will give you the workspace itself.
Somewhat unnecessary caching
So like mentioned above, there would be times where you don’t need to cache that reference at all for good reasons. According to Luau optimizations, some libraries are imported which means that you don’t need to cache the functions. I said some since it wasn’t specified, but it had an example with the math library so we can safely assume that the math library is imported.
local max = math.max -- it wouldn't change anything now
Hopefully you learned when to use variables from this post.