Tested working in the Studio..
--ModuleScript in ReplicatedStorage.MyModule
local mod = {}
function mod.handle_button(button, actions)
if actions.click then
button.MouseButton1Click:Connect(function()
actions.click(button)
end)
end
if actions.enter then
button.MouseEnter:Connect(function()
actions.enter(button)
end)
end
if actions.leave then
button.MouseLeave:Connect(function()
actions.leave(button)
end)
end
end
function mod.handle_all_buttons(container, actions)
for _, button in ipairs(container:GetChildren()) do
if button:IsA("ImageButton") then
mod.handle_button(button, actions)
end
end
end
return mod
--LocalScript in StarterPlayerScripts
task.wait(3) --just a stall for this test, GUI setup time
local player = game.Players.LocalPlayer
local mod = require(game.ReplicatedStorage:WaitForChild("MyModule"))
local gui = player:WaitForChild("PlayerGui"):WaitForChild("MainUI")
local bar = gui:WaitForChild("BottomBar"):WaitForChild("Container"):WaitForChild("Bar")
mod.handle_all_buttons(bar, {
click = function(button)
print("Clicked:", button.Name)
end,
enter = function(button)
print("Hovered:", button.Name)
end,
leave = function(button)
print("Unhovered:", button.Name)
end
})
A quick testing GUI
--LocalScript in StarterPlayerScripts
local player = game.Players.LocalPlayer
local gui = Instance.new("ScreenGui")
gui.Name = "MainUI"
gui.ResetOnSpawn = false
gui.Parent = player:WaitForChild("PlayerGui")
local bottomBar = Instance.new("Frame")
bottomBar.Name = "BottomBar"
bottomBar.Size = UDim2.new(1, 0, 0, 100)
bottomBar.Position = UDim2.new(0, 0, 1, -100)
bottomBar.BackgroundColor3 = Color3.fromRGB(30, 30, 30)
bottomBar.Parent = gui
local container = Instance.new("Frame")
container.Name = "Container"
container.Size = UDim2.new(1, 0, 1, 0)
container.BackgroundTransparency = 1
container.Parent = bottomBar
local bar = Instance.new("Frame")
bar.Name = "Bar"
bar.Size = UDim2.new(1, 0, 1, 0)
bar.BackgroundTransparency = 1
bar.Parent = container
for i = 1, 3 do
local btn = Instance.new("ImageButton")
btn.Name = "Button" .. i
btn.Size = UDim2.new(0, 100, 0, 60)
btn.Position = UDim2.new(0, 20 + (i - 1) * 120, 0.5, -30)
btn.BackgroundColor3 = Color3.fromRGB(60, 60, 60)
btn.AutoButtonColor = true
btn.Parent = bar
end
This is a subtle Roblox quirk that comes up when connecting UI events from ModuleScripts vs LocalScripts. The key issue is which context the code is running in.
ModGuiTest_HoverClickLeave.rbxl (58.7 KB)