Help with finding a button from a module script

I am making simple topbar and I need someone to be able to find the button like:

button.MouseButtonOneClicked

but the button gets created with a module script:

local module = {}

function module:newTopbar(name, image, width)
	local newButton = script.Buttons.Template:Clone()
	newButton.Name = name
	local newsize = Vector2.new(width, 32)
	newButton.UISizeConstraint.MinSize = newsize
	newButton.Icon.Image = image
	for _, player in pairs(game:GetService("Players"):GetPlayers()) do
		local Button = newButton:Clone() 
		Button.Parent = player.PlayerGui:WaitForChild("TopBarUI").Bar
	end
end

return module

So how would i be able to find the button with something like, button. in a server script after creating the button?

I’m not really sure what you are trying to say. If the button is created in a Local Script, the server cannot find the button anywhere. Regardless, I think I might know what you are trying to accomplish. Apologies if I am wrong. Anyways;

You can store a reference to the button in a variable in the module table and then access it from the server script like this:

local module = {}

function module:newTopbar(name, image, width)
	local newButton = script.Buttons.Template:Clone()
	newButton.Name = name
	local newsize = Vector2.new(width, 32)
	newButton.UISizeConstraint.MinSize = newsize
	newButton.Icon.Image = image
	for _, player in pairs(game:GetService("Players"):GetPlayers()) do
		local Button = newButton:Clone() 
		Button.Parent = player.PlayerGui:WaitForChild("TopBarUI").Bar
		module.button = Button  -- store a reference to the button
	end
end

return module

Then, in your server script, you can access the button like this:

local topbarModule = require(script.TopbarModule)
local button = topbarModule.button

button.MouseButtonOneClicked:Connect(function()
	-- handle the click event here
end)

Alternatively, you can store the buttons in a table in the module table and access them by index:

local module = {}
module.buttons = {}  -- create an empty table to store the buttons

function module:newTopbar(name, image, width)
	local newButton = script.Buttons.Template:Clone()
	newButton.Name = name
	local newsize = Vector2.new(width, 32)
	newButton.UISizeConstraint.MinSize = newsize
	newButton.Icon.Image = image
	for _, player in pairs(game:GetService("Players"):GetPlayers()) do
		local Button = newButton:Clone() 
		Button.Parent = player.PlayerGui:WaitForChild("TopBarUI").Bar
		table.insert(module.buttons, Button)  -- add the button to the table
	end
end

return module

Then, in your server script, you can access the buttons like this:

local topbarModule = require(script.TopbarModule)
local buttons = topbarModule.buttons

for i, button in ipairs(buttons) do
	button.MouseButtonOneClicked:Connect(function()
		-- handle the click event here
	end)
end
1 Like

is there any way to do like

module.button.menu -- or like the name of the button?

In what context would you use that code? If you are talking about while you are saving the button, the name should already be included with the button.

I just need a way in one line to find a specific name in the table, for example lets say we save a button called “menu” would there be a way to find that button from the table in one line/go like table.1 or table.2

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