Help with Module Script and Local Script

Hi all,

I’ve been developing a role playing sort of game, as a sort of a nice reintroduction to Roblox after a few years off.

Above is the set up of my workspace.

In said game, players control a character which is created using the “characterModule” script.
This script essentially creates a dictionary containing all sorts of info about said character, as seen below:

function char.createCharacter(name, age, nationality, ID)	-- Function that creates variables and values for a character
	local newCharDict = {									-- Dictionary that stores the contents of our character, such as:
		
		-- Basics
		["Character Name"] = name,							-- Character name
		["Character Age"] = age,							-- Character Age
		["Character Id"] = ID,								-- This is an ID which is unique to every character
		["Character Nationality"] = nationality,			-- Character Nationality
		
		-- Traits and Personality								These are Personality and Trait Objects
		
		["Character Personality"] = {},						-- A table which holds the characters personality - These cannot be changed
		["Character Traits"] = {},							-- A table which holds the characters traits - These can be changed
		
		-- Basic Stats											These are Integers
		
		["Character Stats"] = {								-- A table of character stats, whoch are influenced by their traits and personality
			Marshal = nil,									-- Ability to lead armies
			StewardShip = nil,								-- Ability to manage finances
			Learning = nil,									-- Ability to research
			Diplomacy = nil,								-- Ability to negotiate
			Intrigue = nil,									-- Ability to scheme
			
		},

		-- Physical Stats										These are a variatey of values
		["Character Physical"] = {
			Height = nil,									-- Integer, affects HeighScale of the physical Humanoid
			Fat = nil,										-- Integer, affects WidthScale and DepthScale of the physical Humanoid
			Skin = nil										-- Color3, affects the color3 of the physical Humanoid
		}
	}
	return newCharDict										-- Return the character so that it can be added to a dictionary
end

In the “worldCreation” script, these characters are created and added to a global table, alongside other bits and bobs required.

_G.charDict = {}
...
for i=1, 15 do 
	local newChar = characterModule.createCharacter(characterNamesMale[i], math.random(16,68), "Weslh", i)			-- Create a new Character
	_G.charDict[i] = {characterModule.createPhysicalCharacter(newChar, genChar), newChar}			-- Create a physical representation of that character
end

I finally have some GUI. The aim is to click a button and to pick a random character, and display their stats.

There’s one small issue however - The GUI code is in a LocalScript.
My question therefore is how would i access the “charDict” dictionary in my LocalScript? or at least, what would be some pointers on getting it to work?

Thanks!

1 Like

Looks like you have your ModuleScripts on ReplicatedStorage, so the LocalScript can access them just fine.

Just keep in mind that ModuleScripts can be hacked if the client can access them. So, keep anything the client could use to gain an advantage on ServerScriptService.

I thought this.
However, when i print out the Dictionary on the client from the LocalScript, its empty, whereas when printed from the server, its fully populated…

I tested it real quick, to make sure I’m not crazy.

I put a Module on ReplicatedStorage and another inside a LocalScript inside the GUI.

It worked as expected:

Here is the file I tested with:

TestModules.rbxl (53.2 KB)

The dictionary you’re building as _G.charDict from the server Script only exists in memory on the server. If client code needs to read from that dictionary, you have to either replicate it from server to client, or at least replicate the information needed to re-create it on the client.

The best way to do this is generally by sending a RemoteEvent from server to client. In the case of a large table that’s randomly generated, you can use Random.new( seed ) to generate the same table on both server and client, in which case all you need to send from server to client is the seed. Behavior of the Random generator is deterministic when seeded. For data that’s not generated, you have to just send the real data.

You can also use RemoteFunctions to make a function call on the client that can get data from the server. This is more or less just a yielding function wrapper around RemoteEvents. You can use RemoteEvents directly to achieve the same end, by sending a data request event from client to server, and have the server reply with a server to client event whose payload is the requested data.

You can also replicate value from server to client with Attributes, and with ValueObjects. These are both going to be slower and more cumbersome than RemoteEvents though, and they don’t work client to server.

P.S. You get no benefit from using the _G global; it is not shared by server and clients.

2 Likes