Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I print a table module script?

Asked by 7 years ago
Edited 7 years ago
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
0
555 lol dark_nineret -25 — 5y

1 answer

Log in to vote
1
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
7 years ago
Edited 7 years ago

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.
0
In your ModuleScript examples you need to explicitly return 'module' or calling require on them would return nil. duckwit 1404 — 7y
Ad

Answer this question