Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Pressing buttons in order to enable a line of code?

Asked by 7 years ago

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.

2 answers

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

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.

String comparison

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:

01-- The combination of buttons pressed in order (each digit representing a button in that index)
02local Combination = "2468"
03 
04-- entry records the characters the user inputs
05-- buttons is an array of button objects
06local entry = {}
07local buttons = {}
08 
09-- 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.
10buttons[1] = button1
11buttons[2] = button2
12buttons[3] = button3
13 
14-- Some constants for unnecessary optimization
15local comboLen = #Combination
View all 48 lines...

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.

Concatenation

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.

Help & Questions

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.

0
Would this work client-side? I really need this to work clientside, as an example, other players can't mess up another player entering the password, or, the player entering the password gets a client-side movement of a brick. Loleydude 29 — 7y
0
My bad, I didn't notice it was a Gui. Loleydude 29 — 7y
0
This doesn't have anything to do with network communication, so yes. The buttons in this example represent TextButton objects, which should also always be handled by the client. There'd be no reason for the server to handle any of this input. ScriptGuider 5640 — 7y
0
However, i'd like the buttons to be actual buttons inside of the game itself. Like, bricks that you click. Loleydude 29 — 7y
View all comments (8 more)
0
Yeah, the same logic applies. You'd just be connecting the same function to whatever click event of the button you're using. ScriptGuider 5640 — 7y
0
Sorry if I happened to ruin your entire script. Loleydude 29 — 7y
0
Well... Would the buttons still be clientside? Loleydude 29 — 7y
0
You could event simulate this without buttons. The buttons are only necessary to know what you're inputting. If you said: buttonEvent(2); buttonEvent(4); buttonEvent(6); buttonEvent(8) - that would work just as well. ScriptGuider 5640 — 7y
0
Yeah, the input to them would be handled locally. ScriptGuider 5640 — 7y
0
Oh, okay. Thanks! Loleydude 29 — 7y
0
I hope you don't mind.. But I need help on my other script. It's extremely simple, I just need to find out whats wrong. Apparently i'm calling a function and then testing. https://scriptinghelpers.org/questions/43526/can-someone-please-help-me-find-out-whats-wrong-with-it-everything-looks-fine-to-me Loleydude 29 — 7y
0
Also, it says "Expected ) to close ( at like 44 Loleydude 29 — 7y
Ad
Log in to vote
-1
Answered by 7 years ago
Edited 7 years ago

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.

01--Set up GUI vars
02 
03--Code = 1, 3, 2, 4
04 
05b1 = script.Parent.Button1  --Button1
06b2 = script.Parent.Button2 --Button2
07b3 = script.Parent.Button3 --Button3
08b4 = script.Parent.Button4 --Button4
09 
10code1 = b1.Code--Fist check
11--[[
12Rest of the checks
13]]
14 
15b1.MouseButton1Click:Connect(function()
View all 37 lines...

Answer this question