Guest User

Notification Handler

a guest
Feb 1st, 2025
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.49 KB | Source Code | 0 0
  1. --[[
  2.     NotificationHandler Module
  3.     Manages in-game notifications with different styles (Info, Warning, Error)
  4. ]]
  5.  
  6. local NotificationHandler = {}
  7.  
  8. -- Services
  9. local TweenService = game:GetService("TweenService")
  10. local Players = game:GetService("Players")
  11. local TextService = game:GetService("TextService")
  12.  
  13. -- Constants
  14. local MAX_NOTIFICATIONS = 8
  15. local NOTIFICATION_ICONS = {
  16.     INFO = "6026568227",
  17.     WARN = "6031071053",
  18.     ERROR = "6031071057"
  19. }
  20. local NOTIFICATION_COLORS = {
  21.     INFO = Color3.fromRGB(100, 200, 255),
  22.     WARN = Color3.fromRGB(255, 200, 0),
  23.     ERROR = Color3.fromRGB(255, 100, 100)
  24. }
  25.  
  26. -- References
  27. local LocalPlayer = Players.LocalPlayer
  28. local PlayerGui = LocalPlayer:WaitForChild("PlayerGui")
  29. local NotificationGui = PlayerGui:WaitForChild("NoficationGui")
  30. local Canvas = NotificationGui:WaitForChild("Canvas")
  31.  
  32. -- State
  33. local ActiveNotifications = {}
  34.  
  35. -- Types
  36. type NotificationConfig = {
  37.     message: string,
  38.     duration: number,
  39.     iconId: string,
  40.     color: Color3
  41. }
  42.  
  43. -- Private Functions
  44. local function createUIComponents()
  45.     local components = {}
  46.  
  47.     components.Frame = Instance.new("Frame")
  48.     components.Frame.Size = UDim2.new(0, 200, 0, 50)
  49.     components.Frame.Position = UDim2.new(0.5, -100, 0.8, 0)
  50.     components.Frame.BackgroundColor3 = Color3.fromRGB(40, 40, 40)
  51.     components.Frame.BackgroundTransparency = 0.1
  52.     components.Frame.ClipsDescendants = true
  53.  
  54.     components.Corner = Instance.new("UICorner")
  55.     components.Corner.CornerRadius = UDim.new(0, 8)
  56.     components.Corner.Parent = components.Frame
  57.  
  58.     components.Stroke = Instance.new("UIStroke")
  59.     components.Stroke.ApplyStrokeMode = Enum.ApplyStrokeMode.Border
  60.     components.Stroke.LineJoinMode = Enum.LineJoinMode.Round
  61.     components.Stroke.Thickness = 2
  62.     components.Stroke.Transparency = 0.2
  63.     components.Stroke.Parent = components.Frame
  64.  
  65.     components.Icon = Instance.new("ImageLabel")
  66.     components.Icon.BackgroundTransparency = 1
  67.     components.Icon.Size = UDim2.new(0, 40, 0, 40)
  68.     components.Icon.AnchorPoint = Vector2.new(0, 0.5)
  69.     components.Icon.Position = UDim2.new(0, 5, 0.5, 0)
  70.     components.Icon.Parent = components.Frame
  71.  
  72.     local aspectRatio = Instance.new("UIAspectRatioConstraint")
  73.     aspectRatio.AspectRatio = 1
  74.     aspectRatio.AspectType = Enum.AspectType.FitWithinMaxSize
  75.     aspectRatio.DominantAxis = Enum.DominantAxis.Height
  76.     aspectRatio.Parent = components.Icon
  77.  
  78.     components.Text = Instance.new("TextLabel")
  79.     components.Text.BackgroundTransparency = 1
  80.     components.Text.Position = UDim2.new(0, 50, 0, 0)
  81.     components.Text.Size = UDim2.new(1, -60, 1, 0)
  82.     components.Text.Font = Enum.Font.SourceSansBold
  83.     components.Text.TextColor3 = Color3.fromRGB(255, 255, 255)
  84.     components.Text.TextSize = 25
  85.     components.Text.TextXAlignment = Enum.TextXAlignment.Left
  86.     components.Text.ClipsDescendants = true
  87.     components.Text.TextWrapped = false
  88.     components.Text.Parent = components.Frame
  89.  
  90.     return components
  91. end
  92.  
  93. local function createTweens(frame: Frame, textSize: Vector2)
  94.     local appearTween = TweenService:Create(frame,
  95.         TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.Out),
  96.         {
  97.             Size = UDim2.new(0, textSize.X + 60, 0, 50),
  98.             Position = UDim2.new(0.5, -(textSize.X + 60)/2, 0.8, 0)
  99.         }
  100.     )
  101.  
  102.     local disappearTween = TweenService:Create(frame,
  103.         TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.In),
  104.         {
  105.             Size = UDim2.new(0, 0, 0, 50),
  106.             Position = UDim2.new(0.5, 0, 0.8, 0)
  107.         }
  108.     )
  109.  
  110.     return appearTween, disappearTween
  111. end
  112.  
  113. local function removeOldestNotification()
  114.     if #ActiveNotifications >= MAX_NOTIFICATIONS then
  115.         local oldestNotification = table.remove(ActiveNotifications, 1)
  116.         oldestNotification:Destroy()
  117.     end
  118. end
  119.  
  120. local function removeNotification(notification: Frame)
  121.     for i, activeNotification in ipairs(ActiveNotifications) do
  122.         if activeNotification == notification then
  123.             table.remove(ActiveNotifications, i)
  124.             break
  125.         end
  126.     end
  127.     notification:Destroy()
  128. end
  129.  
  130. local function createBaseNotification(config: NotificationConfig)
  131.     local function notificationProcess()
  132.         removeOldestNotification()
  133.  
  134.         local components = createUIComponents()
  135.         local notification = components.Frame
  136.         notification.Parent = Canvas
  137.  
  138.         table.insert(ActiveNotifications, notification)
  139.  
  140.         components.Stroke.Color = config.color
  141.         components.Icon.Image = "rbxassetid://" .. config.iconId
  142.         components.Icon.ImageColor3 = config.color
  143.         components.Text.Text = config.message
  144.  
  145.         local textSize = TextService:GetTextSize(
  146.             config.message,
  147.             25,
  148.             Enum.Font.SourceSansBold,
  149.             Vector2.new(400, 50)
  150.         )
  151.  
  152.         local appearTween, disappearTween = createTweens(notification, textSize)
  153.  
  154.         appearTween:Play()
  155.         task.wait(config.duration)
  156.         disappearTween:Play()
  157.         task.wait(0.5)
  158.  
  159.         removeNotification(notification)
  160.     end
  161.  
  162.     task.spawn(notificationProcess)
  163. end
  164.  
  165. -- Public Methods
  166. function NotificationHandler.CreateInfo(message: string, duration: number)
  167.     createBaseNotification({
  168.         message = message,
  169.         duration = duration,
  170.         iconId = NOTIFICATION_ICONS.INFO,
  171.         color = NOTIFICATION_COLORS.INFO
  172.     })
  173. end
  174.  
  175. function NotificationHandler.CreateWarn(message: string, duration: number)
  176.     createBaseNotification({
  177.         message = message,
  178.         duration = duration,
  179.         iconId = NOTIFICATION_ICONS.WARN,
  180.         color = NOTIFICATION_COLORS.WARN
  181.     })
  182. end
  183.  
  184. function NotificationHandler.CreateError(message: string, duration: number)
  185.     createBaseNotification({
  186.         message = message,
  187.         duration = duration,
  188.         iconId = NOTIFICATION_ICONS.ERROR,
  189.         color = NOTIFICATION_COLORS.ERROR
  190.     })
  191. end
  192.  
  193. return NotificationHandler
Tags: luau
Advertisement
Add Comment
Please, Sign In to add comment