ChatBot AI Script

I need hyelp, I am planning to make a Raise a Baby Game and when it turns to a certain age, it will enable the script so the baby can speak back when talked to, this is my script, the other script is just pirinting it and chatting it:

ServerScriptService in a Server Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local HttpService = game:GetService("HttpService")
local Players = game:GetService("Players")

local chatToAIEvent = ReplicatedStorage:WaitForChild("ChatToAI")

local openaiUrl = "https://api.openai.com/v1/chat/completions"

-- Replace with your actual OpenAI API Key
local openaiApiKey = "YOUR API"

local function getChatbotResponse(userInput, role)
	-- Define the conversation context
	local messages = {
		{role = "system", content = "You are a friendly chatbot."},
		{role = role, content = userInput}
	}

	-- Prepare the request data
	local requestData = {
		model = "gpt-3.5-turbo",  -- Using the appropriate model
		messages = messages,
		max_tokens = 150,
		temperature = 0.7
	}

	local success, response = pcall(function()
		-- Send request to OpenAI API
		return HttpService:RequestAsync({
			Url = openaiUrl,
			Method = "POST",
			Headers = {
				["Content-Type"] = "application/json",
				["Authorization"] = "Bearer " .. openaiApiKey
			},
			Body = HttpService:JSONEncode(requestData)
		})
	end)

	if success then
		if response.Success then
			local responseData
			local statusCode = response.StatusCode
			-- Handle the response data
			local success, decodedData = pcall(function()
				return HttpService:JSONDecode(response.Body)
			end)

			if success and decodedData.choices and #decodedData.choices > 0 then
				return decodedData.choices[1].message.content:trim()  -- Extract response
			else
				return "Error: No valid response from the chatbot service."
			end
		else
			return "Error: API request failed with status code " .. response.StatusCode
		end
	else
		return "Error: Request failed with error: " .. response
	end
end

local function onPlayerChatted(player, message)
	local role = "user"  -- This can change depending on how you want the NPC to speak (you can add logic for adult/kid)

	-- You could add logic to change the 'role' or system context based on the player's choice
	local response = getChatbotResponse(message, role)
	chatToAIEvent:FireClient(player, response)
end

Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message)
		onPlayerChatted(player, message)
	end)
end)

Im trying to make the future

did you just post your api key :skull:

5 Likes

This script looks like it was generated by ChatGPT and as such it is missing some key things:

  1. You return decodedData.choices[1].message.content:trim() on a successful HTTP response. trim() is not a class method of the string datatype. This looks to be just a straight up hallucination by your chatbot.

  2. You are returning values but not printing or displaying them anywhere, this may be the root of you not being able to debug your code.

  3. You are storing your API key in plaintext (though I’m presuming that is not your real API key that’s in the embedded script). You should be using Secrets to store all vulnerable information used in HTTP requests. Even though this is a server script, I would still call storing keys in plaintext an unsafe practice.

  4. If you continue to use ChatGPT or other LLMS to generate code for you, you will not understand what you are implementing. While at first this may seem harmless, if you aren’t able to understand your code, you can’t debug it effectively. I really suggest you look further into whatever you are trying to implement, as a chatbot like this can be created fully in Roblox using Luau if you really wish to do so.

If you are confused or want to know more about anything pls lmk and I’ll be happy to inform. I have a working example of the script you provided, but I’d like to see you work to achieve it rather than just handing it out.

1 Like

thank you for the scripting help, i am on it rn

She helped more but i ended up finding a chat bot plugin

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.