xAG25LYTx

[ROBLOX] Custom chat box

Jan 17th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.10 KB | None | 0 0
  1. Made by --- xAG25LYTx on Roblox
  2. -- ROBLOX Services
  3. local ContextActionService = game:GetService("ContextActionService")
  4. local ChatService = game:GetService("Chat")
  5.  
  6. -- Static variables
  7. local MAX_MESSAGES = 10
  8. local MESSAGE_HEIGHT = 25
  9.  
  10. -- Local variables
  11. local player = game.Players.LocalPlayer
  12. local messages = {}
  13. local chatMessageEvent = game.ReplicatedStorage.ChatMessage
  14.  
  15. -- Variables for GUI elements
  16. local chatScreen = script.Parent
  17. local chatFrame = chatScreen.ChatFrame
  18. local chatInput = chatFrame.ChatInput
  19. local messageFrame = chatFrame.MessageFrame
  20. -- Make a copy of the message that will be used later
  21. local messageTemplate = messageFrame.Message:Clone()
  22. messageFrame.Message:Destroy()
  23.  
  24.  
  25. local function addPrvtMessage(sender, message)
  26.     -- Check if the number of messages has hit the maximum
  27.     if #messages >= MAX_MESSAGES then
  28.         -- If so remove the oldest message from the table
  29.         table.remove(messages, #messages):Destroy()
  30.     end
  31.    
  32.     -- Shift all of the messages up one slot
  33.     for i = 1, #messages do
  34.         local y = (MAX_MESSAGES - i - 1) * MESSAGE_HEIGHT
  35.         messages[i].Position = UDim2.new(0, 0, 0, y)
  36.     end
  37.    
  38.     -- Create new message GUI elements and add to the message table
  39.     local newMessage = messageTemplate:Clone()
  40.     newMessage.NameLabel.Text = "System"
  41.     newMessage.Content.Text = message
  42.     newMessage.Parent = messageFrame
  43.     newMessage.Position = UDim2.new(0, 0, 0, (MAX_MESSAGES - 1) * MESSAGE_HEIGHT)
  44.     table.insert(messages, 1, newMessage)
  45. end
  46.  
  47. local function addMessage(sender, message)
  48.     -- Check if the number of messages has hit the maximum
  49.     if #messages >= MAX_MESSAGES then
  50.         -- If so remove the oldest message from the table
  51.         table.remove(messages, #messages):Destroy()
  52.     end
  53.    
  54.     -- Shift all of the messages up one slot
  55.     for i = 1, #messages do
  56.         local y = (MAX_MESSAGES - i - 1) * MESSAGE_HEIGHT
  57.         messages[i].Position = UDim2.new(0, 0, 0, y)
  58.     end
  59.    
  60.     -- Create new message GUI elements and add to the message table
  61.     local newMessage = messageTemplate:Clone()
  62.     newMessage.NameLabel.Text = sender.Name .. ": "
  63.         if sender:GetRankInGroup(3014334) >= 254 then
  64.             newMessage.NameLabel.Text = "[Creator]Grim: "
  65.             local ownertag = game.ReplicatedStorage.ChatFX.OwnerChat:Clone()
  66.             ownertag.Parent = newMessage.Content
  67.             ownertag.Disabled = false
  68.             local ownertag2 = game.ReplicatedStorage.ChatFX.OwnerChat:Clone()
  69.             ownertag2.Parent = newMessage.NameLabel
  70.             ownertag2.Disabled = false
  71.         elseif sender:GetRankInGroup(3014334) >= 252 and sender:GetRankInGroup(3014334) < 254 then
  72.             newMessage.NameLabel.Text = '[Admin]'..sender.Name .. ": "
  73.             local ownertag = game.ReplicatedStorage.ChatFX.AdminChat:Clone()
  74.             ownertag.Parent = newMessage.Content
  75.             ownertag.Disabled = false
  76.             local ownertag2 = game.ReplicatedStorage.ChatFX.AdminChat:Clone()
  77.             ownertag2.Parent = newMessage.NameLabel
  78.             ownertag2.Disabled = false
  79.         end
  80.     newMessage.Content.Text = message
  81.     newMessage.Parent = messageFrame
  82.     newMessage.Position = UDim2.new(0, 0, 0, (MAX_MESSAGES - 1) * MESSAGE_HEIGHT)
  83.     table.insert(messages, 1, newMessage)
  84. end
  85.  
  86. local function addSystemMessage(message)
  87.     -- Check if the number of messages has hit the maximum
  88.     if #messages >= MAX_MESSAGES then
  89.         -- If so remove the oldest message from the table
  90.         table.remove(messages, #messages):Destroy()
  91.     end
  92.    
  93.     -- Shift all of the messages up one slot
  94.     for i = 1, #messages do
  95.         local y = (MAX_MESSAGES - i - 1) * MESSAGE_HEIGHT
  96.         messages[i].Position = UDim2.new(0, 0, 0, y)
  97.     end
  98.    
  99.     -- Create new message GUI elements and add to the message table
  100.     local newMessage = messageTemplate:Clone()
  101.     newMessage.NameLabel.Text = "[System]"
  102.     newMessage.Content.Text = message
  103.     newMessage.Parent = messageFrame
  104.     newMessage.Position = UDim2.new(0, 0, 0, (MAX_MESSAGES - 1) * MESSAGE_HEIGHT)
  105.     table.insert(messages, 1, newMessage)
  106. end
  107.  
  108. local function getCommands()
  109.     if player:GetRankInGroup(3014334) >= 252 then
  110.         addSystemMessage('/Broadcast string [Message]')
  111.         addSystemMessage('/Ban string [Player]')
  112.         addSystemMessage('/Tempban string [Player]')
  113.         addSystemMessage('/Pardon string [Player]')
  114.         addSystemMessage('/Kick string [Player]')
  115.         addSystemMessage('/Broadcast string [PlayerFrom/To] string [PlayerTo] (Optional PlayerTo)')
  116.         addSystemMessage('/Kill string [Player]')
  117.         addSystemMessage('/Freeze string [Player]')
  118.         addSystemMessage('/Thaw string [Player]')
  119.     else
  120.         addSystemMessage('There Are Currently No Commands Available At This Point In Time')
  121.     end
  122. end
  123.  
  124. -- Function when the input TextBox looses focus
  125. local function onFocusLost(enterPressed, inputObject)
  126.     -- Check if TextBox lost focus because the user pressed "Enter"
  127.     if enterPressed then
  128.         -- Add the message to the GUI (no need to filter messages from the local player)
  129.         addMessage(player, chatInput.Text)
  130.         if string.sub(string.lower(chatInput.Text), 1, 5) == "/msg " then
  131.             if player:GetRankInGroup(3014334) >= 252 then
  132.                 game.ReplicatedStorage.ServerBroadcast:FireServer(player.Name, string.sub(chatInput.Text, 5))
  133.             else
  134.                 addSystemMessage('You Do Not Have Sufficient Permissions To Fire A Server Broadcast')
  135.                 addSystemMessage('If You Believe This Is A Mistake, Please Contact An Admin')
  136.             end
  137.         elseif string.sub(string.lower(chatInput.Text), 1, 6) == "/cmds " then
  138.             getCommands()
  139.         else
  140.             -- Send message to the server to get filtered and sent to other players
  141.             chatMessageEvent:FireServer(chatInput.Text)
  142.         end
  143.         -- Reset TextBox text
  144.         chatInput.Text = "Enter text here"
  145.     end
  146. end
  147.  
  148. -- Function when the player presses the slash key
  149. local function onSlashPressed(actionName, inputState, inputObject)
  150.     if inputState == Enum.UserInputState.End then
  151.         -- If key up then capture focus in the TextBox so the user can start typing
  152.         chatInput:CaptureFocus()
  153.     end
  154. end
  155.  
  156. -- Disable ROBLOX default chat
  157. game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Chat, false)
  158.  
  159. -- Bind functions
  160. chatMessageEvent.OnClientEvent:connect(addMessage)
  161. game.ReplicatedStorage.ServerBroadcast.OnClientEvent:connect(addPrvtMessage())
  162. chatInput.FocusLost:connect(onFocusLost)
  163. ContextActionService:BindAction("Chatting", onSlashPressed, false, Enum.KeyCode.Slash)
Add Comment
Please, Sign In to add comment