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