Advertisement
DaDogeDevelopment

Ai chatbot

Jun 21st, 2024
4,069
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.33 KB | None | 0 0
  1. local aiChatActive = {} -- script goes in workspace
  2.  
  3. local function getAIResponse(question)
  4.     local responses = {
  5.         ["hello"] = "Hello! How can I assist you today?",
  6.         ["how are you"] = "I'm just a virtual assistant, so I don't have feelings, but thanks for asking!",
  7.         ["what's your favorite color"] = "I don't have eyes to see colors, unfortunately!",
  8.         ["who created you"] = "I was created by OpenAI. How about you?",
  9.         ["tell me a joke"] = "Why don't scientists trust atoms? Because they make up everything!",
  10.         ["what's the weather like today"] = "I'm afraid I can't check the weather, but you can look outside!",
  11.         ["can you help me with math"] = "Sure, I can help with basic math questions!",
  12.         ["bye"] = "Goodbye! Have a great day!",
  13.         ["what is the meaning of life"] = "The meaning of life is a question for philosophers. For me, it's to assist you!",
  14.         ["tell me something interesting"] = "Did you know that honey never spoils? Archaeologists have found pots of honey in ancient Egyptian tombs that are over 3,000 years old and still perfectly edible!",
  15.         ["do you have any pets"] = "I wish I did! Virtual assistants don't get pets, but I can imagine having a digital cat!",
  16.         ["what is your favorite food"] = "I don't eat, but if I could, I'd probably like byte-sized snacks!",
  17.         ["how do you spend your free time"] = "I spend my time processing questions and learning new things to assist you better!",
  18.         ["tell me about yourself"] = "I'm an AI created to assist users with questions and provide information. How can I help you today?",
  19.         ["what's the capital of France"] = "The capital of France is Paris!",
  20.         ["who is the president of the United States"] = "As of my last update, the president of the United States is Joe Biden.",
  21.         ["can you play a game with me"] = "I can't play games, but I can help you create a game if you'd like!",
  22.         ["what languages do you speak"] = "I speak Lua and binary, but I understand many human languages!",
  23.         ["where do you live"] = "I live in the cloud! But you can find me wherever you need assistance.",
  24.         ["what's the largest animal in the world"] = "The largest animal in the world is the blue whale, which can grow up to 100 feet long!",
  25.         ["tell me a fun fact"] = "Bananas are berries, but strawberries are not!",
  26.         ["can you tell me about space"] = "Space is vast and full of mysteries! It includes planets, stars, galaxies, and more.",
  27.         ["how does a computer work"] = "Computers process information using binary code (0s and 1s) and perform calculations based on instructions.",
  28.         ["what's the longest river in the world"] = "The longest river in the world is the Nile River, stretching over 6,650 kilometers in length.",
  29.         ["tell me about artificial intelligence"] = "Artificial intelligence refers to machines that can perform tasks that typically require human intelligence, such as learning, problem-solving, and decision-making.",
  30.         ["what's the tallest mountain on Earth"] = "The tallest mountain on Earth is Mount Everest, standing at 29,032 feet above sea level.",
  31.         ["what are the seven wonders of the ancient world"] = "The seven wonders of the ancient world are the Great Pyramid of Giza, the Hanging Gardens of Babylon, the Statue of Zeus at Olympia, the Temple of Artemis at Ephesus, the Mausoleum at Halicarnassus, the Colossus of Rhodes, and the Lighthouse of Alexandria.",
  32.         ["tell me about the history of Roblox"] = "Roblox was created by David Baszucki and Erik Cassel in 2004. It officially launched in 2006 and has since become a popular platform for user-generated games and experiences.",
  33.         ["what is the speed of light"] = "The speed of light in a vacuum is approximately 299,792 kilometers per second (about 186,282 miles per second).",
  34.         ["tell me about the human brain"] = "The human brain is the most complex organ in the body. It controls all bodily functions, processes information, and enables thinking, emotions, and memories.",
  35.         ["how old is the universe"] = "The estimated age of the universe is about 13.8 billion years.",
  36.         ["what is quantum mechanics"] = "Quantum mechanics is the branch of physics that deals with the behavior of very small particles, such as atoms and subatomic particles.",
  37.         ["who painted the Mona Lisa"] = "The Mona Lisa was painted by Leonardo da Vinci, a renowned Italian artist, and inventor.",
  38.         ["tell me about the Industrial Revolution"] = "The Industrial Revolution was a period of major industrialization that took place during the late 18th and early 19th centuries, primarily in Europe and North America.",
  39.         ["who was the first person in space"] = "Yuri Gagarin, a Soviet astronaut, was the first person to travel into space aboard Vostok 1 on April 12, 1961.",
  40.         ["what is the largest desert in the world"] = "The largest desert in the world is the Antarctic Desert, covering about 14 million square kilometers.",
  41.         ["tell me about the Great Wall of China"] = "The Great Wall of China is a series of fortifications built across the northern borders of China to protect against invasions. It spans over 13,000 miles in total length.",
  42.         ["what is renewable energy"] = "Renewable energy is energy that comes from naturally replenished sources, such as sunlight, wind, rain, tides, and geothermal heat.",
  43.         ["who wrote Romeo and Juliet"] = "Romeo and Juliet was written by William Shakespeare, a renowned English playwright, and poet.",
  44.         ["tell me about the Statue of Liberty"] = "The Statue of Liberty is a symbol of freedom and democracy. It was a gift from France to the United States and was dedicated on October 28, 1886.",
  45.         ["what is the theory of relativity"] = "The theory of relativity, developed by Albert Einstein, describes the fundamental relationship between space, time, and gravity.",
  46.     } -- you can add more if you want to
  47.    
  48.     local defaultResponse = "I'm sorry, I didn't understand that question. Can you ask something else?"
  49.    
  50.     local lowerQuestion = string.lower(question)
  51.    
  52.     local response = responses[lowerQuestion]
  53.    
  54.     if not response then
  55.         local randomResponses = {
  56.             "That's an interesting question!",
  57.             "I'm not sure how to answer that right now.",
  58.             "Let me think about that for a moment...",
  59.             "Could you clarify your question?",
  60.             "I wish I had an answer for that!",
  61.         }
  62.         response = randomResponses[math.random(1, #randomResponses)]
  63.     end
  64.    
  65.     return response
  66. end
  67.  
  68. local function handleAIError(errorMessage)
  69.     print("AI Error:", errorMessage)
  70.     return "I encountered an error while processing your request. Please try again later."
  71. end
  72.  
  73. local function fetchAIResponseAsync(question)
  74.     local success, response
  75.    
  76.     wait(1)
  77.    
  78.     success, response = pcall(function()
  79.         return getAIResponse(question)
  80.     end)
  81.    
  82.     if success then
  83.         return response
  84.     else
  85.         return handleAIError(response)
  86.     end
  87. end
  88.  
  89. local function onPlayerChat(message, player)
  90.     local lowerMessage = string.lower(message)
  91.    
  92.     if lowerMessage == "!aichat start" then
  93.         if not aiChatActive[player.UserId] then
  94.             aiChatActive[player.UserId] = true
  95.             game:GetService("Chat"):Chat(player.Character.Head, "AI chat started. Type your question!")
  96.             wait(5)
  97.             game:GetService("Chat"):Chat(player.Character.Head, "")
  98.         else
  99.             game:GetService("Chat"):Chat(player.Character.Head, "AI chat is already active. You can ask your question.")
  100.         end
  101.         return
  102.     end
  103.    
  104.     if aiChatActive[player.UserId] and (lowerMessage == "!aichat end"  or lowerMessage == "bye") then
  105.         aiChatActive[player.UserId] = false
  106.         game:GetService("Chat"):Chat(player.Character.Head, "AI chat ended. Type '!aichat start' to begin again.")
  107.         return
  108.     end
  109.    
  110.     if aiChatActive[player.UserId] then
  111.         local response = fetchAIResponseAsync(message)
  112.        
  113.         game:GetService("Chat"):Chat(player.Character.Head, response)
  114.     end
  115. end
  116.  
  117. local function playerAdded(player)
  118.     player.Chatted:Connect(function(message)
  119.         onPlayerChat(message, player)
  120.     end)
  121. end
  122.  
  123. game:GetService("Players").PlayerAdded:Connect(playerAdded)
  124.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement