Attempt to index nil with Triggered?

So, im making a system for opening vents & its very simple, but for some reason its saying Nil for Triggered. I have never seen this before. Ive reloaded Roblox Studio, checked for any misspeals, and I have no idea what else I can do.

CODE:

local Folder = script.Parent.Vents

for _, v in pairs(Folder:GetChildren()) do
	
	local Model = v
	
	local Prompt = Model:FindFirstChild("Prompt")
	local VelocityForVentPiece = Model:FindFirstChild("LinearVelocity")
	
	local IsOpen = false
	
	Prompt.Triggered:Connect(function() -- Why is it getting Nil for Triggered??
		if IsOpen == false then
			IsOpen = true
			Prompt.Enabled = false
			VelocityForVentPiece.Enabled = true
			task.wait(0.2)
			VelocityForVentPiece.Enabled = false
		else
			print("Opened, why is this showing up??")
		end
	end)
	
end

Here is a screenshot of it just in case you need it
ScreenShotOfVentPiece

Cheers

2 Likes

Check if the prompt exists, and I suggest using FindFirstChildOfClass instead of FindFirstChild

“Attempt to index nil with ‘Triggered’” actually means the value of Prompt is nil. This is because the model doesn’t contain your prompt, so :FindFirstChild() returns nil due to its failure to locate a child named Prompt. A very simple fix would be calling :FindFirstChild() for both MainVentPart and Prompt within the model instead:

local Prompt = Model:FindFirstChild'MainVentPart':FindFirstChild'Prompt'

Oh, and as @NormalDeveloperX stated, :FindFirstChildOfClass() may be convenient here since it accounts for unexpected name changes in both objects:

local Prompt = Model:FindFirstChildOfClass'UnionOperation':FindFirstChildOfClass'ProximityPrompt'

The answer above is correct; you just need to adjust the path to the button and Velocity :

local Folder = script.Parent.Vents

for _, v in pairs(Folder:GetChildren()) do
	
	local Model = v
	
	local Prompt = Model:FindFirstChild("MainVentPart"):FindFirstChild("Prompt")
	local VelocityForVentPiece = Model:FindFirstChild("MainVentPart"):FindFirstChild("LinearVelocity")
	
	local IsOpen = false
	
	Prompt.Triggered:Connect(function() -- Why is it getting Nil for Triggered??
		if IsOpen == false then
			IsOpen = true
			Prompt.Enabled = false
			VelocityForVentPiece.Enabled = true
			task.wait(0.2)
			VelocityForVentPiece.Enabled = false
		else
			print("Opened, why is this showing up??")
		end
	end)
	
end
1 Like

Ah, I see now. Kind of annoying that’s how it works, but know I know!

Cheers

1 Like

If it’s that much a nuisance, you can directly call :FindFirstDescendant() on the model which searches all descendants of an object for the instance with the specified name rather than just direct children:

local Prompt = Model:FindFirstDescendant'Prompt';
1 Like

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