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