Why do we make variables with no information?

Heyo Devforum! Recently I have been wondering why we make variables with no information. What does this do? I have searched the Devforum and didn’t find anything. Maybe this is called something, but I am not sure. If there are any topics out there, please let me know so that I can read them, else, please tell me why we do this. I am very confused.

1 Like

What do you mean by “variables with no information”, preferably a code example.

Do you mean like this?

local savedData

Yes just like that. Why make variables like that?

oh so we can assign it later. but

local savedData

Should be the same as

local savedData = nil
1 Like

So you can assign it many times throughout the script?

here is an example:

-- This code would be somewhere in a DataStore script
local savedData
pcall(function()
savedData = MDS:GetAsync(ID)
end)

Ok so we make these variables so that we can assign information later that way you can call the variable anywhere in the script?

1 Like

yes but note

local MDS = game:GetService("DataStoreService"):GetDataStore("MyDataStore")

game.Players.PlayerAdded:Connect(function(plr)
local savedData
--Some data store script here
end)

game.Players.PlayerRemoving:Connect(function(plr)
savedData = --What you want it to be
end)

that script would not work because in the second function you cant call a variables that are in other functions

1 Like

Hi @HonestJar!
You are right, you can use/rehuse that variable within the “place” where it was created.

If its created within the script, then u can use it anywhere in the script, if its created inside a function, then that variable only belongs inside that function and you cant use it from outside the function

1 Like

For what @Dev_Peashie said + for organization since until a value is assigned it is equal to nil meaning it doesnt take up any memory.

2 Likes

Its usually an issue with the scope of the variable.

1 Like

Yeah, like everyone else mentioned, it essentially allows you to store variables that can later referenced in the script without a limit due to the scope of the variable. They’re are basically used as placeholders, to transfer information across so they can be accessed when you need them. For example, say you have a function that chooses a random player; you might want that random player to become transparent. Because the random player is chosen within the function, it can’t be used directly outside of the function. Hence, you may want to create a variable to reference that random player and set their transparency.

1 Like

The reason of declaring variables with no value is for re-using the variable in the scope after processing some functions on it. Usually useful and avoids writing inside another scope. This variable can later reference something if needed.

2 Likes

for example i want to do a repeat loop but i want to get the variables that repeat loop created, look at this code

repeat
 local hi = 1
until hi

print(hi) --nil

as you can see repeat has a scope inside itself so i cant use hi outside it, but with this code:

local hi

repeat
 hi = 1
until hi

print(hi) --prints 1

as you can see from the new code i dont have to explain it its basically self explanitory

2 Likes

Unassigned variables are usually defined so we can use them in any scope.

Basically what that means is this:
Consider a variable inside a function

function SomeFunction()
local Variable = true
end

This ‘Variable’ Can only be used within the scope of the ‘SomeFunction’ Function, So if we wanted to call it outside of the function, it would print nil as it doesn’t exist

function SomeFunction()
local Variable = true
end

print(Variable)
>>--Variable is nil, variable is undefined (within scope)

If we ever need to be able to access this ‘Variable’ We just use an unassigned “global” variable which allows us to use it in any scope by defining it at the top of the script:

local Variable

function SomeFunction()
Variable = true
end

print(Variable)
>>-- Prints true, Variable is defined
1 Like

While in other programming langauges it may error, in Lua(u) it would be treated as nil.
image

1 Like

Thanks for the correction, I’ve edited my post

1 Like