Attempt to index nil with 'Connect' while trying to check if any proximity prompts are triggered within a folder

I get the error attempt to index nil with ‘Connect’ , heres my code:

local Players = game:GetService("Players")

local player = game.Players.LocalPlayer

local Prompts = game.Workspace.CheckIn:GetDescendants("ProximityPrompt")

local TweenService = game:GetService("TweenService")

local UI = game.StarterGui.RoomBookingSystem:GetChildren()

local GroupID = 6880318

local GroupRank = 7

--//AVATAR AND LOCAL PLAYER DATA

-- Fetch the thumbnail

local userId = player.UserId

local thumbType = Enum.ThumbnailType.HeadShot

local thumbSize = Enum.ThumbnailSize.Size420x420

local content, isReady = Players:GetUserThumbnailAsync(userId, thumbType, thumbSize)

-- Set the ImageLabel's content to the user thumbnail

local imageLabel = script.Parent.Backdrop.UserIcon

imageLabel.Image = content

-- Username

local TextLabel = script.Parent.Backdrop.Text

TextLabel.Text = "@".. player.Name

if TextLabel.Text == "@".. player.Name then

-- ends the code

end

--// Tweens

local UIOpen = TweenInfo.new(

0.5, --Time

Enum.EasingStyle.Sine, -- Style of door open animation

Enum.EasingDirection.InOut, -- Direction of easing.

0, -- Repeat count.

false, -- Will it reverse?

0 -- The delay time.

)

--//ToggleUI

Prompts.Triggered:Connect(function()

return

end)

Any help is appreciated :smiley:!

I don’t know too much about proximity prompts, but I believe you’re using :GetDescendants incorrectly. :GetDescendants returns an array with all of the descendants, and I believe you’re trying to connect an event to that array.

Try this:

for i,v in pairs(game.Workspace.CheckIn:GetDescendants()) do
	if v.Name == "ProximityPrompt" then
		v.Triggered:Connect(function()
			-- enter code here
		end)
	end
end

Again, I really haven’t looked into documentation for proximity prompts so If I’m super wrong or off base, I apologize.

1 Like

Thank you, this worked fine :smiley:!

This is good, all I’d change is this:

if v.Name == "ProximityPrompt" then

To this:

if v:IsA("ProximityPrompt") then

That way you aren’t restricting the names of the ProximityPrompt instances.

2 Likes

Ah. I was going off his code where he was trying to look for descendants named ProximityPrompt. I didn’t know proximity prompts were an instance as I haven’t had used them yet,

Thank you for future reference!

1 Like