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

How to use Modulescripts instead of _G?

Asked by 6 years ago

A fellow developer helped me with my question on "why my GameRounds script breaks after a certain numbers of rounds", so basically he gave me some tips to clean it up like, don't use :remove() use :Destroy() and so on im not sure if its fixed but he mentioned something else i am stuck on.

I have corrected those errors, but now the part that calls the gamemode script is

local gameModes = {_G.eliminationRound, _G.teamSwapRound, _G.controllerRound,}
    local randomSlot = math.random(1, #gameModes)
    local randomMode = gameModes[randomSlot]
    roundType = gameModeNames[randomSlot]
    _G.announce("Round chosen: "..gameModeNames[randomSlot], Color3.new(0, 0, 0), 2)
    wait(1)

as you can see it has _G.

From a little research, others say that is a unorganized way and its buggy i'm guessing and i should use Modulescripts, the problem is i dont know how to use modulescript,

how do i use modulescript for this? thank you!

1
There are way too much posts on module scripts so I recommend you search up module scripts on Scripting Helpers Search Bar. Also, study from this wiki: https://wiki.roblox.com/index.php?title=API:Class/ModuleScript . They really are not hard to learn. Zafirua 1348 — 6y
0
Oh ok see i had no idea that it was that many out there, ill read on that thanks for your help! Carforlife1 59 — 6y

1 answer

Log in to vote
2
Answered by 6 years ago
Edited 6 years ago

Module Scripts

Module Scripts are scripts that can store anything. This can be given to both the client and server. _G lacks on this ability.

NOTE: When I say array, it is technically the same thing as a table

Let's start by setting up our first ModuleScript code

Let's insert our first module script in ReplicatedStorage

local module = {}

return module

This table is being returned at the end of our module script. I will explain later on why our return is very useful.

Let's put our first function inside of here

local module = {}

function module.announce()
    print("This function is in a module")
end

-- You can store variables as well.
module.Var1 = "hi"
module.Var2 = 5
module.Var3 = false
module.NewTable = {}

return module

NOTE: Values in ModuleScripts must be global(after looking at incapaz's anwser)

Just like arrays, you can insert values.

How to use ModuleScripts

Now that we have set up our ModuleScript, how can we use this? We have a built-in function called require(). This returns what we have returned in our ModuleScript(remember our module array)

local moduleScript = require(game.ReplicatedStorage.ModuleScript)

moduleScript.announce() -- Now we can call the function announce

You can use moduleScript.announce() in a LocalScript as well(You could not do this if you were to use _G)

Private Modules

A cool thing about ModuleScripts is that you can take an ID of a ModuleScript and require it.

Let's take an example!

local privateModule = require(0431449) -- This isn't a real ID but I think you get the point
privateModule.PrintFunc()

A huge downside to this is that you can't test it in studio. Especially when you have to test a game that heavily relies on Private Modules. Another downside that can be countered is that an exploiter can require the private module, then, parent it to a Data Model, save the game and then steal your private module. To easily counter this, place script = nil on the top of the ModuleScript.

I am hoping this helped you understand ModuleScripts and why you should use it over _G

0
This is great! Thank you so much! Carforlife1 59 — 6y
0
Your welcome! saSlol2436 716 — 6y
Ad

Answer this question