Search code examples
luaroblox

How to get only some digits in a number value in roblox?


I am still working on the project that I made, the computer PIN cracking thing. And I want to make that when somebody click on a hint button, it will display the 1/4 of the password, the other will remains "*". But I can't even think of something to do!

I think the math element can help me with this, but I can't understand what does what, so I can't write anything out, sorry.


Solution

  • The string methods can help out...

    > function click(pin)
    >> local rnd = math.random(#pin)
    >> local dig = pin:sub(rnd, rnd)
    >> return(pin:gsub('.', function(m) if m == dig then return(m) else return('*') end end))
    >> end
    > pin=1234567890
    > click(tostring(pin))
    *******8**
    > click(tostring(pin))
    *****6****
    > click(tostring(pin))
    ********9*
    > click(tostring(pin))
    ****5*****
    
    1. Pick a random char/digit from pin (dig = pin:sub(rnd, rnd))
    2. Return what match (m == dig) at its position the rest fill out with *