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

How do i change _G to module form in my code?

Asked by 5 years ago

i'm back again, with the same question sorta, but how do i make my code into a modulescript form, i'm reading about modulescripts but its not helping because no one is providing a example with my code and not codes that are saying "HI" in a input.

I dont know were to start, where do i put what?, because i have a feeling whats causing my game to break is these garbage _G functions, and i need to eliminate those as soon as possible.

So if you would be kind enough to provide, a sample of what i should do with my code i am about to show you.

function getRandomMode()
    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)
    local description = descriptions[randomSlot]
    for index, text in pairs(description) do
        _G.announce(text, Color3.new(0, 0, 0), 2)
        wait(1)
    end
    return randomMode
end

function getRandomMap()
    local gameMaps = game.Lighting.Maps:GetChildren()
    local randomMap = gameMaps[math.random(1, #gameMaps)]
    _G.announce("Map chosen: "..randomMap.Name, Color3.new(0, 0, 0), 2)
    return randomMap
end

function getRandomSpawn(teamColor)
    if (workspace:findFirstChild("Map")) then
        local spawns = {}
        if (teamColor == game.Teams.Red.TeamColor) then
            spawns = workspace.Map.Red:GetChildren()
        else
            spawns = workspace.Map.Blue:GetChildren()
        end
        local spawn = spawns[math.random(1, #spawns)]
        return spawn
    end
    return nil
end

Exactly how do i eliminate these _G functions on my code, thank you for helping.

1 answer

Log in to vote
0
Answered by
amanda 1059 Moderation Voter
5 years ago
Edited 5 years ago

I will not be using your code at all in my example, however it should be similar enough to where you can understand.

_G is a shared table, and still only a table. As you've been previously informed, the better alternative, is using modules.

Modules can contain and return a table, so the simplest conversion is to store all of your methods and so on inside of a table which is returned

You currently have code somewhere that might look like this:

_G.Print = function(str)
    print("SERVER: "..str)
end

or maybe

_G.gamestate = "active"

You can maintain the same way of defining them, the only difference, is you will do it in a ModuleScript, and you will create those variables and methods as apart of the table in the module, and not the _G table.

Example:

local module = {}

module.Print = function(str)
    print("SERVER: "..str)
end)

module.gamestate = "active"

return module

Some other script:

local module = require(script.Module)
--replace script.Module with the path to the ModuleScript above

module.Print(module.gamestate) --> SERVER: active
0
so in my code i would replace the _G with local module =(script.Module) would i need to put anything else in the workspace. Carforlife1 59 — 5y
0
you need to use the keyword require, and the code I have in "Example" is the code that would be in a modulescript at the location you use require on. amanda 1059 — 5y
0
Also important to note that Module Scripts only return a single value. However, it is also important to note that that value can be anything. Zafirua 1348 — 5y
0
ok i get the purpose of modulescript, but how do i type it in my script, this example you gave me too me is completely different from my script that is why is said could someone make a example with my script because im still scratching my head and asking how do i convert that in to my script, i was going to type local module.GameModes = {.eliminationRound,} like that but with out the _G Carforlife1 59 — 5y
View all comments (2 more)
0
Part of being a scripter is not having everything handed to you. Go into studio and figure it out. I showed you the setup for something in the _G table, and how to put it in a module instead. The only difference between our scripts is the contents of our tables. amanda 1059 — 5y
0
In fact, this is the second question you've posted about this, and the other one was filled with answers that showed you how to use modules. If you understand that _G is just a table, and you understand how modules work, then the only thing left is to do it, not to ask someone to do it fot you. amanda 1059 — 5y
Ad

Answer this question