So basically I've been trying to make a system with 61 light switches (with FilteringEnabled) that can be switched on/off across a map. The problem is that I tried many possibilities in order to make this function properly. So I tried this method, but it returns an error indicating that the function has too many upvalues. Is there any other possible alternatives to this?
WARNING, MEDIOCRE CODE INCOMING
-- [[ LIGHT SWITCHES ]] -- local switch1 = workspace.LightSwitch1 local switch2 = workspace.LightSwitch2 local switch3 = workspace.LightSwitch3 local switch4 = workspace.LightSwitch4 local switch5 = workspace.LightSwitch5 local switch6 = workspace.LightSwitch6 local switch7 = workspace.LightSwitch7 local switch8 = workspace.LightSwitch8 local switch9 = workspace.LightSwitch9 local switch10 = workspace.LightSwitch10 local switch11 = workspace.LightSwitch11 local switch12 = workspace.LightSwitch12 local switch13 = workspace.LightSwitch13 local switch14 = workspace.LightSwitch14 local switch15 = workspace.LightSwitch15 local switch16 = workspace.LightSwitch16 local switch17 = workspace.LightSwitch17 local switch18 = workspace.LightSwitch18 local switch19 = workspace.LightSwitch19 local switch20 = workspace.LightSwitch20 local switch21 = workspace.LightSwitch21 local switch22 = workspace.LightSwitch22 local switch23 = workspace.LightSwitch23 local switch24 = workspace.LightSwitch24 local switch25 = workspace.LightSwitch25 local switch26 = workspace.LightSwitch26 local switch27 = workspace.LightSwitch27 local switch28 = workspace.LightSwitch28 local switch29 = workspace.LightSwitch29 local switch30 = workspace.LightSwitch30 local switch31 = workspace.LightSwitch31 local switch32 = workspace.LightSwitch32 local switch33 = workspace.LightSwitch33 local switch34 = workspace.LightSwitch34 local switch35 = workspace.LightSwitch35 local switch36 = workspace.LightSwitch36 local switch37 = workspace.LightSwitch37 local switch38 = workspace.LightSwitch38 local switch39 = workspace.LightSwitch39 local switch40 = workspace.LightSwitch40 local switch41 = workspace.LightSwitch41 local switch42 = workspace.LightSwitch42 local switch43 = workspace.LightSwitch43 local switch44 = workspace.LightSwitch44 local switch45 = workspace.LightSwitch45 local switch46 = workspace.LightSwitch46 local switch47 = workspace.LightSwitch47 local switch48 = workspace.LightSwitch48 local switch49 = workspace.LightSwitch49 local switch50 = workspace.LightSwitch50 local switch51 = workspace.LightSwitch51 local switch52 = workspace.LightSwitch52 local switch53 = workspace.LightSwitch53 local switch54 = workspace.LightSwitch54 local switch55 = workspace.LightSwitch55 local switch56 = workspace.LightSwitch56 local switch57 = workspace.LightSwitch57 local switch58 = workspace.LightSwitch58 local switch59 = workspace.LightSwitch59 local switch60 = workspace.LightSwitch60 local switch61 = workspace.LightSwitch61 -- [[ Normal Variables ]] local plr = game.Players.LocalPlayer local chr = plr.CharacterAdded:wait() local notice = script.Parent.Parent.HUD.Notices.LightSwitch local UIS = game:GetService('UserInputService') local remote = game.ReplicatedStorage.InGameEvents.LightSwitch -- [[ Events ]] UIS.InputBegan:Connect(function(input) if input.KeyCode == Enum.KeyCode.E then if (chr.Torso.Position - switch1.Switch.Position).magnitude <= 1.8 then remote:FireServer('1') elseif (chr.Torso.Position - switch2.Switch.Position).magnitude <= 1.8 then remote:FireServer('2') elseif (chr.Torso.Position - switch3.Switch.Position).magnitude <= 1.8 then remote:FireServer('3') elseif (chr.Torso.Position - switch4.Switch.Position).magnitude <= 1.8 then remote:FireServer('4') elseif (chr.Torso.Position - switch5.Switch.Position).magnitude <= 1.8 then remote:FireServer('5') elseif (chr.Torso.Position - switch6.Switch.Position).magnitude <= 1.8 then remote:FireServer('6') elseif (chr.Torso.Position - switch7.Switch.Position).magnitude <= 1.8 then remote:FireServer('7') elseif (chr.Torso.Position - switch8.Switch.Position).magnitude <= 1.8 then remote:FireServer('8') elseif (chr.Torso.Position - switch9.Switch.Position).magnitude <= 1.8 then remote:FireServer('9') elseif (chr.Torso.Position - switch10.Switch.Position).magnitude <= 1.8 then remote:FireServer('10') elseif (chr.Torso.Position - switch11.Switch.Position).magnitude <= 1.8 then remote:FireServer('11') elseif (chr.Torso.Position - switch12.Switch.Position).magnitude <= 1.8 then remote:FireServer('12') elseif (chr.Torso.Position - switch13.Switch.Position).magnitude <= 1.8 then remote:FireServer('13') elseif (chr.Torso.Position - switch14.Switch.Position).magnitude <= 1.8 then remote:FireServer('14') elseif (chr.Torso.Position - switch15.Switch.Position).magnitude <= 1.8 then remote:FireServer('15') elseif (chr.Torso.Position - switch16.Switch.Position).magnitude <= 1.8 then remote:FireServer('16') elseif (chr.Torso.Position - switch17.Switch.Position).magnitude <= 1.8 then remote:FireServer('17') elseif (chr.Torso.Position - switch18.Switch.Position).magnitude <= 1.8 then remote:FireServer('18') elseif (chr.Torso.Position - switch19.Switch.Position).magnitude <= 1.8 then remote:FireServer('19') elseif (chr.Torso.Position - switch20.Switch.Position).magnitude <= 1.8 then remote:FireServer('20') elseif (chr.Torso.Position - switch21.Switch.Position).magnitude <= 1.8 then remote:FireServer('21') elseif (chr.Torso.Position - switch22.Switch.Position).magnitude <= 1.8 then remote:FireServer('22') elseif (chr.Torso.Position - switch23.Switch.Position).magnitude <= 1.8 then remote:FireServer('23') elseif (chr.Torso.Position - switch24.Switch.Position).magnitude <= 1.8 then remote:FireServer('24') elseif (chr.Torso.Position - switch25.Switch.Position).magnitude <= 1.8 then remote:FireServer('25') elseif (chr.Torso.Position - switch26.Switch.Position).magnitude <= 1.8 then remote:FireServer('26') elseif (chr.Torso.Position - switch27.Switch.Position).magnitude <= 1.8 then remote:FireServer('27') elseif (chr.Torso.Position - switch28.Switch.Position).magnitude <= 1.8 then remote:FireServer('28') elseif (chr.Torso.Position - switch29.Switch.Position).magnitude <= 1.8 then remote:FireServer('29') elseif (chr.Torso.Position - switch30.Switch.Position).magnitude <= 1.8 then remote:FireServer('30') elseif (chr.Torso.Position - switch31.Switch.Position).magnitude <= 1.8 then remote:FireServer('31') elseif (chr.Torso.Position - switch32.Switch.Position).magnitude <= 1.8 then remote:FireServer('32') elseif (chr.Torso.Position - switch33.Switch.Position).magnitude <= 1.8 then remote:FireServer('33') elseif (chr.Torso.Position - switch34.Switch.Position).magnitude <= 1.8 then remote:FireServer('34') elseif (chr.Torso.Position - switch35.Switch.Position).magnitude <= 1.8 then remote:FireServer('35') elseif (chr.Torso.Position - switch36.Switch.Position).magnitude <= 1.8 then remote:FireServer('36') elseif (chr.Torso.Position - switch37.Switch.Position).magnitude <= 1.8 then remote:FireServer('37') elseif (chr.Torso.Position - switch38.Switch.Position).magnitude <= 1.8 then remote:FireServer('38') elseif (chr.Torso.Position - switch39.Switch.Position).magnitude <= 1.8 then remote:FireServer('39') elseif (chr.Torso.Position - switch40.Switch.Position).magnitude <= 1.8 then remote:FireServer('40') elseif (chr.Torso.Position - switch41.Switch.Position).magnitude <= 1.8 then remote:FireServer('41') elseif (chr.Torso.Position - switch42.Switch.Position).magnitude <= 1.8 then remote:FireServer('42') elseif (chr.Torso.Position - switch43.Switch.Position).magnitude <= 1.8 then remote:FireServer('43') elseif (chr.Torso.Position - switch44.Switch.Position).magnitude <= 1.8 then remote:FireServer('44') elseif (chr.Torso.Position - switch45.Switch.Position).magnitude <= 1.8 then remote:FireServer('45') elseif (chr.Torso.Position - switch46.Switch.Position).magnitude <= 1.8 then remote:FireServer('46') elseif (chr.Torso.Position - switch47.Switch.Position).magnitude <= 1.8 then remote:FireServer('47') elseif (chr.Torso.Position - switch48.Switch.Position).magnitude <= 1.8 then remote:FireServer('48') elseif (chr.Torso.Position - switch49.Switch.Position).magnitude <= 1.8 then remote:FireServer('49') elseif (chr.Torso.Position - switch50.Switch.Position).magnitude <= 1.8 then remote:FireServer('50') elseif (chr.Torso.Position - switch51.Switch.Position).magnitude <= 1.8 then remote:FireServer('51') elseif (chr.Torso.Position - switch52.Switch.Position).magnitude <= 1.8 then remote:FireServer('52') elseif (chr.Torso.Position - switch53.Switch.Position).magnitude <= 1.8 then remote:FireServer('53') elseif (chr.Torso.Position - switch54.Switch.Position).magnitude <= 1.8 then remote:FireServer('54') elseif (chr.Torso.Position - switch55.Switch.Position).magnitude <= 1.8 then remote:FireServer('55') elseif (chr.Torso.Position - switch56.Switch.Position).magnitude <= 1.8 then remote:FireServer('56') elseif (chr.Torso.Position - switch57.Switch.Position).magnitude <= 1.8 then remote:FireServer('57') elseif (chr.Torso.Position - switch58.Switch.Position).magnitude <= 1.8 then remote:FireServer('58') elseif (chr.Torso.Position - switch59.Switch.Position).magnitude <= 1.8 then remote:FireServer('59') elseif (chr.Torso.Position - switch60.Switch.Position).magnitude <= 1.8 then remote:FireServer('60') elseif (chr.Torso.Position - switch61.Switch.Position).magnitude <= 1.8 then remote:FireServer('61') end end end)
First, if you have many similar objects, it's a great idea to put them all in an array. This allows you to access a light switch based on it's number in the array, like so:
local lightSwitches = {workspace.LightSwitch1, workspace.LightSwitch2, workspace.LightSwitch3, workspace.LightSwitch4, workspace.LightSwitch5} --Continue adding your lights from here... print(lightSwitches[1])
Alternatively, you can automate discovery of your lights, using a for-each loop. Don't do this too often, as it is quite expensive. To be more efficient, you could put all your lights into a common folder you could quickly scan through. To do this, you should name them all the same thing. It's not important for you to be able to distinguish between light switches, so this is by far the easiest solution.
local placeWithAllLights = workspace.folderWithLights --This might be workspace for you if you didn't put your lights in a folder. local lightSwitches = {} --Telling the game that this variable will be an array, so it knows what to expect for index, object in pairs(placeWithAllLights:GetChildren()) do --i is the index, object is the current value of the array. if object.Name == "LightSwitch" then --check to make sure it is a light switch... table.insert(lightSwitches, object) --add the light switch to the array print("This is a light!") else print("Not a light!") end end print("Finished scanning for lights, found ", #lightSwitches)
This should give you an array with all of your lights in it. Then, it's a simple matter of scanning through lights when E is pressed, and finding the closest one. This is what it would look like finished:
local placeWithAllLights = workspace.folderWithLights --This might be workspace for you if you didn't put your lights in a folder. local lightSwitches = {} --Telling the game that this variable will be an array, so it knows what to expect for index, object in pairs(placeWithAllLights:GetChildren()) do --i is the index, object is the current value of the array. if object.Name == "LightSwitch" then --check to make sure it is a light switch... table.insert(lightSwitches, object) --add the light switch to the array print("This is a light!") else print("Not a light!") end end print("Finished scanning for lights, found ", #lightSwitches) local nearestLightSwitchIndex local nearestLightDistance = math.huge local range = 1.8 --Define your range in a variable to easily change it later... UIS.InputBegan:Connect(function(input) if input.KeyCode == Enum.KeyCode.E then for index, lightSwitch in pairs(lightSwitches) do local distance = (chr.Torso.Position - lightSwitch.Switch.Position).magnitude if distance <= range and distance < nearestLightDistance then --The second part guarantees that we get the absolute closest switch nearestLightSwitchIndex = index nearestLightDistance = distance end end end remote:FireServer(tostring(nearestLightSwitchIndex )) --Finally, convert the number index to a string for compatibility with your in place code, and fire the server. end)
I HIGHLY recommend reading up on for loops and generic for loops, as well as tables/array. They can make life a million times easier in the long run. Hope I helped!
http://wiki.roblox.com/index.php?title=Loops
http://wiki.roblox.com/index.php?title=Generic_for
http://wiki.roblox.com/index.php?title=Table