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

Help with ModuleScripts?

Asked by 9 years ago

So I have a modulescript, but this is very annoying. Seeing many examples, this should not be an error:

ModuleScript

local lib = {}

local lib;Assets = { -- script analysis tells me when i put a '.' instead of a ';', it is not allowed?
    SwordMesh = 10909387;
    PistolMesh = 13508017;
    AxeMesh = 38161203;
    HatMesh = 76062497;
    UnEquipAnimation = 199885477;
    EquipSwordAnimation = 199899943;
    UnEquipSwordAnim = 199921179;
    SwordHitAnimation = 200148600;
    SwordHitAnimation2 = 200199200;
    DodgeAnimation = 200246650;
    SwordLungeAnimation = 200262909;
    KickAnimation = 200283487;
    PunchAnimation = 200287046;
    EquipPistolAnimation = 200288522;
    PistolHold = 200292188;
    PistolHitAnimation = 200352915;
    PistolReloadAnimation = 200501545;
    PistolFlickAnimation = 200502436;
    EquipBladesAnimation = 200976094;
    BCombo1 = 200982931;
    BCombo2 = 200984042;
    Idle = 199926412;
    Idle2 = 199927268;
    SwordTexture = 10909398;
    PistolTexture = 13508018;
    AxeTexture = 38161622;
    ShirtTexture = 123727606;
    PantsTexture = 101469244;
    HatTexture = 76465978;
    ToolTexture = 48851464;
    FaceTexture = 97824388;
    HoodImage = 201035139;
    PistolImage = 201039164;
    AxeImage = 201038100;
    SwordImage = 201033741;
};
return lib

LocalScript

local lib = require(game.workspace:WaitForChild('Configuration'):WaitForChild('AssetHandler')) -- attempted to index local 'lib' (a nil value)
print(lib.Assets[1])

Any help?

1 answer

Log in to vote
3
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

TLDR

  • Only use local on variables, not properties/fields/members
  • Only do what the computer tells you to when you know why it's telling you to do it

What Went Wrong

Script Analysis is making a guess at what you meant to do -- but it made the wrong guess.

The proper suggestion here is to delete the local and keep the ..

Since lib.Assets isn't actually a variable (it's just a part of the table lib) it isn't legal to declare it as local.


What Script Analysis was suggesting

Script Analysis believes you are declaring lib again.

Since following it with another name (Assets) starts a new statement, it wants you to add a semicolon to make it really clear that you are starting a new statement, yet decided to not put a line-break in.

That is, it thinks you wanted to do this:

local lib
Assets = {

However, it is respecting your decision to keep it on the same line -- and proposing ; as an alternative to pressing enter.


Semicolons?

Semicolons are used to separate statements in Lua. This rarely affects how code works (when it would, the code without the semicolon will actually have a syntax error); however, it can make it clearer when you do things like group multiple statements onto a single line, e.g.,

a = 1 b = 2 c = 3

Rather than accidentally think this just one statement, Script Analysis wants you to add semicolons to make it really clear there are three statements here:

a = 1; b = 2; c = 3

Lua actually completely ignores whitespace*. That means you can put spaces, tabs, and newlines whereever* you want.

As a result, certain lines can be ambiguous:

cat = {}

dog = cat

(cat).meow()

Workspace.Script:5: ambiguous syntax (function call x new statement) near '('

The issue is it doesn't know if the above means

dog = cat(cat).meow()

That is, it's not sure if cat is being used as a function or not, since it ignores the newline.

A semicolon after dog = cat; fixes this.


The asterisk Lua won't ignore whitespace in a few obvious places: the middle of keywords / names. It also cares that there isn't space between operators like ~= and == (after all = means something else!).

Lastly, comments do care about whitespace, since -- ends only at a newline.

0
Thank you so much! killerkill29 35 — 9y
0
Also, when I do print(lib.Assets[2]), it prints as nil. Any help? killerkill29 35 — 9y
0
You don't have a list, you have a dictionary. There isn't a second element. If you want to get something out of it, use the name you used! E.g., `lib.Assets["SwordTexture"]` or the equivalent `lib.Assets.SwordTexture` BlueTaslem 18071 — 9y
0
Thank you! killerkill29 35 — 9y
Ad

Answer this question