Making an NPC Speak
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.
Step two: An infinite loop.
Simple, just create an infinite while loop with a few seconds wait in between.
Step three: Fetching a random phrase.
Now you just need to use math.random() to fetch a random phrase.
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:
- partOrCharacter (Instance)
- message (string)
- color (ChatColor, Enum.ChatColor)
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:
Med Written By MrSprinkleToes
See MrSprinkleToes's profile on Roblox