Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Too many 'upvalues' in function, Alternatives possible?

Asked by 7 years ago

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

001-- [[ LIGHT SWITCHES ]] --        
002local switch1 = workspace.LightSwitch1
003local switch2 = workspace.LightSwitch2
004local switch3 = workspace.LightSwitch3
005local switch4 = workspace.LightSwitch4
006local switch5 = workspace.LightSwitch5
007local switch6 = workspace.LightSwitch6
008local switch7 = workspace.LightSwitch7
009local switch8 = workspace.LightSwitch8
010local switch9 = workspace.LightSwitch9
011local switch10 = workspace.LightSwitch10
012local switch11 = workspace.LightSwitch11
013local switch12 = workspace.LightSwitch12
014local switch13 = workspace.LightSwitch13
015local switch14 = workspace.LightSwitch14
View all 202 lines...
0
that's lots of elseif statments, find a way to simplify your code saSlol2436 716 — 7y
1
plus, put your switches in an array since it just stores things and keeps your code organized saSlol2436 716 — 7y
0
I really don't know how to simplify the code, i've tried multiple methods and I can't think of another anymore Adrian12094 12 — 7y
0
bruh use for loops, arrays, tables, get children, and anything else! this code looks very inefficient. abnotaddable 920 — 7y
0
Ahh your code hurts my eyes. Please us an array raspyjessie 117 — 7y

1 answer

Log in to vote
2
Answered by 7 years ago

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:

1local lightSwitches = {workspace.LightSwitch1,
2                workspace.LightSwitch2,
3                workspace.LightSwitch3,
4                workspace.LightSwitch4,
5                workspace.LightSwitch5} --Continue adding your lights from here...
6print(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.

01local placeWithAllLights = workspace.folderWithLights --This might be workspace for you if you didn't put your lights in a folder.
02 
03local lightSwitches = {} --Telling the game that this variable will be an array, so it knows what to expect
04for index, object in pairs(placeWithAllLights:GetChildren()) do --i is the index, object is the current value of the array.
05    if object.Name == "LightSwitch" then --check to make sure it is a light switch...
06        table.insert(lightSwitches, object) --add the light switch to the array
07        print("This is a light!")
08    else
09        print("Not a light!")
10    end
11end
12print("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:

01local placeWithAllLights = workspace.folderWithLights --This might be workspace for you if you didn't put your lights in a folder.
02 
03local lightSwitches = {} --Telling the game that this variable will be an array, so it knows what to expect
04for index, object in pairs(placeWithAllLights:GetChildren()) do --i is the index, object is the current value of the array.
05    if object.Name == "LightSwitch" then --check to make sure it is a light switch...
06        table.insert(lightSwitches, object) --add the light switch to the array
07        print("This is a light!")
08    else
09        print("Not a light!")
10    end
11end
12print("Finished scanning for lights, found ", #lightSwitches)
13 
14local nearestLightSwitchIndex
15local nearestLightDistance = math.huge
View all 29 lines...

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

1
+1, but note: for loops aren't expensive (GetChildren is a bit, but not worth worrying about if you're not doing it regularly). If you have a folder with *just* light switches, your table of lights can come directly from :GetChildren(), rather than iterating over it and filtering by name. Finally, for the FireServer, I'd recommend sending the light switch itself, rather than its index. chess123mate 5873 — 7y
0
(Sending the light instead of its index isn't a concern if lights are never added/removed while the game is running.) chess123mate 5873 — 7y
0
@chess123mate About the check to verify a switch and the strange server firing, I wrote this code to integrate into OPs original script structure as a basically copy-paste job. Seems like a new scripter, and I didn't want to over-complicate things. Thanks for the tip about :GetChildren() though, wasn't aware it was an expensive function :) ! whenallthepigsfly 541 — 7y
0
Thanks mate Adrian12094 12 — 7y
0
You coulld do "workspace['LightSwitch'..number]" hiimgoodpack 2009 — 7y
Ad

Answer this question