Hey, i’ve been needing an AI chatbot for my game, and i can’t wrap my head around all this python and HTTP and pricing stuff, so i thought i could just code my own. It’s nice because it doesn’t need to remember anything, just puke stuff out as a response to text.
The basis of my game is that players try to sell basic items to customers with an outrageous pitch, like “You rub this rock on your skin and it makes you live forever.” How can i make such a thing?
go get yourself a google gemini api key and just follow the instructions from the API, you get 30 prompts per minute for free with Gemini 2.0 Flash-Lite
here’s my code, obviously doesn’t work since i haven’t set it up with gemini yet, its currently set up with openrouter
--// AI setup
local url = "https://openrouter.ai/api/v1/chat/completions"
local headers = {
["Authorization"] = "key" -- there is an actual key here
["HTTP-Referer"] = "roblox.com", -- required by OpenRouter, can be anything
["X-Title"] = "RobloxCustomerBot" -- optional, for dashboard naming
}
--// Sequence
while true do
task.wait(3)
Sounds.Bell:Play()
task.wait(0.2)
tweenIn:Play()
task.wait(1)
local product = products[math.random(1, #products)]
local messagePicked = beginnings[math.random(1, #beginnings)]
local textAsked = string.gsub(messagePicked, "<PRODUCT>", product)
dialogueRF:InvokeClient(player, textAsked)
local pitch = player.Chatted:Wait()
local prompt = "The player is trying to sell you an item called '" .. product .. "'. They say: " .. pitch ..
". Respond as a playful, slightly gullible customer who judges the pitch. At the end, say <YES> if you’ll buy, or <NO> if you won’t."
local body = HTTPService:JSONEncode({
model = "mistralai/mistral-7b-instruct",
messages = {
{ role = "user", content = prompt }
}
})
local success, response = pcall(function()
return HTTPService:PostAsync(url, body, Enum.HttpContentType.ApplicationJson, false, headers)
end)
if success then
local result = HTTPService:JSONDecode(response)
local message = result.choices and result.choices[1] and result.choices[1].message and result.choices[1].message.content or "Huh?"
dialogueRF:InvokeClient(player, message)
else
warn("OpenRouter error:", response)
dialogueRF:InvokeClient(player, "Uh... Let me sleep on it.")
end
task.wait(3)
tweenOut:Play()
end
local HTTPService = game:GetService("HttpService")
local APIKey = "" -- Change this!!!
local URL = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=" .. APIKey
local prompt = "Say hello!"
local body = HTTPService:JSONEncode({
contents = {
["parts"] = {
["text"] = prompt
}
}
})
local response = HTTPService:PostAsync(URL, body)
local data = HTTPService:JSONDecode(response)
print(data)
for Index, Value in data.candidates[1].content.parts do
print(Value["text"])
end
if data.message == "success" then
print(data.message)
end