Advertisement
ElijahCrafter

Test

Apr 5th, 2024 (edited)
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.22 KB | None | 0 0
  1. local chatLog = {}  -- Table to store chat messages
  2. local maxMessages = 10  -- Maximum number of messages to display at once
  3. local startIndex = 1  -- Starting index for displaying chat messages
  4.  
  5. local function printChatMessages()
  6.     term.clear()
  7.     term.setCursorPos(1, 1)
  8.     print("=== Chat Log ===")
  9.     for i = startIndex, math.min(#chatLog, startIndex + maxMessages - 1) do
  10.         print(chatLog[i])
  11.     end
  12.     print("Press 'Q' to exit. Use Up/Down arrows to scroll.")
  13. end
  14.  
  15. while true do
  16.     local event, p1, p2, p3, p4, p5 = os.pullEvent()
  17.     if event == "chat" then
  18.         local username = p2
  19.         local message = p3
  20.         table.insert(chatLog, username .. ": " .. message)  -- Log the chat message
  21.         printChatMessages()  -- Print the updated chat log
  22.     elseif event == "key" then
  23.         if p1 == keys.q then
  24.             break  -- Exit the program if 'Q' key is pressed
  25.         elseif p1 == keys.up then
  26.             startIndex = math.max(1, startIndex - 1)  -- Scroll up
  27.             printChatMessages()
  28.         elseif p1 == keys.down then
  29.             startIndex = math.min(#chatLog - maxMessages + 1, startIndex + 1)  -- Scroll down
  30.             printChatMessages()
  31.         end
  32.     end
  33. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement