#How do use...indexes, I think..?

1 messages · Page 1 of 1 (latest)

wide bronze
#

How do I have code check if a value is a certain numerical value without having a HUGE if-then else if-then else... chain?

quasi nimbus
#

or what

wide bronze
#

No, the variable will always be a number. Id just want code that does basically what this would do:

if variable == 1 then
Code
Else
If variable == 2 then
Code
Else
If variable == 3 then
Etc, but I know there is a better way that’s MUCH less messy, but idk how

#

I thought it was with indexes or something..?

quasi nimbus
#

do u need to do smth completly diffrent depending on variables value?

wide bronze
#

Yes

quasi nimbus
#

well like u said u could have a table

#

like

#

local mytable = {}
mytable[1] = function()
print("Hello world")
end

#

same for 1-2-3-4

wide bronze
#

I see…

#

I don’t fully understand, though…

quasi nimbus
#

which part

wide bronze
#

Could you give another example with integration and such..?

wide bronze
quasi nimbus
#

have u ever used tables?

quasi nimbus
wide bronze
lean smelt
#

switch[value]() will initiate the function assigned to a number in the table that is equal to the values number

#

so if value is 10, it will run function assign to [10]

#

which here will just print The value is 10

wide bronze
#

thank you!!

wide bronze
lean smelt
#
local module = require(path.to.module)

module.variable
wide bronze
#

kk, thank you!

oblique walrusBOT
#

studio** You are now Level 6! **studio

sly jetty
#

you could also set up a tablewith values and loop through and if the value you want equals the current loop iteration of the table you set a variable to that loops current index and value and then break

sly jetty
#

or not...