Possibility to use 1 script for multiple buttons detections

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I’m not sure if this is possible, but is it possible to use 1 script to detect all button presses without stacking multiple functions

  2. What is the issue? Include screenshots / videos if possible!
    I don’t want to clutter the script/add 1 script per button, because there are so many, so I thought of this possibility

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    No one else seemed to have this question, so I decided to make my own

local player = game.Players.LocalPlayer
local material = script.Parent
local makeStation = nil
local properties = script.Parent
local partSelected = properties.partSelected

local function findMakeStation()
	for _, v in ipairs(workspace:GetChildren()) do
		if v:IsA("Model") and v.Name == "makeStation" and v:FindFirstChild(player.Name) then
			makeStation = v
		end
	end
end

material.Activated:Connect(function()
	findMakeStation()
	
	local part = makeStation.partFolder:FindFirstChild(partSelected.Value)
	makeStation.partFolder.propertyChanger.Material:FireServer(part, script.Parent.Text)
end)

This is the script, and instead of saying “script.Parent.Activated”, it says “[All buttons here].Activated”.
Is this possible?

I’m not sure if I understand your question, but if you want initialize multiple buttons one shot you can use loop.

-- Variables
local folder = script.Parent -- Folder with all Buttons

-- Functions
local function OnClick()
	print("Clicked")
end

-- Initialize Buttons
for _, instance in pairs(folder:GetChildren()) do
	instance.Activated:Connect(OnClick)
end

You can ask me your questions if you have any.

1 Like

Oh wow, I didn’t know it was so simple, thanks!

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