Trying to make a system for hero selection and wanted to get some feedback on my approach it’s probably basic, but seems like it’s effective and a bit better than my previous version which relied on button names while this new approach uses IDs.
-- CLIENT
--- Create a new button object (i.e: connect mouse event handlers to a button)
-- @param buttonInstance The button object to create.
-- @param[opt] heroId ID of the hero to select when the button is clicked.
local function createButton(buttonInstance, heroId)
if not buttonInstance then
return
end
if heroId then
local buttonObject = HeroSelectButton.new(buttonInstance, heroId)
else
-- Create a regular button
local buttonObject = Button.new(buttonInstance)
end
end
function UI.initialize()
-- Setup every button in the game on start
for _,buttonInstance in ipairs(screenGui:GetDescendants()) do
if buttonInstance:IsA("TextButton") or buttonInstance:IsA("ImageButton") then
local heroId = buttonInstance:GetAttribute("HeroId")
if heroId then
createButton(buttonInstance, heroId)
else
createButton(buttonInstance)
end
end
end
end
-- CLIENT
function HeroSelectButton.new(buttonInstance, heroId)
local self = Button.new(buttonInstance)
local button = buttonInstance
local heroId = heroId
function self:onMouseButton1Click()
print("HeroSelectButton:onMouseButton1Click")
onHeroSelectButtonClickRE:FireServer(heroId)
end
return self
end
-- SERVER
local heroIdMapping = {
[10001] = "TestHero"
}
local function onHeroSelectButtonClick(player, heroId)
local hero = heroIdMapping[heroId]
if not hero then
warn("Invalid hero ID: "..heroId)
end
end
onHeroSelectButtonClickRE.OnServerEvent:Connect(onHeroSelectButtonClick)