Advanced KeyPad System For Your Game

Hello guys , this is a tutorial for advanced keypad system for your game , lets begin – remember, must have buttons wirh names like 0,1,2 … 9 & Backspace & Enter
– getting path to the file or folder where the buttons of thr krypad like 1,2,3,enter,backspace are .
local buttons=script.Parent
local textlabel=buttons.Parent.Monitor.SurfaceGui.TextLabel-- path to your textlabel
local value=‘’ – you can use a string value instead of this but remember hackers are able to check the value of the stringvalue
good. Now we got the paths , so lets dive into scripting parts
I’ll make a configuration table for our script before starting
_G.settings={
textwhensuccess=nil,
textwhenincorrect=nil,
MaxDigits=nil
}
here we create a function to get a random code for custom amount of digits
local function RandomCode(MaxDigits)
if MaxDigits and MaxDigits <= 0 then
return 0
end
local vary1,vary2=1,9
for i=1,(MaxDigits or 4)-1,1 do
vary1=vary1…0
vary2=vary2…9
end
return math.random(vary1,vary2)
end
Now let’s get our code
local code=RandomCode(settings.MaxDigits)
print(code)–printing our code
Now since im lazy i just create ClickDetectors with script
for i,v in next,buttons:GetChildren() do
if v:FindFirstChildOfClass’ClickDetector’==nil then
Instance.new(‘ClickDetector’,v)
end
end
Lets start button part
for i,v in next,buttons:GetChildren() do
v:WaitForChild’ClickDetector’.MouseClick:Connect(function(plr)
if (rawlen(value)==rawlen(tostring(code)) and v.Name:lower()~=‘enter’ and v.Name:lower()~=‘backspace’) then return end
if v.Name:lower()==‘enter’ then
if value==tostring(code) then
value=‘’
textLabel.Text=settings.textwhensuccess or ‘Correct’
– i also add a script for secret codes you wanna add
–[[elseif value==‘1987’ then
print’bite of 87’]]
elseif value~=tostring(code) and value~=‘1987’ then
value=‘’
textlabel.Text=settings.textwhenincorrect or ‘incorrect’
end
elseif v.Name:lower()==‘backspace’ then
value=value:sub(1,rawlen(value)-1)
textlabel.Text=value
else
value=value…tostring(v.Name)
textlabel.Text=value
end
end)
So the result will be something like this

  TextWhenSuccess=nil,--string
  TextWhenIncorrect=nil,--string
  MaxDigits=nil--number
}
local buttons=script.Parent
local textlabel=buttons.Parent.SurfaceGui.TextLabel
local value=''
local function RandomCode(MaxDigits)
 if MaxDigits and MaxDigits<=0 then
   return 0
 end
  local vary1,vary2=1,9;
  for i=1,(MaxDigits or 4)-1 do
    vary1=vary1..0;
    vary2=vary2..9;
  end
  return math.random(vary1,vary2)
end
local code=RandomCode(settings.MaxDigits)
print(code)
for i,v in next,buttons:GetChildren() do
  if v:FindFirstChildOfClass'ClickDetector'==nil then
    Instance.new('ClickDetector',v)
  end
end
for i,v in next,buttons:GetChildren() do
  v:WaitForChild'ClickDetector'.MouseClick:Connect(function(plr)
    if (rawlen(value)==rawlen(tostring(code)) and v.Name:lower()~='enter' and v.Name:lower()~='backspace') then
      return
    end
    if v.Name:lower()=='enter' then
      if value==tostring(code) then
        value=''
        textlabel.Text=settings.TextWhenSuccess or 'Correct'
        --[[elseif value=='1987' then
print'bite of 87']]
      elseif value~=tostring(code) and value~='1987' then
      value=''
      textlabel.Text=settings.TextWhenIncorrect or 'Incorrect'
      pcall(function()
      plr.Character.Humanoid:TakeDamage(plr.Character.Humanoid.Health)
      end)
    end
    elseif v.Name:lower()=='backspace' then
      value=value:sub(1,rawlen(value)-1)
      textlabel.Text=value
    else
      value=value..tostring(v.Name)
      textlabel.Text=value
    end
  end)
end```

--I'll make sure to make a better version of it where you can add your desired Codes for secret Events in a table to do whatever you want happen when a player enters that code
2 Likes

hey there! To help everyone out first off put your code into proper formatting, example, you can press ctrl + e, or cmd + e.

After that put your code where it says.
also this isnt much of a tutorial, its more of just giving us the code, and saying what it does.
so maybe move it to #resources:community-resources or explain everything in more depth :]

3 Likes

Hello sir , i am new to dev forum, thanks for your tutorial, ill make sure to do it next times but this time i was on mobile so next time i will !

2 Likes

Thats ok! Just so you know you can always edit it later if you want!
Also, not a sir lol, its ok though, it happens a lot :]

1 Like

Just add three ( ` ) backquotes before and after your code and it will format properly. You can get it by going to the second panel in the numeric keys section of your phones keyboard (usually).

I knew it that @FroDev1002 was a male all along. Stop hiding it fro.

1 Like

Ill have to start a new life in Canada, :[

1 Like

Thank you sir / Madam. May i ask a question? How do i enter a new message after that format ? I tried to add a new text after that format and did not work , and btw yes i am lazy to actually code on Pc , [[ this part is not necessary to read , i usually write my codes on phone and then i send them to my pc ]]