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

Why define table from module returns nil?

Asked by 4 years ago

Hello, im just making a game with alot of IDs for different stuff in shop so i used module script to put all the data from the shop there. Actually i gonna send only one thing from the script because if i send the whole script it will be a spam and also its pointless because everything else is same as this just different values on it. So this is my module script

local info = {}
Chars = {
    ["Robot1"] = {
        Name = "Robot",
        Price = 0,
        Health = 100,
        Head = 0,
        Torso = 54116290,
        RightArm = 54116338,
        RightLeg = 54116432,
        LeftArm = 54116373,
        LeftLeg = 54116394,

    },
    }
return info

Im trying to use some of the values there into my actual codes but the output says that table "Chars" is a nil value. I tried a few different ways to do it but it still does not work do you know why? Here is what i tried. I will put just a simple prints and the errors

local info = require(game.ServerStorage:WaitForChild("Modules").Info)
print(info:GetChars()[1][1]) -- attempt to call method "GetChars"(a nil value)
print(info.Chars[1][1]) -- attempt to index field "Chars" (a nil value)
print(info.Chars["Robot1"]["Name"]) -- attempt to index field "Chars" (a nil value)

Anyone know why?

0
You did not assign anything to info, so basically it is an empty table, so of course it will return nil nc2r 117 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

It's pretty simple.

Look at line 1 for me.

local info = {}

info points to an empty table.

You meant for Chars to be a field in info.

This works:

local info = {
    Chars = {
        Robot1 = {
            Name = "Robot",
            Price = 0,
            Health = 100,
            Head = 0,
            Torso = 54116290,
            RightArm = 54116338,
            RightLeg = 54116432,
            LeftArm = 54116373,
            LeftLeg = 54116394
        }
    }
}
return info
0
I have no idea why it is indenting so bad programmerHere 371 — 4y
0
Thanks sir, I didnt notice that but its actually still nil. here is the new code: https://codeshare.io/213kqn theswagboy0813 -14 — 4y
0
Communicate with me in codeshare. It shouldn't be nil. You probably wrote it wrong or you added something/removed something that messed it up programmerHere 371 — 4y
Ad

Answer this question