Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I print a table module script?

local module = {}

function module.Molly()
	local Ammo = 555
end

return module
local s = require(game.ReplicatedStorage.Test)

print(s.Ammo) <--- Idk how to print that table

1 comment

0

555 lol

1 answer

##Problem

Module scripts do not turn over variables to the script requiring it. Meaning, you would have to refer to the module script each time you want to gather the variable.


##Solution

There are a couple ways to go about gathering information. Either through getters or through the table you're returning.


##Getters Script

A "getter" is known in programming terms to be a value that gets returned to another class or code to be set as a variable.

Module Script:

local module = {}
local Ammo = 555

function module.Molly()
	return Ammo
end

return module

Requiring Script:

local mod = require(moduleName)
local Ammo = mod.Molly()

Since the value gets returned when calling the function, all we simply have to do is set Ammo in the Requiring Script to a variable. Otherwise, you'd be calling mod.Molly() every time you need Ammo.


##Through The Returned Table

For this script, you don't even need a function to return a value.

Module Script:

local module = {
	Ammo = 555
}

--Alternative method for outside the table would be to set module.Ammo = 555.
return module

Requiring Script:

local mod = require(moduleName)
local Ammo = mod.Ammo

######Hopefully this answered your question. If it did, do not forget to hit the accept answer button. If you have any questions, leave them in the comments below.

1 comment

0

In your ModuleScript examples you need to explicitly return 'module' or calling require on them would return nil.

This discussion is preserved as it was. New answers, comments, and votes are closed.