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

How to make a door disappear when four people step on four buttons?

Asked by 5 years ago

I'm currently trying to create a game (obviously). In the end, I'm trying to add a door that opens/disappears when four people step on each button at the same time. How do you script this?

3 answers

Log in to vote
2
Answered by 5 years ago

One is to use some novel behavior of coroutines. Coroutines are roblox's way of handling threads. tl;dr being that threads allow multiple scripts to run at the same time. By using the coroutine library of roblox we can actually control these threads and start and stop programs. Coroutines before were kinda wild but with a recent change they are much more manageable. We can stop our program until the four touched events of our buttons fire and then continue (maybe continue with the goal of opening a door).

I'm going to re-use the code from the devforum post linked up above. This allows us to wait for multiple events to fire (in this case Touched). We actually want one more thing, we want to detect if players get off our buttons. That means we have four other events to keep track of (TouchEnded).

local function waitForEvents(goodevents, badevents)
    local thread = coroutine.running()
    local listeners = {}
    local c = 0
    for i = 1, #goodevents do
        local listener
        listener = goodevents[i]:Connect(function ()
            listener:Disconnect()
            c = c + 1
            if c == #goodevents then coroutine.resume(thread) end
        end)
        listeners[i] = listener -- Me. We don't want to disconnect the listener
                    -- until we are done with the four button sequence.
    end
    for i = 1, #badevents do
        local listener
        listener = badevents[i]:Connect(function ()
            listener:Disconnect()
            c = c - 1 -- We subtract to get away from the threshold.
        end)
        listeners[i + #goodevents] = listener
    end
    coroutine.yield()
end

local button1, button2, button3, button4

waitForEvents({button1.Touched, button2.Touched, button3.Touched, button4.Touched},
            {button1.TouchEnded, button2.TouchEnded, button3.TouchEnded, button4.TouchEnded})

-- Door opening here or something ...

TouchEnded might be buggy though, but I think coroutines are cool and we should use them more.

The alternative to this is to have Touched events for all the buttons flip some BooleanValue to mark that they are recessed or something (you can then turn these BooleanValues false with the TouchEnded event or a timer). In the Touched event for each button check the boolean values of the other buttons. If the other 3 are true then trigger your door animation.

0
button1, button2, button3, and button4 are all placeholder variables pointing to your buttons. User#18718 0 — 5y
0
Though this has a source, it's still a clever solution. Great answer pidgey 548 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

or make a loop that looks at the bool value of the false and true buttons and when they are all 4 true the do this (open door) if false en close door but make also a boolvalue to see if the door already is open or closed else the loop stays doing the same thing over and over again becourse the othe boolvalue is true or false

like while true do if Button1.value == true and Buttonr2.Value == true and button3.value is true and Button4.value == true and doorstate.value == "closed" then -- do this end

end

on touched make button i . value true

on touchedend make button i . value false

this is just an example how i would think of it

0
Bad answer. Coroutines are superior. DeceptiveCaster 3761 — 5y
Log in to vote
0
Answered by 5 years ago

Well, you can within each of those buttons, write a method which, connected by the Touched event, which changes the value of a boolValue object within the game, this will be our 'global variable'. we can have any number of these depending on how many buttons u got. 5 buttons then 5 of these IntValues.

So for each of the buttons, once touched change the corresponding boolValue Value to true when touched.

on a separate script make a while loop checking the status of all these values, if they're all true do what u gotta do.

I'm new to scripting within Lua so this may not be the most efficient solution but it is certainly simple for me.

0
dont forget to make the door value to check if its already open or closed else the while true do loop will stay doing the same over and over again like move door away 2 nods and again 2 nods but id the door value is already open or close it would stop User#27824 0 — 5y
0
Still wrong. Coroutines should be used. DeceptiveCaster 3761 — 5y

Answer this question