Im trying to figure out if there is any way to compare multiple values to one value using if statements. right now the only way I know how to do this takes a long time and im wondering if i can shorten the amount of code I use.
01 | local comparative = false |
02 | local table = { Box 1 ,Box 2 ,Box 3 ,Box 4 ,Box 5 } |
03 | for i = 1 ,#table do |
04 | if (NewBox.Name = = table [ i ] .Name) then |
05 | comparative = true |
06 | end |
07 | if (comparative = = true ) then |
08 | print ( "NewBox is the same as one of the old boxes" ) |
09 | else |
10 | print ( "NewBox is not the same as any of the boxes" ) |
11 | end |
Here is the code that I don't know if it works or not.
1 | if (slot.Name = = "Box" .. { 1 , 2 , 3 , 4 , 5 } ) then |
2 | print ( "slot is an equiping spot" ) |
3 | end |
This answer's to be used as a reference
Perhaps the answer to your question's to use a dictionary
? A dictionary is a table with set indexes and values, vs arrays where they have numbers for their index (to put simply). An example for a dictionary would be.
01 | local Boxes = { -- Dictionary example |
02 | [ "Box1" ] = true ; -- `Box1` is the index, and `true` is the value for said index |
03 | [ "Box2" ] = true ; -- etc |
04 | [ "Box3" ] = true ; -- etc |
05 | } |
06 |
07 | if Boxes [ Box.Name ] then -- Checks to see if the name's in the table, and if it is... |
08 | print ( 'Box found!' ) -- Print that there was! :D |
09 | else -- If not... |
10 | print ( 'No such box found!' ) -- Nothing! D: |
11 | end |
Stuff touched on
Dictionaries
- As the dev hub states, "a set of key/value pairs".If you have any questions, please let me know. :) I hope this helped! :D