Reducing code for everytime a Clickdetector event fires

I was wondering if there was a better or more efficient way to activate this function.
I have a GUI with 10 buttons. When any 10 of them are activated, they print the name of their object and the block the object belongs to. However, because repeating the same 3 lines 10 times is super redundant, I was wondering if there was a more efficient way to get the the name and block of the object every time it is activated. I am super new to the Dev Forums, and couldn’t find anything related to this, probably because I didn’t search for it correctly or sumthn but any suggestions or help is appreciated.


local function HandleServer()
	SendRequestedServer:FireServer(RequestingServer, Block)
	SendServerData.OnClientEvent:Connect(DistributeData)
	LoadUI()
end

script.Parent.BlockAServers.ServerA1.MouseButton1Click:Connect(function()
	RequestingServer = "ServerA1"
	Block = "BlockA"
	HandleServer()
end)

script.Parent.BlockAServers.ServerA2.MouseButton1Click:Connect(function()
	RequestingServer = "ServerA2"
	Block = "BlockA"
	HandleServer()
end)

script.Parent.BlockAServers.ServerA3.MouseButton1Click:Connect(function()
	RequestingServer = "ServerA3"
	Block = "BlockA"
	HandleServer()
end)

script.Parent.BlockAServers.ServerA4.MouseButton1Click:Connect(function()
	RequestingServer = "ServerA4"
	Block = "BlockA"
	HandleServer()
end)

script.Parent.BlockAServers.ServerA5.MouseButton1Click:Connect(function()
	RequestingServer = "ServerA5"
	Block = "BlockA"
	HandleServer()
end)

script.Parent.BlockAServers.ServerA6.MouseButton1Click:Connect(function()
	RequestingServer = "ServerA6"
	Block = "BlockA"
	HandleServer()
end)

script.Parent.BlockAServers.ServerA7.MouseButton1Click:Connect(function()
	RequestingServer = "ServerA7"
	Block = "BlockA"
	HandleServer()
end)

script.Parent.BlockAServers.ServerA8.MouseButton1Click:Connect(function()
	RequestingServer = "ServerA8"
	Block = "BlockA"
	HandleServer()
end)

script.Parent.BlockAServers.ServerA9.MouseButton1Click:Connect(function()
	RequestingServer = "ServerA9"
	Block = "BlockA"
	HandleServer()
end)

script.Parent.BlockAServers.ServerA10.MouseButton1Click:Connect(function()
	RequestingServer = "ServerA10"
	Block = "BlockA"
	HandleServer()
end)
1 Like

Try this script using for

local function HandleServer()
	SendRequestedServer:FireServer(RequestingServer, Block)
	SendServerData.OnClientEvent:Connect(DistributeData)
	LoadUI()
end

for Index, Buttons in pairs(script.Parent.BlockAServers:GetDescendants()) do
	if Buttons:IsA("TextButton") then
		if Buttons then
			Buttons.MouseButton1Click(function()
				RequestingServer = Buttons.Name
				Block = "BlockA"
				HandleServer()
			end);
		end;
	end;
end;

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