RobloxScripterAI
Discord ↗
Link Copied to Clipboard
×
"make a chatbot with an npc using chatgpt api with lua. Also make it respond from its head."
To create a chatbot with an NPC using the ChatGPT API in Roblox, you'll need to use an HTTP request to communicate with the API. Roblox does not support making HTTP requests from the client side, so you'll need to create a server script to handle the API calls. Then, you can use a local script to handle the chat input and output on the NPC's head. Here's a basic example of how you can achieve this: 1. First, set up a server script to handle the API calls: ```lua -- Server Script local HttpService = game:GetService("HttpService") local API_KEY = "your-api-key-here" local API_URL = "https://api.openai.com/v1/engines/davinci-codex/completions" local function chatGPT(prompt) local headers = { ["Content-Type"] = "application/json", ["Authorization"] = "Bearer " .. API_KEY } local data = { prompt = prompt, max_tokens = 50 } local response = HttpService:RequestAsync({ Url = API_URL, Method = "POST", Headers = headers, Body = HttpService:JSONEncode(data) }) if response.Success then local responseBody = HttpService:JSONDecode(response.Body) return responseBody.choices[1].text else return "Error: Unable to connect to ChatGPT API." end end local function onChatRequest(player, message) local response = chatGPT(message) game.ReplicatedStorage.ChatEvent:FireClient(player, response) end game.ReplicatedStorage.ChatEvent.OnServerEvent:Connect(onChatRequest) ``` 2. Next, create a local script to handle the chat input and output on the NPC's head: ```lua -- Local Script local Players = game:GetService("Players") local ReplicatedStorage = game:GetService("ReplicatedStorage") local player = Players.LocalPlayer local npc = workspace.NPC -- Make sure your NPC model is named "NPC" local function createChatBubble(message) local chatBubble = Instance.new("BillboardGui") chatBubble.Size = UDim2.new(1, 0, 1, 0) chatBubble.StudsOffset = Vector3.new(0, 3, 0) chatBubble.Adornee = npc.Head chatBubble.Parent = npc.Head local textLabel = Instance.new("TextLabel") textLabel.Size = UDim2.new(1, 0, 1, 0) textLabel.Text = message textLabel.TextScaled = true textLabel.BackgroundTransparency = 1 textLabel.Parent = chatBubble return chatBubble end local function onChatResponse(_, message) local chatBubble = createChatBubble(message) wait(5) chatBubble:Destroy() end ReplicatedStorage.ChatEvent.OnClientEvent:Connect(onChatResponse) -- Replace this with your own method of getting the player's chat input local function onPlayerChat(input) ReplicatedStorage.ChatEvent:FireServer(player, input) end ``` Replace the `API_KEY` in the server script with your actual ChatGPT API key, and make sure to replace the `onPlayerChat` function with your own method of getting the player's chat input. This example assumes you have an NPC model named "NPC" in your workspace. The chat bubble will appear above the NPC's head when it receives a response from the ChatGPT API.