Tomc0 Posted July 28, 2013 Share Posted July 28, 2013 Hello, if that's going to be a noob question sorry, but I just can't find a great tutorial for my problem. So, I want to reach a variable from another lua file, like that: Example1.lua: function example() string = "Test" end So, if I'm in another lua file (Example2.lua), and I want to get the "string" variable from the Example1.lua file, what do I need to do? I heard about "require", but I can't really understand how to use it. Thanks! Link to comment
Bssol Posted July 28, 2013 Share Posted July 28, 2013 look, if the two files are in the same resource, and same type ( server or client ) You should make the variable global, this happens when you you don't write 'local' before the variable. example of global variable: theString = "Hello World!" example of local variable: local theString = "Hello World!" If the two files in the same resource, the global variables will be shared in the whole resource, like this file1.lua theString = "Hello World!" file2.lua outputChatBox(theString) However, if you want to get the value of the variable in another resource, you have to use export functions. Link to comment
Tomc0 Posted July 28, 2013 Author Share Posted July 28, 2013 Thanks for the help! And if I want to use a variable from a server side script in a client type script? I can't do it? Link to comment
Castillo Posted July 28, 2013 Share Posted July 28, 2013 Client and server side scripts don't share the variables by default, but you could sync them. Link to comment
Tomc0 Posted July 28, 2013 Author Share Posted July 28, 2013 Can you help me with that? Do I need to use the "require" lua statement for that? Thanks! Link to comment
Castillo Posted July 28, 2013 Share Posted July 28, 2013 "require" on MTA is not used, nor available. You could send the variable to the client side everytime you update it, or create an element and use element data. Link to comment
Miyuru Posted November 9, 2020 Share Posted November 9, 2020 can share variable from server script to another server script? Link to comment
Moderators IIYAMA Posted November 9, 2020 Moderators Share Posted November 9, 2020 (edited) 5 hours ago, Miyuru said: can share variable from server script to another server script? no, each resource has it's own unique environment. But you can use export functions to retrieve variable values from other resources. https://wiki.multitheftauto.com/wiki/Call Keep in mind that: Tables are deep-copied. (loaded) Functions can't be past over. There is also elementdata, that can be act as an in-between storage place, but that has the same restrictions as exports. Edited November 9, 2020 by IIYAMA Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now