basically, a function is something that encloses code inside of it, some "parameters" can be passed, which are essentially variable defined to suit whatever occasion nessasery
take this function:
function Print2Times(text)
print(text..text)
end
when we say "function", we are telling the lua interpereter that we are creating a function, "Print2Times" being the name of the function, and "text" being a variable, inside of the function is some code that reads:
print(text..text)
notice how we reference the "text" variable from earlier? text..text returns whatever the value of the text is joined with itself, so if text = "Hello", then text..text = "HelloHello", now lets call the function, replacing "text" with any string we want (enclosed within quotation marks of course)
Print2Times("Hello World!")
the function we defined will run, with the variable "text" being equal to the string "Hello World!",
whats cool is that you can use a "return" statement to set a variable to whatever the function returns
function GiveMeANumber()
return math.random(1,10)
end
local a = GiveMeANumber()
print(a)
>7
as for the question, any variables defined directly within the function cannot be accessed outside because it is outside of the scope, it can only access variables passed and variables outside of it
local a = "j"
function lol()
print(a)
end
>a
function lol2()
local f = "foo" .. 44
print(f)
end
f = a
>Error
but if its in different enviroments then you cant access each others variables unless it is part of _G, the global variable table, or if you comminucate via Bindable/Remote Events/Functions