Hello, developers!
Today, I want to show you how to make a part or NPC chat!
A talking part!
Cool talking noob!
This tutorial requires a beginner amount of knowledge of scripting such as variables, services, loops, and tables.
Step 1: Create a part in workspace
and insert a script. Delete the default code.
Step 2: Get the Chat
service. This service is mainly used for handling the built-in chat system, but us developers can use it, too! Today, we’ll be using the built-in :Chat()
function. Type this in your script:
local ChatService = game:GetService("Chat")
Step 3: You can customize this however you want and I’ll explain how to in a minute. For the sake of this tutorial though, we will create a table of phrases that the part/NPC will be able to say. Under where you previously defined the ChatService
, create a new table named Phrases and create a few strings of text you want the part or NPC to say.
--Create as much text as you want!
local phrases = {
"Hello!",
"Welcome to the game!",
"ROBLOX is the best game ever!"
}
Step 4: Create a while do loop
that runs every four seconds and chooses a random piece of text from our phrase table.
while task.wait(4) do
local RandomPhrase = phrases[math.random(1 , #phrases)] --Chooses a random string from our phrases table. It will randomize between a number as low as 1 and a maximum of however many strings are in our table.
end
Step 5: Make the part/NPC chat. To do this, inside of our while loop
, we will fire the :Chat()
method of the ChatService
.
This function takes three parameters:
- partOrCharacter: Instance – The part to display a chat bubble above.
- message: string – The text to appear in the chat bubble.
- color: Enum.ChatColor – The color of the chatted message. (White is recommended as other colors sometimes make the bubble and/or text invisible when other colors overlap with it on screen.)
ChatService:Chat(workspace.Part, RandomPhrase, Enum.ChatColor.White)
Final code:
local ChatService = game:GetService("Chat")
local phrases = {
"Hello!",
"Welcome to the game!",
"ROBLOX is the best game ever!"
}
while task.wait(4) do
local RandomPhrase = phrases[math.random(1 , #phrases)]
ChatService:Chat(workspace.Part, RandomPhrase, Enum.ChatColor.White)
end
Playtest this and see if it works!
If you have an NPC you want to make talk, change workspace.Part
to NPC.Head
.
If you enjoyed this tutorial, please let me know by liking it! If there is anything you think I could do to improve or change, feel free to let me know. I’m new to creating tutorials, so feedback is always appreciated! You can also let me know if there is anything you would like to know how to create! I will try to help you!
Have a wonderful day!
~Kittylitterking123