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

How come my variables inside my module script are returning nil?

Asked by 6 years ago

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.

0
Try doing :FindFirstChild(toolName) or :WaitForChild(toolName). There is a chance that it might not be able to find it properly. Also with any kind of dictionary, you can index anything that has no spaces with something like wepTable.MagazineSize Kroniso 20 — 6y
0
Functionally, both are the same. tab.index is merely syntax sugar for tab[index], though the former only works if the index is a legal identifier User#24403 69 — 6y
0
I've confirmed that toolName is being found correctly. Thanks for the tips about the dictionary. beeswithstingerss 41 — 6y

3 answers

Log in to vote
1
Answered by
Kroniso 20
6 years ago

(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
Ad
Log in to vote
0
Answered by
yHasteeD 1819 Moderation Voter
6 years ago
Edited 6 years ago

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

Log in to vote
-1
Answered by 6 years ago
Edited 6 years ago

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

Answer this question