I am trying to make a keypad code door that has a random code in every server, I’ve managed create the keypad door and generate a randomized code for it, I’ve also managed to get the randomly generated code and store it in a table. But i can not figure out how i can check for each number in this table in an if statement. I need to do this so i can generate a binary version of the code and hide it somewhere in my map for people to decrypt.
I have tried using table.find but it didn’t work. here’s a short sample of the long (and most likely inefficient) script i’ve used, since it’s too long and repeating this same thing i won’t post the whole thing (i used elseif to check for all the numbers ranging from 0 to 9 for the all 5 digits that the random generated code contains)
if table.find(therandomcode, 3) == 1 then
thirddigit = 00110001
elseif table.find(therandomcode, 3) == 2 then
thirddigit = 00110010
i know this is probably not the most optimal way to make the thing im trying to make, but this is my first time working with tables so i am experimenting with things.
edit: i am trying to store these binary values in individual NumberValues
Firstly, i believe you are using table.find incorrectly, table.find returns the index of a value if it is found( unless you are doing that) . if you are trying to iterate through a table you could just use for loops
the numbers of the code are individually stored in the therandomcode table i’ve created here, I’ve tried creating a table for binaries like you showed and then did the for loop with print(index) to check if it has worked but it didn’t. It didn’t print anything, it didn’t even give an error message. I also don’t know if this could be done with a for loop since the numbers have to be in exact order for, well, people to figure out the true code.
I’ve placed the for loop inside of a function after not getting any output from it. But that didn’t help
it’s a 5 digit randomly generated code that is inserted in it as individual numbers.
in this case it’s local therandomcode{2, 6, 0, 8, 9}
but like i’ve said it’s a random code that is determined when the server starts.
on a second thought, i am not entirely sure, it might also be a single string, like local therandomcode{26089}
this is the output i get when i try printing it. but that part of the script that i’ve disabled copies the code from the table
and then just add 1 every time they add in a number and store that into a table as so
local typed = 0
local typedTable = {}
local codeNumbers{the code numbers}
if player typed something then
Table.insert{typedTable, number typed}
typed = typed + 1
if typed == 5 then
local success = 1
for i = 1, 5 do
if codeNumbers[i] == typedTable[i] then
success = success + 1
end
end
if success == 5 then
print("Match")
else
success = 0
typedTable = nil
typed = nil
print("Nope")
end
end
end
i believe i know the issue, the reason why nothing prints is because each value in your therandomcode table is not a number it’s a string so to fix this you would do "1" or tonumber(v). But i have a question why are you using gmatch(".") instead of gmatch("%d") (which only returns numbers/digits).
Okay, i found my mistake in the printing part, It was cause i entered the wrong table name in the for loop. After changing that it printed. But i still don’t see how could i use this method to take these individual numbers out and then turn them into their binary version.
it took me a bit to understand what you were trying to do but now i think i understand (sorry). To convert the code into binary you can do something like this:
local BinaryTable = {}
local code = "15125"
local Binaries = { [00110001] = 1 } -- you can add more
local FormatedCode = {}
for number in code:gmatch("%d") do
for i,v in pairs(Binaries) do
if tostring(v) == number then
print(i) -- this would print 00110001
table.insert(FormatedCode, i) -- insert our binary number
end
end
end
Thank you very much, I am sorry for lacking the ability to explain this properly i should’ve shown the full code and told it properly from the beginning