#How do use...indexes, I think..?
1 messages · Page 1 of 1 (latest)
so u wanna check if string is number?
or what
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..?
do u need to do smth completly diffrent depending on variables value?
Yes
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
and just do mytablevariable
which part
Could you give another example with integration and such..?
Just the format for it…
have u ever used tables?
.
I have not, sorry. I was driving home
you can make a table where you store values and asign them a function
local value = 1
local switch = {
[1] = function()
print("The value is 1")
end,
[10] = function()
print("The value is 10")
end,
["default"] = function()
print("Unknown value")
end
}
if switch[value] then
switch[value]()
else
switch["default"]()
end
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
thank you!!
how do I use this with a module variable?
local module = require(path.to.module)
module.variable
kk, thank you!
** You are now Level 6! **
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
or not...