Attempt To Index Nil With Connect


I am just started trying to learn lua and I got stuck. I did a tutorial for a fading part, and now I am trying to make it so that instead of the script being in the individual part, I have 1 script in the serverscriptservice that way I can duplicate the part without making a million scripts. However I get this error, any help would be appreciated, thank you

.Touched is a property found on all BaseParts. However, you are trying to index a table with the key Touched, which will be nil.

local platform = game.Workspace.FadingParts:GetChildren()
-- You can replace game.Workspace with Workspace
-- local platform = Workspace.FadingParts:GetChildren(

If you want to run fade on each of the parts, then you should iterate over the platform table.

for _, platform in pairs(Workspace.FadingParts:GetChildren()) do
    platform.Touched:Connect(fade)
end

Edit: I had written “Instances” instead of “BaseParts”. Thanks to @JackscarIitt for pointing it out!

2 Likes

Just a side note here, but not all Instances can have the Touched Event, as it only applies to BaseParts via Physics and such

image

Other Instances, such as BodyMovers (AlignOrientation, LinearVelocity, etc) don’t have this specific property

Even though FadingParts might just have only BaseParts, it’s still good to consider a sanity check first before assuming that everything inside it is confirmed a BasePart Instance

local Parts = workspace:WaitForChild("FadingParts") -- Just realized I stupidly lowercased the "C"

for _, Platform in pairs(Parts:GetChildren()) do
    local BasePartCheck = Platform:IsA("BasePart") -- Returns back as "true", or "false"

    if BasePartCheck == true then 
        Platform.Touched:Connect(fade)
    else
        warn(Platform, "is not a Base Part Instance!")
    end
end
1 Like

Yes, sorry, my bad. I meant all BaseParts. Thank you for correcting me! I’ll edit my post in order to prevent future confusion.

2 Likes

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