Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- NotificationHandler Module
- Manages in-game notifications with different styles (Info, Warning, Error)
- ]]
- local NotificationHandler = {}
- -- Services
- local TweenService = game:GetService("TweenService")
- local Players = game:GetService("Players")
- local TextService = game:GetService("TextService")
- -- Constants
- local MAX_NOTIFICATIONS = 8
- local NOTIFICATION_ICONS = {
- INFO = "6026568227",
- WARN = "6031071053",
- ERROR = "6031071057"
- }
- local NOTIFICATION_COLORS = {
- INFO = Color3.fromRGB(100, 200, 255),
- WARN = Color3.fromRGB(255, 200, 0),
- ERROR = Color3.fromRGB(255, 100, 100)
- }
- -- References
- local LocalPlayer = Players.LocalPlayer
- local PlayerGui = LocalPlayer:WaitForChild("PlayerGui")
- local NotificationGui = PlayerGui:WaitForChild("NoficationGui")
- local Canvas = NotificationGui:WaitForChild("Canvas")
- -- State
- local ActiveNotifications = {}
- -- Types
- type NotificationConfig = {
- message: string,
- duration: number,
- iconId: string,
- color: Color3
- }
- -- Private Functions
- local function createUIComponents()
- local components = {}
- components.Frame = Instance.new("Frame")
- components.Frame.Size = UDim2.new(0, 200, 0, 50)
- components.Frame.Position = UDim2.new(0.5, -100, 0.8, 0)
- components.Frame.BackgroundColor3 = Color3.fromRGB(40, 40, 40)
- components.Frame.BackgroundTransparency = 0.1
- components.Frame.ClipsDescendants = true
- components.Corner = Instance.new("UICorner")
- components.Corner.CornerRadius = UDim.new(0, 8)
- components.Corner.Parent = components.Frame
- components.Stroke = Instance.new("UIStroke")
- components.Stroke.ApplyStrokeMode = Enum.ApplyStrokeMode.Border
- components.Stroke.LineJoinMode = Enum.LineJoinMode.Round
- components.Stroke.Thickness = 2
- components.Stroke.Transparency = 0.2
- components.Stroke.Parent = components.Frame
- components.Icon = Instance.new("ImageLabel")
- components.Icon.BackgroundTransparency = 1
- components.Icon.Size = UDim2.new(0, 40, 0, 40)
- components.Icon.AnchorPoint = Vector2.new(0, 0.5)
- components.Icon.Position = UDim2.new(0, 5, 0.5, 0)
- components.Icon.Parent = components.Frame
- local aspectRatio = Instance.new("UIAspectRatioConstraint")
- aspectRatio.AspectRatio = 1
- aspectRatio.AspectType = Enum.AspectType.FitWithinMaxSize
- aspectRatio.DominantAxis = Enum.DominantAxis.Height
- aspectRatio.Parent = components.Icon
- components.Text = Instance.new("TextLabel")
- components.Text.BackgroundTransparency = 1
- components.Text.Position = UDim2.new(0, 50, 0, 0)
- components.Text.Size = UDim2.new(1, -60, 1, 0)
- components.Text.Font = Enum.Font.SourceSansBold
- components.Text.TextColor3 = Color3.fromRGB(255, 255, 255)
- components.Text.TextSize = 25
- components.Text.TextXAlignment = Enum.TextXAlignment.Left
- components.Text.ClipsDescendants = true
- components.Text.TextWrapped = false
- components.Text.Parent = components.Frame
- return components
- end
- local function createTweens(frame: Frame, textSize: Vector2)
- local appearTween = TweenService:Create(frame,
- TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.Out),
- {
- Size = UDim2.new(0, textSize.X + 60, 0, 50),
- Position = UDim2.new(0.5, -(textSize.X + 60)/2, 0.8, 0)
- }
- )
- local disappearTween = TweenService:Create(frame,
- TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.In),
- {
- Size = UDim2.new(0, 0, 0, 50),
- Position = UDim2.new(0.5, 0, 0.8, 0)
- }
- )
- return appearTween, disappearTween
- end
- local function removeOldestNotification()
- if #ActiveNotifications >= MAX_NOTIFICATIONS then
- local oldestNotification = table.remove(ActiveNotifications, 1)
- oldestNotification:Destroy()
- end
- end
- local function removeNotification(notification: Frame)
- for i, activeNotification in ipairs(ActiveNotifications) do
- if activeNotification == notification then
- table.remove(ActiveNotifications, i)
- break
- end
- end
- notification:Destroy()
- end
- local function createBaseNotification(config: NotificationConfig)
- local function notificationProcess()
- removeOldestNotification()
- local components = createUIComponents()
- local notification = components.Frame
- notification.Parent = Canvas
- table.insert(ActiveNotifications, notification)
- components.Stroke.Color = config.color
- components.Icon.Image = "rbxassetid://" .. config.iconId
- components.Icon.ImageColor3 = config.color
- components.Text.Text = config.message
- local textSize = TextService:GetTextSize(
- config.message,
- 25,
- Enum.Font.SourceSansBold,
- Vector2.new(400, 50)
- )
- local appearTween, disappearTween = createTweens(notification, textSize)
- appearTween:Play()
- task.wait(config.duration)
- disappearTween:Play()
- task.wait(0.5)
- removeNotification(notification)
- end
- task.spawn(notificationProcess)
- end
- -- Public Methods
- function NotificationHandler.CreateInfo(message: string, duration: number)
- createBaseNotification({
- message = message,
- duration = duration,
- iconId = NOTIFICATION_ICONS.INFO,
- color = NOTIFICATION_COLORS.INFO
- })
- end
- function NotificationHandler.CreateWarn(message: string, duration: number)
- createBaseNotification({
- message = message,
- duration = duration,
- iconId = NOTIFICATION_ICONS.WARN,
- color = NOTIFICATION_COLORS.WARN
- })
- end
- function NotificationHandler.CreateError(message: string, duration: number)
- createBaseNotification({
- message = message,
- duration = duration,
- iconId = NOTIFICATION_ICONS.ERROR,
- color = NOTIFICATION_COLORS.ERROR
- })
- end
- return NotificationHandler
Advertisement
Add Comment
Please, Sign In to add comment