Attempt to index nil with 'Connect'

Hello fellow developers!
I am a novice developer, although I have created several games last 6 months.

Now I am developing an obby game and there are 24 checkpoints.
I am writing a server script in ServerScriptService so that a player can get some reward once per game session when a player touch a checkpoint part.

For that, I am using CollectionService and set tags for the checkpoints. These tag names are “CP1”, “CP2”,…“CP24”.
Once a player touch a checkpoint, the player should be tagged with the checkpoint tag name.

The following is part of code and I got an error message about line 17.
“attempt to index nil with ‘Connect’”.

I doubt the process to create taggedParts array (Line8-11) may be wrong,
but I don’t know how to fix it.
I appreciate if someone help me.

Please insert print(taggedPart) in lime 13 and share the result. Thank you. (This is not a solution, just a diagnosis.)

You are right in line 8 - 11 being wrong. GetTagged returns an array of the tagged parts.

So your tagged parts table would look like this

taggedParts = {
   {tagged}, {tagged}... and so on for all the 24
}

To fix this put a second pairs loop in the first one

for _, tag in ipairs(taggedParts) do
    for _, part in ipairs(tag) do
         -- touched event
   end
end

I think that should fix It, but I haven’t tested it. Hope this helps.

3 Likes

That should result into the error “Attempt to index table with Connect.” However, it returns “Attempts to index nil with Connect”. You are however correct about CollectionService returning arrays. I believe the instances are tagged after this script runs, so nil is returned everytime.

you would connect the touched event to the part gotten from looping through the tag array so this would not happen.

I tried print(taggedPart) and print(taggedParts) on Line13, but got the same error result.

Add a line after the for loop (in which you insert) and print out like this

print(taggedPart[1][1])

Please share what happens when you do it.

1 Like

I don’t think so. OP’s method should still return a table.

I modified the code and the error disappeared.
I think you are right.
Thank you so much!

I also add the test code on Line 13.

When op inserts the tagged parts array into the taggedParts table this creates a table nestled in a table.

taggedParts = {
    {arrray of parts tagged with CP1}, {arrray of parts tagged with CP2},
}

with all the arrays up to 24.

When op loops through the taggedParts table on line 15, the taggedPart variable is a table. This is the array of all the tagged parts with the tag. To get the actually parts, OP would then need to loop through the table from taggedParts, thus the for loop in the for loop. OP can then connect their touched event on line 17 to the part defined in the for loop.

2 Likes

Yes, I did. Pls check L13 of the updated code pic.

1 Like

Just to make it clear in simple language. When you use :GetTagged() it adds a table inside the table. So you need to loop inside of the second table. Hopefully you get what I mean!

It’s not exactly what it does, but I just explained in a simple language

Sorry but I am a novice developer and I didn’t know how to do that.
I checked the API-reference (Collection Service) and
thought I can use only one instance for the argument of GetTagged().
Because 24 tag names are all different, so I didn’t know how to handle it other than using a table. I will appreciate if u let me know any simpler ways.

1 Like

There’s no need to worry! There’s always something that we don’t know about!

1 Like