Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

How to use different files?

Asked by 8 years ago
Edited 8 years ago

I am trying to use the commun object orientation, I know lua it's a little different from others programming languages, but I can't find how to use diferents files to be able to sort my code, something like this:

function dog.new ()
    dog = {}
    dog.name = "the name of the dog"
    return dog
end

and from another file I can call this fuction to get new dog

dog1 = game.ServerScriptService.Dog.new()
dog2 = game.ServerScriptService.Dog.new()

it's very simple, I trying to get the normal and simple way to import files to other files to inherit and keep an organized and recycled code.

Any help will be appreciated.

2 answers

Log in to vote
0
Answered by 8 years ago
Edited 8 years ago

Well, to put it simple, there're two ways you could go about this: you could use a module script, and have the main(?) script call on it, while the module script returns the set data, or you could use _G, which I highly recommend against using, as it shares across any serverside script, and does not work w/ localscripts.

This is how a module script would be set up, and how the main(?) script would call upon it:

-- Module

return {
    'Data'
}

-- Main Script

local returnTableFromModule = require(MODULE)
print(returnTableFromModule[1]) -- Data

To add, and note from before, the require function will call upon the module script, but also yield the code until the module script returns the code/ exists w/in the specific parent.

Now, onto _G; however, before I begin, I recommend NOT using this, as it has loopholes & security vulnerabilities, i.e. any serverside could retrieve it, localscripts can not communicate w/ it, and any serverside script could edit/ alter the data (at will), so if an exploiter wanted to edit the data at any time, it's free for the taking to edit/ alter.

This is how it would be set up, incase you're curious:

-- Script 1

_G.Data = {
    'Data'
}

-- Script 2

print(_G.Data[1]) -- Data

_G.Data = {
    'Altered Data'
}

-- Script 3

print(_G.Data[1]) -- Altered Data

More information about what was mentioned

  1. _G - I recommend AGAINST using this.

  2. Module Scripts - I highly recommend this over _G, as it has better security, and is safer for script to communicate through, + I believe localscripts can access this (however, I can not confirm this, so if you plan on doing this, proceed w/ care).

Stuff touched on, but didn't go into great detail about

The Require Function

Hope this helped you in any way! :D

0
Exploiters cannot add scripts to the server, can they? My impression is that they only have control over the client (though if you don't have FilteringEnabled on, I suppose they could...). And, if they do have control over the server, it doesn't matter whether you use _G or ModuleScripts, an added script can easily read from both locations. ModuleScripts are still better, as LocalScripts can use.. chess123mate 5873 — 8y
0
...the code. Of note, all server-side scripts have access to the contents of the ModuleScript, the same as _G. The ModuleScript only runs once (for the server). chess123mate 5873 — 8y
0
@chess123mate Ah, alright; ty for letting me know. :) As to exploiters controlling the client, I always believed they both messed w/ the client & server, *buuut* that seems to be wrong. XP TheeDeathCaster 2368 — 8y
0
lol it seems I really tripped up here; please forgive me. XD TheeDeathCaster 2368 — 8y
0
Um, well, tyvm for accepting my answer. XD TheeDeathCaster 2368 — 7y
Ad
Log in to vote
2
Answered by 8 years ago

Consider reading this lua tutorial on inheritance. They show you some good patterns and even have a class creator function which you might like to use.

Be warned, your script has two problems:

  1. You don't use 'local' (but should) when creating a variable in a function
  2. Never use the exact name of the class as the name of the variable (you have overwritten your class with the instance variable!)

Instead of using the link above, you might want to see how Roblox does this with the chat system - simply run Roblox Studio and look in the Explorer window for the 'Chat' content. Look for a "ClassCreator" script and look at how other ModuleScripts use it to create their own classes.

Here's a sample dog class, with usage:

--Dog ModuleScript
local Dog = {__index=Dog}
function Dog.new(name)
    local dog = {} --assign variables here if desired
    dog.name = name
    return setmetatable(dog, Dog)
end
function Dog:Bark()
    print(self.name .. " barks!") --self exists because of the ':' syntax on the previous line
end
return Dog


--Main script
local Dog = require(game.ServerScriptService.Dog)
local dog1 = Dog.new("Dog1")
local dog2 = Dog.new("Dog2")
dog1:Bark() --In lua, this is identical to saying "dog1.Bark(dog1)"
dog2:Bark()
--You can technically also use Dog.Bark(dog1), but you shouldn't do this unless you know for a certain that 'dog1' is a Dog and not some child class (ex a 'Terrier'). Since you generally don't know what class an object is without testing for it (which is a questionable thing to do in most cases), you should avoid doing it this way

This is the same style used in the link at the top.

Minor note: In some languages it is recommended to put every class in its own file. I've found it much more pleasant to put several related classes in the same script (treating ModuleScripts as a "package" or mini-library), but this is completely up to you. Roblox's chat system has a different ModuleScript for every class. If you want to use the "multiple classes in one file" system, how you require things is naturally a bit different:

--Animals ModuleScript
local Animals = {}
--Define Dog class here
Animals.Dog = Dog
--Define Cat class here
Animals.Cat = Cat
--etc
return Animals

--Main script
local Animals = require(game.ServerScriptService.Animals)
local myDog = Animals.Dog.new()
--otherwise the same

Answer this question