InterfaceService - Scripting GUI Interactions is now easier

InterfaceService

Documentation | Module


InterfaceService is a stand-alone ModuleScript that helps you script interactions with GUI Instances in a better, more efficient way!

You can detect an interaction (such as left click) on multiple GUI Instances which is achievable without InterfaceService but it requires you to write out a for loop for every GUI Interaction you want, and if your script creates a new GUI Instance after the for loop then the Instance would be omitted from the loop as it did not exist at the time the loop was executed. InterfaceService aims to solve these problems.

Example

local interfaceService = require(game.ReplicatedStorage:WaitForChild("InterfaceService"))

-- function that fires whenever a GUI Instance with the ClassName of "TextButton" is left clicked
local leftClick = interfaceService:Get(".TextButton"):LeftClick(function(this)
    print(this.Name.." was clicked!")
end)

-- creates a new TextButton
newButton = interfaceService:Create({
    ClassName = "TextButton",
    Size = UDim2.new(0, 100, 0, 100),
    Position = UDim2.new(0.5, 0, 0.5, 0),
    AnchorPoint = Vector2.new(0.5, 0.5),
    Parent = script.Parent
})


-- has to update the leftClick function so "newButton" can be detected
leftClick:Update()
10 Likes

You should definitely check out the docs if you are interested in this module - it literally has all the info you need. Also please check the docs before answering any questions!
image

Gonna be honest. Roblox API is already basic as your’s

local TextButtonParent = script.Parent

-- function that fires whenever a GUI Instance with the ClassName of "TextButton" is left clicked (You can detect it too with for loops) 
local leftClick = interfaceService.TextButton.MouseButton1Down:Connect(function() 
    print(this.Name.." was clicked!")
end)

-- creates a new TextButton
newButton = Instance.new("TextButton", game.StarterGui) 
    
    NewButton.Size = UDim2.new(0, 100, 0, 100),
    NewButton.Position = UDim2.new(0.5, 0, 0.5, 0),
    NewButton.AnchorPoint = Vector2.new(0.5, 0.5),
    NewButton.Parent = script.Parent


-- has to update the leftClick function so "newButton" can be detected
NewButton.MouseButton1Down:Connect()

I wrote all the code with roblox’s API. You can compare it to each other.

2 Likes

Roblox’s api only allows you to specify one instance whilst my api allows you to specify more than one instance

as I said in the comments, you can do the same with for loops like

for i,v in pairs(game.StarterGui:GetDescendants())
if v:IsA("TextButton") then
v.MouseButton1Down:Connect(function()
print("A unnamed TextButton were clicked!") 
end)
end
end

Well what if you created a new TextButton after you defined the loop, and you wanted that TextButton in the loop.

1 Like
for i,v in pairs(pcall(game:GetDescendants()))
if v:IsA("TextButton") and v.Name == "Name" then
print("(Buttons in the same name and same ClassNames I wanted were clicked!)" 
end
end

Challenge accepted. (That were the best I can do it scans whole game and finds the instance you wanted)

2 Likes

I agree that your module helps GUI scripting but it doesn’t matters much with Roblox’s API. You could’ve add more commands to help people scripting UIs much more efficiently

1 Like
for i,v in pairs(game.StarterGui:GetDescendants())
    if v:IsA("TextButton") then
        v.MouseButton1Down:Connect(function()
             print("A unnamed TextButton were clicked!") 
        end)
    end
end

Instance.new("TextButton")

-- this new TextButton won't be detected by the for loop above
1 Like

ok you won lol.

your module is great but maybe have some more functions like DrawLine(X1,Y1,X2,Y2) or more. You can turn this into a UI library more than a module created to help beginner scripters

4 Likes

make a ui lib would be more useful for new scripters -_-

1 Like

could you elaborate about what you mean by that?

Not hard to put the loop inside of a function and call it everytime you need it to update (might as well keep all the connections in a table too so you can disconnect them easily)

function TextButtonHandler(v)
    if v:IsA("TextButton") then
        v.MouseButton1Down:Connect(function()
             print("A unnamed TextButton were clicked!") 
        end)
    end
end
for i, v in ipairs(game:GetService("StarterGui"):GetDescendants())
    TextButtonHandler(v)
end
game:GetService("StarterGui").DescendantAdded:Connect(TextButtonHandler)

Instance.new("TextButton", game:GetService("StarterGui")
2 Likes

Why should one use this over Roact?

1 Like

ok, so I am not too familiar with Roact but looking at their documentation it doesn’t seem like you can detect, for example, MouseButton1Click on multiple elements using the same function. Although feel free to correct me when im wrong.

so you would rather repeatedly write out that code, instead of using a module script that can save your time and reduce the amount of lines your script has thus making it cleaner and easier to read?

Honestly, I don’t really see a use case for when any text button on the screen is clicked. It doesn’t even take into account surface gui or billboard gui clicks (although storing them in something other than StarterGui is pretty bad practice).
If you write it out like I did, you can accustom it to your liking more easily. You don’t have to use a text button, it could be anything - an event for every sound in the game that is playing, or even an event for every part in the workspace that is set to anchored.
I would rather write out this code than have a module script I have to require and this arcane line of text:
local leftClick = interfaceService:Get(".TextButton"):LeftClick(function(this)

Also, you appear to be living in the 1970’s because copy and paste was invented in 1973. You don’t have to write it out every time, and if you really wanted to you could make a function for it.

2 Likes

The point of Roact is that you can split your project up into individual, reusable, components. That helps you to write code once and reuse it wherever it’s needed; e.g. a TextButton with custom styling would be one use case.

This project seems more akin to basic JavaScript than a full fledged “virtual DOM” type framework like Roact.

cc. @zamd157

5 Likes
for i,v in pairs(game.StarterGui:GetDescendants())
    if v:IsA("TextButton") then
        v.MouseButton1Down:Connect(function()
             print("A unnamed TextButton were clicked!") 
        end)
    end
end

YourGui.ChildAdded:Connect(function(v) -- ?? I think childadded works in a localscript?
    if v:IsA("TextButton") then
        v.MouseButton1Down:Connect(function()
             print("A unnamed TextButton were clicked!") 
        end)
    end
end)

Instance.new("TextButton")

This should do the exact same for every textbutton added AFTER the for loop.