Basically the problem is, how values are passed and how they react after being passed. For example
Code: Select all
local a = 3
local b = a + 1
a = 5
print(b) -- prints 4
So when you set the players position to mousePos, the mousePos is nil at that moment, because when you require lua-file it does everything that is on the file-scope of stuff, meaning the update hasn't been called even once, and when you try to add value to nil, it doesn't work. Also like bdjnk pointed out, the players position will not be updated automatically, because the value is copied from mousePosX and mousePosY when you use the =.
It is good to know that values aren't like mathematical equal, meaning that when a = b, then a will always be the same value as b (there is something called pass-by-reference in other languages like c++/c# for this kind of thing, but lua doesn't have references up to my knowledge). For example when you assign string to local a, then you assign local b to what local a is assigned to, changing a will not change b. On the otherhand if you assign local a to be a table, and then assign local b to value of a, changing stuff inside table will be seen in both a and b. If you think tables are more like pointers from c/c++ it is a lot easier to understand why this is happening, although lua might not be exactly like this. I think this is one of those reasons why every programmer should know a bit of C/C++ or Assembly to at least have understanding how integers/strings etc. use memory, what are underflow/overflow and why would these happen. Why using <, <=, >=, > is prefered when dealing with floating points, instead of using exact values (such as while a ~= 0.1 vs while a < 0.1).