I need help please

If i make a non-local variable, and i set the value of that variable inside a if statement, am i able to get the value of that variable outside of the if statement?

2 Likes
local variable
if true then
   variable = "hi"
end

print(variable) -- hi
-- dont do it like this tho
if true then
   variable = "hi"
end

print(variable) -- hi
if true then
   local variable = "hi"
end

print(variable) -- nil
2 Likes

but you can make

local var
if var then
    var = "123"
end
print(var)

What?
var is equal to nil so it’s never set to “123”
What are you trying to prove here?

2 Likes

no i dont get right if var is exist its then

Yes. Do not use NON-local vars (variables)

str_1 = "1"
str_2 = nil -- empty won't work (non-local)

if str_1 then
	str_1 = "1"
	str_2 = "2"
    print(str_1)
end

if str_2 then
    print(str_2)
end

print(str_1, str_2)

Noooo, you just did what the first guy said not to do lol. please type local str_2 and local str_1, even though they are “local” you need to define them. Unless you have more code we cannot see.

1 Like

As far as I’m aware, no. If you just put the name of the variable and nothing else, you’ll get an error.

If you use the keyword local, then you can do this:

But, since you don’t want to use the keyword, you’d have to do something like this:
image

Edit: Meant to add on that this will only work if your if-statement executes.

image

If you don’t assign a value to a local variable it’s going to be nil.

image

And because of this, if you tried to do something like this, the if-statement’s condition is not truthy, so the code under it doesn’t execute.