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

Rblx.lua Engine Vs Lua Stand-Alone Engine - Functions don't work the same way?

Today I finally downloaded the lua engine. ^-^ For the first code I tried, I used the code below:

function returnNumPlus1(num)
	return num + 1
end

print(returnNumPlus1(0))

print(returnNumPlus1(9000))

local func1 = returnNumPlus1
print(func1(11))

I tested both codes; the Rblx.lua engine fired, while the stand-alone lua engine returned an error. The error is as follows:

stdin:1: attempt to call global 'func1' (a nil value)
stack traceback
		stdin:1: in main chunk
		[C]: ?

Why is it that Roblox's worked, while the lua stand-alone engine errored? I can't see why one works while the other doesn't? Why is that?

Thanks for reading. ;)

1 comment

0

The only real difference between Roblox Lua and stand alone Lua is that stand alone Lua can only be used for text operations/programs, and nothing visual can be created with it.

1 answer

✓ Accepted answer

Assuming you are running this code in lua REPL, you need to define func1 as global rather than local as the local context is specific to each line execution in the REPL and is not available for the next line.

Try:

function returnNumPlus1(num)
    return num + 1
end

print(returnNumPlus1(0))

print(returnNumPlus1(9000))

func1 = returnNumPlus1
print(func1(11))

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