Hello, I have a player script containing: require “assets.scripts.gamePhysics”, however, when I try to use gamePhysics.pr(), the error “attempt to index global ‘gamePhysics’ (a nil value)” pops up, i have no idea why gamePhysics is nil.
inside gamePhysics.lua there is only a function pr that does print(1)
Thank you for your reply, I tried saving it to a local variable and use gamePhysics.collide(), another error pops up: “attempt to index upvalue ‘gamePhysics’ (a boolean value)” how is gamePhysics a boolean???
I initially used
require "assets.scripts.gamePhysics"
chnged it to
local gamePhysics = require "assets.scripts.gamePhysics"
In short require returns the value you return at the module’s global scope. If there is nothing, it seems it returns a bool.
I didn’t find anything in the official Lua documentation (anyone?) though.
@sodaJar There’s nothing special about code inside of a module. Nothing secret is going on behind the scenes. When you ‘require()’ a file, it runs the code in that file and returns what the file returns (or ‘true’ if it returns nothing). If you return a table, you get a table. If you return a function, you get a function. If you return ‘5’, then that’s what you get, or ‘vmath.vector3(-23, 45.03, -1)’—what you return is exactly what you’ll get.
What this does:
function pr()
print(1)
end
Is define a global function named ‘pr’. It doesn’t put it in a table for you, it just defines it in the global scope (as it would in any .script or .gui_script file as well).
It’s just a convention that lua files usually return a table of functions and values. But that’s something that the author does on purpose, it doesn’t get done for you automatically.