Hello. I would like to know how to create buttons in which you must press them in a certain order to activate a line of code in a certain script. I'm not very good at scripting myself, but I well know it will involve BoolValues. I would like the effect to take place clientside, so I know we need a local script. Of course, click detectors as well. I can create a basic clicking script that makes a line of code activate when you click a button, but I need help creating buttons that must be clicked in a certain order.
There are many different possible solutions for combination locks, and mine is only one of those many. While I do believe there are better solutions out there than mine, I'm pretty happy with what I have, and hopefully you are too.
My combination lock involves comparing two string values (they could also be numbers, but I guess it doesn't really matter) after the length of the entry string is equal to the length of the combination string. Here's what I've come up with:
-- The combination of buttons pressed in order (each digit representing a button in that index) local Combination = "2468" -- entry records the characters the user inputs -- buttons is an array of button objects local entry = {} local buttons = {} -- You could insert them manually if you're in a more dynamic situation, or you could use GetChildren on the parent of all the buttons. It really depends, but index order matters. buttons[1] = button1 buttons[2] = button2 buttons[3] = button3 -- Some constants for unnecessary optimization local comboLen = #Combination local concat = table.concat -- This function maps to each button's click event local function buttonEvent(num) entry[#entry+1] = num -- New character entry -- Compare length of entry with combination length if #entry == comboLen then -- If it's equal, then compare their value and see if they're equivalent if concat(entry) == Combination then -- If they are, the combination was completed in order print("Combination cracked") else -- If they're not, it wasn't. print("Invalid combination") end -- Clear the entry list for i = 1, comboLen do entry[i] = nil end end end -- Traverse the buttons, and connect our buttonEvent function to a click event for i = 1, #buttons do local button = buttons[i] -- Individual button -- Use buttonEvent in a callback sort of way button.MouseButton1Click:Connect(function() -- Notice how we're passing "i", the index to the function buttonEvent(i) end end
It may seem a little complicated, but it's actually pretty simple. Each time we click the button, we push a new digit on the end of our entry
table. We then check if both length and value are equal to the Combination
to determine whether or not it's valid. In both cases, when the length of our entry is equal to the length of the combination, we reset the entry table because their combination was wrong.
In case you're wondering why I'm using table.concat
instead of ..
for concatenation, it's just a small optimization feature. Lua stores all created string values in memory which makes for quick retrieval, but somewhat slower construction. This way we're only creating strings 1-9
, and just reusing what's in memory. If any of that doesn't make sense to you, you can just change it to ..
, it really doesn't matter in this situation.
Just for fun, I made a quick implementation of the code above to prove it works, which you can check out here. If you have any further questions, just leave a comment and I'll get back to you as soon as possible.
Well since there is no code you could do something like 4 GUI buttons and inside of them add a bool value named code or something.
Then in your script just add checks with if statements. Say the click code is 1, 3, 2, 4. If button1 is clicked set code value to true because thats the first check in the code.
If button2 is clicked but button3's value isn't true then set all the values back to false because 3 is second number in the code, not 2.
Then keep checking until the player clicks 1 then 3 then 2 then 4 in order. Then have a check if all code values = true do something.
--Set up GUI vars --Code = 1, 3, 2, 4 b1 = script.Parent.Button1 --Button1 b2 = script.Parent.Button2 --Button2 b3 = script.Parent.Button3 --Button3 b4 = script.Parent.Button4 --Button4 code1 = b1.Code--Fist check --[[ Rest of the checks ]] b1.MouseButton1Click:Connect(function() code1.Value = true end) --b2 clicked if code1 == true and code 3 == true then code2.Value = true else code1.Value = false --ect print("Pass reset") end --b3 clicked if code1 == true then code3 = true end --And so on --You can make a for loop to check all of them if they all == true then --unlock end