Most "AI NPC" tutorials work great in a solo test and then fall apart the moment two players are in the server. Here's how to do it so it actually holds up.
The one request
AI text generation happens on the server. Never call it from a LocalScript — your API key would ship to every client. Put a ModuleScript in ServerStorage and call it from a server Script:
local Cortex = require(game.ServerStorage.Cortex)
local ai = Cortex.new("YOUR_KEY")
local line = ai:ask("You are a gruff blacksmith. One short line.", "forge me a sword?")
print(line) --> "Aye, hand over the ore and I'll hammer ye a blade."
The three bugs that only show up in multiplayer
-
Global debounce. Tutorials use one shared
isBusyflag, so only the first player to talk gets a reply and everyone else is dropped. Fix: cooldown keyed byUserId, not a global lock. - No retries. The endpoint 429s under load and one failed request kills the interaction. Fix: retry with backoff.
-
Unfiltered output. Raw model text shown to players can get your game moderated. Fix: run replies through
TextService:FilterStringAsyncbefore displaying.
Doing it right, briefly
If you want per-player memory and the fixes above handled for you, the open-source Cortex kit gives you an NPC object:
local bramm = ai:npc({ personality = "You are Bramm, a dwarven blacksmith.", moderate = true })
local reply = bramm:say(player, message) -- per-player memory + cooldown
local safe = ai:filterForPlayer(reply, player) -- Roblox-filtered, safe to show
Code is MIT and readable: https://github.com/cortex-rbx/roblox-ai-kit
Top comments (0)