Why Is It That Sometimes I Encounter A Variable That Has No Value

Hello, :wave:

If you don’t understand what I mean, I am talking about this:

local connection
connection = script.Parent.Touched:Connect(function(part)
    if part.Parent:FindFirstChild("Humanoid")
        part.Parent.Humanoid.Health = 0
        connection:Disconnect()
    end
end)

At the very top of the script, the “connection” variable is not equal to anything. What does this mean?? Thanks, :wave:

Try this:

local connection

local function PartTouched(part)
    if part.Parent:FindFirstChild("Humanoid")
        part.Parent.Humanoid.Health = 0
        connection:Disconnect()
    end
end)

connection = script.Parent.Touched:Connect(PartTouched)

You’re probably defining your Event without calling it “local”

It means that the variable doesn’t contain anything.
You are putting a touch event into the variable on the next line. Later on in your code when you say connection:Disconnect(), you are disconnecting the event.

We have to write it like you’re writing it above because otherwise, if we write it like the code below, the script wont know what connect is when we say connection:Disconnect() and it’ll error

local connection = script.Parent.Touched:Connect(function(part)
    if part.Parent:FindFirstChild("Humanoid") then
        part.Parent.Humanoid.Health = 0
        connection:Disconnect()
    end
end)
2 Likes

its simply a shortened version of local connection = nil

1 Like

So in the example. I make a variable and assign it to nil. Then later in the code, I assign the variable to a different value?

1 Like

yes, that is what you’re doing

That’s called declaring a variable without initializing it. When you declare a variable, you’re basically letting the script “know” that that variable exists, whether or not you assign an initial value to it.

When you initialize a variable, that simply means assigning a value to it for the first time. You can either declare and initialize a variable on the same line like you’re used to or you could split the declaration and initialization across multiple lines.

The applications of that can be kind of specific, but you’ll know when to use that method when you need it. Usually it just has to do to get around scoping issues, like in that example you showed. If the declaration and initialization was done on the same line, then the script would not know what “connection” is inside that event unless you declare it beforehand.

3 Likes

Ohh I see, but why would I bother defining the connection variable at the top of the script when I can define the variable later in the code??

look at the code i wrote in my first reply. we’re trying to use connection inside the code, but the script doesn’t know what connection is yet when we write it like that so it errors

I also have a question about this, if you put the variable at the top, and then later, for example in a function, you define it, can you use that variable later on in the script (outside the function), with it having the same value as defined in the function?

I don’t think so bc I tried this:

local num


function printSomething()
	
	num = 900
	
end


print(num)

Ouput: image

num is still nil because youre never calling printSomething meaning you’re never changing num
if you called printSomething, then num will be 900

4 Likes

I know this is already explained but.

local a
local b = nil
if a==b then 
return true --This is what you will get.
else 
return false

An even easier version:
local a = local a = nil

2 Likes

I think I messed up the wording in my post, sorry. I meant to say declaration instead of definition; they mean different things (feel free to correct me if I got anything else wrong).

It depends on how that variable appears in the function. If you use the variable like in @Mysterious_Myth11’s example then that variable will change across all scopes, assuming you actually call the function like what @royaltoe said.

If you pass the variable into the function as an argument, then if it’s a table or roblox instance (which is actually just a fancy table), then the variable gets passed by reference. In basic terms, that means that the function and the variable reference the same “thing”, so whatever happens to that “thing” in the function also happens to the variable.

If you pass a variable of any other type, however, like a number or a boolean, it gets passed by value. That means that the function gets a copy of that variable’s value, but it doesn’t actually get the variable itself, to put it simply. This way, when you pass that variable into a function call, the value of that original variable won’t change, no matter what happens in the body of the function.

1 Like