Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How can I assign variables to functions?

Like the print function for example.

local hi = print()

hi("Hello world!")

When I do that I get attempt to call a nil value error.

3 comments

0

Just remove the "()"s.

0

But wouldn't that throw an error because I don't have them?

0

no.

2 answers

As ud2v3cf mentioned in the comments, remove the brackets when creating the variable, only use parentheses when you're calling it.

Example code:

local hi = print
local bye = warn

hi("Hello world!")
bye("Goodbye world!")

sorry for my terrible analogy, but hope this helps.

###Adding on to V_ChampionSSR, remove the brackets when you want to assign the variable to the function itself. The reason for this is because if you did not remove the brackets, you'd be assigning the variable to what the function returns, not the function itself. If it does not return, it returns nil. Take this for example:

local function NumberOne()
    return 1 -- this function now acts as number 1
end

local function ThisDoesNotReturn()
    
end

local noreturn = ThisDoesNotReturn()
print(noreturn) --> nil
noreturn = ThisDoesNotReturn
print(noreturn) --> function: 0xfa8f50 
-- this is the function id. yours will be different 
noreturn() -- call function. 

local hi = NumberOne()
print(hi) --> 1
hi = NumberOne
print(hi) --> function id again

hi() -- call function. 

This discussion is preserved as it was. New answers, comments, and votes are closed.