If I were to theoretically make an elevator, and the player were to click all of the buttons inside. How would I get the list of buttons IN ORDER and then take the buttons out when the elevator reaches its floor?
(i’m not trying to make the elevator itself, just the button system)
I’m not entirely sure what you mean, but assuming you want to save a list of buttons in the order that they were pressed, here’s a clean way to achieve it, with an explanation at the end:
-- Collection Service
local CollectionService = game:GetService("CollectionService")
local ButtonTag = CollectionService:GetTagged("Button")
-- Table to store all buttons
local ButtonTable = {}
-- Functions
local function ActivateButton(Button)
local ClickDetector = Button:FindFirstChild("ClickDetector")
local ClickConnection
ClickConnection = ClickDetector.MouseClick:Connect(function(Player)
if not table.find(ButtonTable, Button) then -- Make sure that the button has not already been inserted into the table
table.insert(ButtonTable, Button)
end
print(ButtonTable) -- Just to see the table in the output
ClickConnection:Disconnect() -- If you want to make the button clickable only once (Optional)
end)
end
-- Loop through all tagged instances (buttons)
for _, Button in ipairs(ButtonTag) do
if Button:IsA("Part") and Button:FindFirstChild("ClickDetector") then -- Just to make sure
task.spawn(ActivateButton, Button)
end
end
Basically what you need to do:
Tag every button in the workspace using CollectionService.
Loop through each tagged button
Call the function which connects the ClickDetector event
Insert the button into a table. Make sure that the button is not added multiple times.
This leaves you with a table of pressed buttons, in the order that they were pressed.
I prefer to use CollectionService for stuff like this, but if you don’t want to use the plugin or CollectionService at all, you can just loop through a folder in the workspace containing the buttons, the same way I showed above.
Yeah, it sure is a great way to organize the scripting of interactable objects, especially combined with the use of OOP (definitely not required though)