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