I am making a module script that is intended to run and determine whether the player is in multiplayer or singleplayer mode. How can I make the module script run like a normal script and achieve that? The module script is in ReplicatedStorage.
Here’s the code.
--//Game Context//
--[Variables]
local MultiplayerPLaceID = 0
local AdventurePlaceID = 0
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlaceID = game.PlaceId
local GameContext
do
if PlaceID == MultiplayerPLaceID then
--//Multiplayer
GameContext = "Multiplayer"
elseif PlaceID == AdventurePlaceID then
--//Adventure
GameContext = "Adventure"
end
end
return GameContext
Fix typo “MultiplayerPLaceID” to "MultiplayerPlaceID "
--[Variables]
local MultiplayerPlaceID = 0
local AdventurePlaceID = 0
MultiplayerPlaceID can be equaled to PlaceID that has more than 1 player joins
AdventurePlaceID can be equaled to a PlaceID that only allows one player joining
return true or false depending on what it is
local Module = {}
--//Game Context//
--[Variables]
local MultiplayerPLaceID = 0
local AdventurePlaceID = 0
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlaceID = game.PlaceId
local GameContext
function Module.ReturnMode() -- Call by using `local module = require(Path); module.ReturnMode()`
if PlaceID == MultiplayerPLaceID then
--//Multiplayer
GameContext = "Multiplayer"
elseif PlaceID == AdventurePlaceID then
--//Adventure
GameContext = "Adventure"
end
end
return Module
You’d use require() like normal, but have a Script inside it, but it will only run once due to only returning a single table (This is wrong) , requiring the ModuleScript even more Wouldn’t break your script but it wont run the code inside either ( unless its connected via function with the Main Table. )
You Use ModuleScripts to Store Data, OOP, and to keep yourself organized, You have functions for code specific code to Fire rather than having code fire when requiring the ModuleScript, They arent used to fire code themselves but for the Scripts to access it, and fire them from there.
If anything, you would just use a Regular Script with access to a ModuleScript
This is really personal preference. There are some people that make their code bases with a majority using ModuleScripts. You can have just 1 server and 1 local script and the rest of your game can be ModuleScripts.
It might sound goofy, but it’s still following the basic principle behind ModuleScripts. Its just taking it beyond what every Youtube tutorial says a ModuleScript can do. There is a hurdle at first on relearning how you need to structure your code, but the long-term benefit is having a codebase that is modular and easily expandable.