Jump to content

Get variable from another lua file


Tomc0

Recommended Posts

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

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
  • 7 years later...
  • Moderators
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 by IIYAMA
Link to comment

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...