How could I check to see if the name of an object was in a dictionary?

I tried checking the output for errors, but there weren’t any. It works fine when I write the actual name in there, however it’s very inefficient and I prefer not to do that. How would I check to see if the object’s name is equal to the one values in the dictionary? Would it be possible to do it?

function onClicked()
	for i,v in pairs (game.Workspace.StageLights:GetChildren()) do
		local names = {
			name1 = script.Parent.Parent.Name1.Value,
			name2 = script.Parent.Parent.Name2.Value,
			name3 = script.Parent.Parent.Name3.Value,
			name4 = script.Parent.Parent.Name4.Value,
		}
	if names[v.Name] then
			v.Body.Hinge.Motor6D.DesiredAngle = 0
			v.Holder.Connect.Motor6D.DesiredAngle = 1
	end
	end
	end
 
script.Parent.MouseButton1Down:connect(onClicked)

Just iterate through the “names” dictionary and compare each value to the name of the object.

I didn’t test this code but it should work:

function onClicked()
	local names = {
		name1 = script.Parent.Parent.Name1.Value,
		name2 = script.Parent.Parent.Name2.Value,
		name3 = script.Parent.Parent.Name3.Value,
		name4 = script.Parent.Parent.Name4.Value,
	}
	for i, v in pairs(workspace.StageLights:GetChildren()) do
		for key, name in pairs(names) do
			if v.Name == name then
				v.Body.Hinge.Motor6D.DesiredAngle = 0
				v.Holder.Connect.Motor6D.DesiredAngle = 1
			end
		end
	end
end

script.Parent.MouseButton1Down:connect(onClicked)
1 Like