By MrSprinkleToes
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!
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!"}
-==========-
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
-==========-
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
-==========-
Not very hard! Simply use the service "Chat" and call the :Chat() function, providing these three parameters:
-==========-
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
-==========-
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!