Why does i,v pairs keep picking the wrong part?

I have a proximity prompt where I want all lights inside of a folder to turn off, but it keeps going to the wrong part in the model??? Someone help

local ProximityPrompt2 = script.Parent.ExternalBox.AttachmentPart.Attachment.ProximityPrompt


ProximityPrompt2.Triggered:Connect(function()
	local LightFolder = game.Workspace.LightFolder
	
	for i, v in pairs(LightFolder.Light:GetChildren()) do
		if v.Name == "Part" then
			print("WHY IS IT PICKING PART AND NOT LIGHTPART")
		else
			v.LightPart.PointLight.Enabled = false
		end
	end
end)

here is what my workspace looks like
image

I don’t understand your issue, what’s not working how intended?



ProximityPrompt2.Triggered:Connect(function()
	local LightFolder = game.Workspace.LightFolder
	
	for i, v in pairs(LightFolder:GetDescendants()) do
		if v.Name == "Part" then
			print("WHY IS IT PICKING PART AND NOT LIGHTPART")
		else
			v.LightPart.PointLight.Enabled = false
		end
	end
end)
1 Like

It isnt disabling the lights, it keep going to the “part” instead of “LightPart”.

an error i get :

LightPart is not a valid member of Part “Workspace.LightFolder.Light.LightPart” - Server

Well, I just gave you a simple code to figure it out yourself but… Oh well, here’s a fixed code:

for i, v in pairs(LightFolder:GetDescendants()) do
		if v:IsA("PointLight") then
				v.Enabled = true
		else
			v.Enabled = false
		end
	end
2 Likes

You could work around by doing if Descendant:FindFirstChildOfClass(“PointLight”) then disable light

1 Like

No errors or anything but it didnt toggle the lights some reason??

edit -

when adding an else to just run the same line again (shown below)

ProximityPrompt2.Triggered:Connect(function()
	local LightFolder = game.Workspace.LightFolder
	
	for i, v in pairs(LightFolder:GetDescendants()) do
		if v:IsA("PointLight") then
			v.Enabled = false
		else
			v.Enabled = false
		end
	end
end)

i get the error

Enabled is not a valid member of Model “Workspace.LightFolder.Light” - Server

Put my code under the line which triggers the proximity prompt.

local ProximityPrompt2 = script.Parent.ExternalBox.AttachmentPart.Attachment.ProximityPrompt

ProximityPrompt2.Triggered:Connect(function()
	local LightFolder = game.Workspace.LightFolder
	
	for i, v in pairs(LightFolder:GetDescendants()) do
		if v:IsA("PointLight") then
				v.Enabled = true
		else
			v.Enabled = false
		end
	end
end)
1 Like
local ProximityPrompt2 = script.Parent.ExternalBox.AttachmentPart.Attachment.ProximityPrompt -- Grabs the ProximityPrompt so you can get the signal "Triggered".


ProximityPrompt2.Triggered:Connect(function() -- On the signal it calls the function
	local LightFolder = game.Workspace.LightFolder -- Reference to the LightFolder
	
	for _, v in pairs(LightFolder:GetDescendants()) do -- Begins iterating through the folder, not assigning anything to the index (as we do not use that in this code) and assign the instance to the variable of "v".
		if v:IsA('PointLight') then -- Checks if our instance is a PointLight, this ensures we do not have any errors and we are only changing the correct class of instance.
			v.Enabled = not v.Enabled -- Toggles it
		end
	end
end)

Edit: Added a comment to most lines explaining what they do.

6 Likes

Are you saying that if its not a Point light, the part will get disabled and not the point light itself?

Yeah actually that “else” doesn’t make sense I guess

1 Like

Oh yeah, I didn’t see that one :sweat_smile: .

Sorry.

1 Like

Beat me to it;
The only flaw with this is that if you have one light that is enabled and another that is disabled, they wont all be synchronized. But as long as your model is consistent then it should be fine.

2 Likes

It was just a small mistake, no need to apologies :+1:

2 Likes

@Zirkonir has fixed it.

              v
1 Like

Yeah, i checked it before replying to your other comment :sparkling_heart:

1 Like