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.
I will not be using your code at all in my example, however it should be similar enough to where you can understand.
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