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
If all services that are being defined are using :GetService(), using workspace hastily becomes an anti-stylistic pattern in your codebase. Parallelism for assigning services would be lacking.
You could argue that you can shred off a few microseconds or femtoseconds, but to a normal human, the performance gain is practically unnoticeable.