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:

-- 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.

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.

--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

Answer this question