Make Module Script Run?

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

Any help will do, thanks!

1 Like
  1. 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

Use require from a ServerScript:

local context = require(path.to.module)
print(context) -- Should print either Multiplayer or Adventure

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

1 Like

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.

1 Like

True

After testing this all, I found that using a mix of this code and everybody elses will work perfectly! Thank you all so much for your time!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.