Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why define table from module returns nil?

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?

1 comment

0

You did not assign anything to info, so basically it is an empty table, so of course it will return nil

1 answer

✓ Accepted answer

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

3 comments

0

I have no idea why it is indenting so bad

0

Thanks sir, I didnt notice that but its actually still nil. here is the new code: https://codeshare.io/213kqn

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

This discussion is preserved as it was. New answers, comments, and votes are closed.