Should I store values as variable for everything?

Let’s suppose I want to access the local player through a local script. Instead of doing it instantly, I’d do:

local plrs = game:GetService("Players")

local plr = plrs.LocalPlayer

-- Instead of

local plr = game:GetService("Players").LocalPlayer

Is it plausible to store important things as variables instead of just ‘raw’ using it? I’ve seen way more than I can say people using services instead of storing them:

local tween1 = game:GetService("TweenService"):Create(...)
local tween2 = game:GetService("TweenService"):Create(...)

tween1:Play()
tween2:Play()
1 Like

In my opinion, if you’re reusing the value more three times then you should probably store it as a variable (especially if it’s like an array, dictionary, or service of some sort)

2 Likes

You should try to avoid using variables for basically anything as it uses up a lot more memory.

do whatever works for you and for your project; very rarely is there going to any impact at all from not having variables

It’s personal preference, but for organization, you should always initialize all services and modules as variables at the top of the script. If you’re too lazy to do it yourself, you can use SimpleComplete.

1 Like

I understand it uses memory because variable is virtually storing something in memory, but it shouldn’t be that much of a problem though?

???

Even if it uses memory (doubt it’s significant), what if you’re using something multiple times?

3 Likes

The point they are trying to make is that a variable takes up memory, so there’s a balance to be made between organization and efficiency.

Exactly, accessing something multiple times is a gigantic error, as it is way worse than simply storing something incredibly small in memory

To put the actual memory use into perspective:
1 integer val is 32 bits/4 bytes (though this has a 64 bit option too) (as per Numbers)
1 number val (non integer/floating point) is 64 bits/8 bytes (Also as per Numbers)
1 CFrame = ~20 bytes.

Making a variable “local” can reduce the amount of memory it uses.

But all of this is negligible unless you’re making a really, really big game or loading a list or instance that has a LOT of data into memory, so the most accurate answer is it doesn’t really matter.

3 Likes

alright, im gonna do this from now on

if game:service'Players'.LocalPlayer.Character == nil then
    game:service'Players'.LocalPlayer.CharacterAdded:wait()
end

if game:service'Players'.LocalPlayer.Character:FindFirstChild'Humanoid' then
    game:service'Players'.LocalPlayer.Character.Humanoid.Died:connect(function()
        game:service'Players'.LocalPlayer:Kick("imagine dying")
    end)
end

thx for the advice

2 Likes

What if we have something like that:

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(input)
     ...
end)

Generally, I store the keyCode (a.k.a. key) into a variable, so I can handle it way more easier:

local key = input.KeyCode

if key == Enum.KeyCode["whatever"] then ... end

That’s what I’m talking about; i’ve seen a LOT of people doing this, and it is not a joke

Because when you are setting up a variable to be locally implemented, you probably won’t need to use much of the memory to make it visible to the rest of the code? I don’t know

2 Likes

Yeah I imagine it’s something like that. There’s normally a big array storing global variables but local does something different. It gets rid of the variable from memory more often. I’m not sure how to explain it myself, so here.

2 Likes

this is 100% preference

2 Likes

Indeed, generally you would rarely use global variables for something. If we want to access a variable to the rest of the code, commonly we set it at the start of the script, so the scope is ‘global.’

EDIT: Then, we can simply update its value when needed:

local currentPart

for _, part in workspace:GetChildren() do
    if part:IsA("BasePart") then
        part.Touched:Connect(function()
            currentPart = part.Name
        end)
    end
end

-- Instead of setting currentPart as a global variable inside the for loop.

1 Like

ok sorry for the bad reply.

what i meant is that if you are consistently using something, you should definitely use a variable. However, a variable takes up memory as it is being stored. So my point is to avoid using variables, but if you have something repetitive or accessing the same thing numerous times, you should use one.

The amount of memory that variables need to be stored is irrelevant. You should use variables whenever you can.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.