local lights = game.Workspace.Interactables.Switches:GetChildren() function keydown(k) if k:lower() == "e" and ((script.Parent.Parent.Character.Torso.Position - lights.Position).magnitude) < 2.25 then for a, aa in pairs(game.Workspace.Interactables.Lights:FindFirstChild(lights.Name):GetChildren()) do if aa.Light.Brightness == 0 then aa.Light.Brightness = 1000 else aa.Light.Brightness = 0 end end end end local m = script.Parent.Parent:GetMouse() m.KeyDown:connect(keydown)
What I'm trying to do is well. I have a lot of light switches and I'm trying to avoid using a billion scripts (one in each lightswitch) and use one local script in starter pack. I'm not sure how to make it work with every light switch? And also. This script does NOT work. and when it was working with one lightswitch it did NOT work online.
local interactables = game.Workspace.Interactables function keydown(k) if k:lower() == "e" then for i, v in pairs(interactables.Switches:GetChildren()) do if game.Players.LocalPlayer:DistanceFromCharacter(v) < 2.25 then for k, va in pairs(interactables.Lights:FindFirstChild(lights.Name):GetChildren()) do --I'm assuming "lights.Name" is valid va.Light.Enabled = not va.Light.Enabled --set the brightness to 1000 in studio end break --if it changes the lights, don't keep looking for a switch close to the player. end end end end local m = game.Players.LocalPlayer:GetMouse() --stays intact in case the ancestry changes, assuming its a local script m.KeyDown:connect(keydown)
Feel free to comment with questions and problems as I have not tested this. If it helps, please accept, and/or vote up.