Make NPC Talk

Make an NPC or part speak random phrases at a specified interval!

by MrSprinkleToes

Author Avatar

Making an NPC Speak

By MrSprinkleToes

Before we begin...

Hello! Welcome to my tutorial. Today you will learn how to make an NPC (or any part) say random phrases, which are stored in a table. Now let's begin!

Step one: The table.

First, you must create a table containing the phrases you want the NPC to speak. Simply create a table containing multiple strings, each string a different phrase.

-==========-

local phrases = {"Hello!","How are you?","I love ROBLOX!"}

-==========-

Step two: An infinite loop.

Simple, just create an infinite while loop with a few seconds wait in between.

-==========-

local phrases = {"Hello!","How are you?","I love ROBLOX!"}
while wait(5) do
    -- ...
end

-==========-

Step three: Fetching a random phrase.

Now you just need to use math.random() to fetch a random phrase.

-==========-

local phrases = {"Hello!","How are you?","I love ROBLOX!"}
while wait(5) do
    local phrase = phrases[math.random(1,#phrases)]
    -- ...
end

-==========-

Step four: Finally, make the part / NPC chat the phrase!

Not very hard! Simply use the service "Chat" and call the :Chat() function, providing these three parameters:

  1. partOrCharacter (Instance)
  2. message (string)
  3. color (ChatColor, Enum.ChatColor)

-==========-

local phrases = {"Hello!","How are you?","I love ROBLOX!"}
while wait(5) do
    local phrase = phrases[math.random(1,#phrases)]
    game:GetService("Chat"):Chat(workspace.PartName,phrase,Enum.ChatColor.White)
end

-==========-

And that's all!

Congratulations, you've done it! Now using the above code we put together, the specified part (or character) will chat every 5 seconds one of the phrases placed in the table "phrases". You can change the wait(number) to anything you'd like, and you can change up the phrases too!

The color Enum.ChatColor.White makes the chat bubble look like a regular chat bubble, however there are different options. Those include:

Happy scripting!