Light Switch Error

I’m trying to make a light switch script for houses. This is my script:

local switch = proximityPrompt.Parent
local lightswitch = script.Parent.Parent.Parent
local On = lightswitch.On
local Off = lightswitch.Off
local on=true
local soundon = lightswitch.Proximity.On
local soundoff = lightswitch.Proximity.Off

proximityPrompt.Triggered:Connect(function(player)
	if on == true then 
		on = false
		On.Transparency = 1
		Off.Transparency = 0
		soundoff:Play()
		for i,v in pairs(lightswitch.Lights:GetChildren()) do
			if v.PointLight then
				v.PointLight.Enabled = false
				v.Material = Enum.Material.SmoothPlastic
				else
				v.SpotLight.Enabled = false
				v.Material = Enum.Material.SmoothPlastic
			end
		end
	else
		on = true
		On.Transparency = 0
		Off.Transparency = 1
		soundon:Play()
		for i,v in pairs(lightswitch.Lights:GetChildren()) do
			if v.PointLight then
				v.PointLight.Enabled = true
				v.Material = Enum.Material.Neon
			else
				v.SpotLight.Enabled = true
				v.Material = Enum.Material.Neon
				end
			end
	end
end)

This is the error I am getting:


It still works fine even with the error, but I want to get rid of that error and am not sure why it is happening.
This is the model:

1 Like

v.PointLight as a conditional may fail if PointLight is not a child of v, try if v:FindFirstChild("PointLight") instead

2 Likes

Yes that works perfectly, thank you!

1 Like