spawn does not return the value returns by the passed body function. I recommend using the coroutine library if you want further control over the âthreadsâ you create:
local co = coroutine.create(function()
return "test value"
end)
local isSuccessful, val = coroutine.resume(co)
print(val) --> test value
Or, to be more concise:
local val = coroutine.wrap(function()
return "test value"
end)()
print(val) --> test value
The first example should result in nil, as spawn has no return value. The second example has no error, the result should be function: 0xABCDEF or something like that. That is the functionâs hexadecimal memory address.
Your 2nd example shouldnât error.
By âanonymous functionâ in lua people usually refer to functions created without name, i.e. directly inside a functionâs call arguments, like so:
event:Connect(function()
print("im an anonymous function!")
end)
However doing this:
local function a()
print("hi, my name is a")
end
local b = function()
print("hello, I'm b")
end
is considered creating a non-anonymous function (although some people might argue about the 2nd one).
With that being said, if you want to create an anonymous function just to get a single value, you can wrap it in () parantheses and then call it at the end:
local val = (function() return 1+1 end)()
print(val) --> 2
As you can see, anonymous functions can be created without putting them inside a function call
Btw you should avoid using global variables when possible (referring to your example code).