I am trying to get variables from a Module script to a script. This is my first time using module scripts so I'm having a bit of a difficulty. When I try to print out the value that I'm getting from the module script, it is nil. The module location is inside two folders, WeaponModule and Module, which is inside the ReplicatedStorage.
Module Script
local module = { { ["Damage"] = 100, ["MagazineSize"] = 5, ["ReloadSpeed"] = 4, ["ShotDelay"] = 1 } } return module
Script
local wepModule = game.ReplicatedStorage.Module.WeaponModule[toolName] local wepTable = require(wepModule) print(wepTable["MagazineSize"])
toolName is a variable passed through a function that is just a name of a tool. I have a bunch of modules planned for each weapon.
(I realized the issue right after posting my comment. I recommend that you still follow that as good habit anyway.)
The problem is that when you use the require()
command, it returns the variable. Image the module script as a function that you are just calling with another script.
local module = {} -- creates a table variable with the name module. Tables can hold almost anything as a value, including functions, parts, other tables, etc. return module -- returns the module table
So, since its returning the module, you are indexing the table itself. You have a table inside the module table, and you need to index that one first. As such:
local wepModule = game.ReplicatedStorage.Module.WeaponModule[toolName] local wepTable = require(wepModule) print(wepTable[1]["MagazineSize"])
Although I would reccomend instead that you just modify the module to be this:
local module = { ["Damage"] = 100, ["MagazineSize"] = 5, ["ReloadSpeed"] = 4, ["ShotDelay"] = 1 } return module
I would personally do this for readability as well.
local module = { ["Damage"] = 100; ["MagazineSize"] = 5; ["ReloadSpeed"] = 4; ["ShotDelay"] = 1 } return module
You can remove the local module = { { ... } }
only use local module = { ... }
Example:
-- MODULE -- local module = { ["Arg1"] = 500, ["Arg2"] = 50, } return module -- SCRIPT local Table = require(script.Parent) -- Location of module print(Table["Arg1"]) print(Table["Arg2"])
Here is your fixed script:
MODULE SCRIPT
local module = { ["Damage"] = 100, ["MagazineSize"] = 5, ["ReloadSpeed"] = 4, ["ShotDelay"] = 1 } return module
Your server script are correct.
if you want to fix with changing the server script try this:
local wepModule = game.ReplicatedStorage.Module.WeaponModule[toolName] local wepTable = require(wepModule) print(wepTable[1]["MagazineSize"]) -- Added [1] for get first table/argument in module script.
Hope it helped :D
i dont see why it would be any different, but try doing
local module = { { Damage = 100, MagazineSize = 5, ReloadSpeed = 4, ShotDelay = 1 } } return module
and then do print(wepTable.MagazineSize)
if it's still nil, your other code is wrong in some way