How would i store and remove data for elevator buttons

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)

2 Likes

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.

Edit:
If you don’t have the tag editor plugin, here’s a link:
(1) Tag Editor - Roblox
And here’s a post explaining it:
Tag Editor Plugin - Help and Feedback / Creations Feedback - Developer Forum | Roblox

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.

Edit 2:
Comments in the script

1 Like

i already finished the elevator script, but this is extremely useful and it will definitely help me in the future, thank you.

1 Like

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)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.