Creating a Prompt Class (Advanced)

I am not good at introductions so let's dive straight into the action.

Here is the structure under the class we are writing. It will be in a Module script.


Here is the Prompt class, paste it into the Prompt Module script ->


local Prompt = {}
Prompt.__index = Prompt
--[[ promptMessage, leftButtonText, rightButtonText, leftButtonTextPressed(), rightButtonTextPressed()]]
function Prompt.new(message, leftButtonText, rightButtonText, leftButtonTextPressed, rightButtonTextPressed)
local self = setmetatable({
ui = script.PromptUI:Clone()
}, Prompt)
self.ui.Parent = game.Players.LocalPlayer.PlayerGui
self.leftButton = self.ui.PromptFrame.LeftButton
self.rightButton = self.ui.PromptFrame.RightButton
self.message = self.ui.PromptFrame.Message
if message then
self.message.Text = message
else
self.message.Text = "Error, take screenshot of logs and send to dev."
end
if rightButtonText then
self.rightButton.Text = rightButtonText
self.rightButton.MouseButton1Down:Connect(function()
rightButtonTextPressed()
self:Destroy()
end)
else
self.rightButton.Text = ""
end
if leftButtonText then
self.leftButton.Text = leftButtonText
self.leftButton.MouseButton1Down:Connect(function()
leftButtonTextPressed()
self:Destroy()
end)
else
self.leftButton.Text = ""
end
self.ui.Enabled = true
return self
end
function Prompt:Destroy()
self.ui:Destroy()
end
return Prompt

Breakdown: function Prompt.new() takes a message, left button text, right button text, function for when left button is pressed, and a function for when right button is pressed. Yes, you can send functions in as arguments in Lua. Use the :Connect syntax to connect the function that is sent in to the event that the button is pressed.


Now how do we use it?

print("test")
local promptClass = require(script.Prompt)
wait(4)
local prompt = promptClass.new("Are you advanced?",
"NO", "YES",
function()print("left was pressed")end,
function()print("right was pressed")end)

Copy and paste the Prompt Module script into a local script, then paste the above code into the local script. Run in Roblox Studio, and study it for yourself. The best tutorials make you do the work.


All of the best and greatest developers simply never give up. I hope you learn from this tutorial!