Bug? script calls a function from a gui script (same name) instead of his function (SOLVED)

I have a gui script, from here I send a message to a script, in that script I call a function, and the Script calls the function of the previous gui script instead of his function!!

both of the scripts has the same name function, but they are not doing the same.

obviosly i got the error “You can only access gui.* functions and values from a gui script instance”

for testing, I change the name of the function in the script, and works well.

Is this normal? thanks

Is the function prefixed with the local keyword? If not it means that it is a global function and bad things such as what you describe will happen.

1 Like

Yeah, script A executes gui.animate with a callback function, this function has local keybord. This function do msg.post (script B)

Script B goes his flow, and calls a function, and calls the function of script A instead of the function of the script B (both with the same name, and without the local keyword)

Ok, so the solution is to not have global functions. Or is there something with your current solution that prevents you from making the functions local.

No, but I dont understand why is this happen if they are diferent scripts. They shoudnt see each other

But if it is a global function then it’s available from all scripts.

Maybe it’s better if you share the relevant code here.

I thought it was avaliable for the entire script, not for all the scripts, need to see more theory of lua.

But, why use properties or send messages to other scripts, if I can call every function from all the scripts?

If you put a function outside the lifecycle function and make it local then it’s available to all functions within the script.

Because you run into the kind of problems you are running in to. Also two global functions with the same name can’t exist. It’s only one of them that will be available. It is generally considered a very bad practice to make everything global. At least in a large application. Unwanted side effects, hard to debug code, hard to test code, hard to change code.

Ok, I will change all my functions to local. Thanks for the explain and the help :slight_smile:

1 Like