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