Help with printing a specific variable from a nested table

not 100% sure how to explain my issue so ill use an example

lets say i have a nested array in a datastore

sessionData[playerUserId] = {
				Save1 = {
					Name = "defaultname",
					TestInt = 5,
					LastSaved = "",
					PartCount = 2,
					partIndex = {
						{
							partShape = "sphere",
							isCustomMesh = false,
							customMesh = "nul",
							partColor = "#FFFFFF",
							partParent = StarterCharacter.Head,
							
						},
						--close part
						
						{
							partShape = "cube",
							isCustomMesh = false,
							customMesh = "nul",
							partColor = "#FFFFFF",
							partParent = StarterCharacter.Head,


						}
						--close part
					}
					--close index
					
					
				}
			}

theres DEFINITELY a better way to do this but i dont know it yet and this is at least formatted in a way i get it so its fine for now

anyway. how would i access the 2nd partShape

i know that

print(sessionData[playerUserId].partShape) 

will return the FIRST partShape, and using my limited knowlege of file structure and reading other posts id think itd be

print(sessionData[playerUserId].Save1.partIndex[2].partShape) 

since that is the path of the exact thing im trying to return
however this just returns the error

 ServerStorage.PlayerHandler:84: attempt to index nil with 'partIndex'

ive tried changing the part names making them like part1/part2 instead of just using the [pos] so itd be partIndex.part2.partShape instead bit this gives the same error, i feel like im missing a really simple solution but my frantic googling across the last 3 days as i try to gain access to posting here has returned NOTHING, im not not sure if its just not possible or if im missing the key words in my searches or what, any help highly appreciated

there’s some error in your data structure before reaching partindex one of the elements is nil

the issue prob one of those

sessionData[playerUserId] might be nil
sessionData[playerUserId].Save1 might be nil
sessionData[playerUserId].Save1.partIndex might be nil
sessionData[playerUserId] = {
    Save1 = {
        Name = "defaultname",
        TestInt = 5,
        LastSaved = "",
        PartCount = 2,
        partIndex = {
            {
                partShape = "sphere",
                isCustomMesh = false,
                customMesh = "nul",
                partColor = "#FFFFFF",
                partParent = StarterCharacter.Head,
            },
            --close part
            
            {
                partShape = "cube",
                isCustomMesh = false,
                customMesh = "nul",
                partColor = "#FFFFFF",
                partParent = StarterCharacter.Head,
            }
            --close part
        }
        --close index
    }
}

print("1. sessionData[playerUserId]:")
print(sessionData[playerUserId])

print("\n2. sessionData[playerUserId].Save1:")
print(sessionData[playerUserId].Save1)

print("\n3. sessionData[playerUserId].Save1.partIndex:")
print(sessionData[playerUserId].Save1.partIndex)

print("\n4. sessionData[playerUserId].Save1.partIndex[2]:")
print(sessionData[playerUserId].Save1.partIndex[2])

print("\n5. sessionData[playerUserId].Save1.partIndex[2].partShape:")
print(sessionData[playerUserId].Save1.partIndex[2].partShape)

i can’t see your whole code but however you can print each line to see where it stops something is nil before accessing that code

THANK YOU, i realized after this it was trying to access the data from storage when the storage didnt know it was suppose to hold that data/have a spot for it. i feel really silly for overlooking this but thank you again

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.