Hello, i’m fairly new to luau scripting, and i’m looking for some advice.
I am making a player chat activated npc, where if you say a certain thing the npc will say something back to you.
So far I have this code, but I hope to add 50+ messages and responses and I was wondering if there is an easier way to do it.
game.Players.PlayerAdded:Connect(function(player)
player.Chatted:connect(function(msg)
if msg == "hi" then
wait(2)
local chats = "hello!"
game:GetService("Chat"):Chat(script.Parent.Head, chats)(Enum.ChatColor.White)
end
end)
end)
Any piece of code that is reusable should generally be put into a function. Also, for string matching, a dictionary would be a very good idea so that you can just store your chat responses in that dictionary and then call the NPC chat function with the corresponding chat you found in your dictionary.
local Players = game:GetService("Players")
local CS = game:GetService("Chat")
Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
Player.Chatted:connect(function(Msg)
if Msg == "hi" then
task.wait(3)
local chats = "hello!"
local head = Character:WaitForChild("Head")
CS:Chat(head, chats, Enum.ChatColor.White)
end
end)
end)
end)
The script you provided wouldn’t have worked, the arguments passed to “:Chat()” need to be together, not split.
This is the way I would approach from my limited scripting knowledge but someone more experienced may even have a way better method-
local p_Chats = {"hi","How are you"} -- for player
local npc_Chats = {"hello","I am fine"}
-- u will also see I use camel casing because I think that is very clean for writing code.
local function onChatted(msg)
for index,value in pairs(p_Chats) do
if (msg == value) then
local reply = npc_Chats[index]
-- the rest of the code using chat service and etc
end
end
end
game.Players.PlayedAdded:Connect(function(plr)
plr.Chatted:Connect(onChatted)
end)
To explain,
Every time the player chats this function gets called.
The function checks if this message is mentioned in p_Chats {player chats}.
If there is, then it takes the index and on npc_Chats it gets the message in the same index which would be the reply.
So you need to set your player chat and NPC chat to some-what like this
local p_Chats = {"Where do you live?","What's your favorite subject?"}
local npc_Chats = {"I live in Antarctica","Physics!"}
The Player’s message and NPC’s replies should have the same index
I am extremely bad at explaining, pardon, and I hope you understood.
Of corse there is! Using if statements for each interaction is not a good practice, use tables instead. Using tables you will also be able to put multiple responses for a player said message.
local messages = {
["hi"] = {
"Hello!",
"Hey!"
},
["How are you?"] = {
"I'm fine",
"I'm fine, and you?"
}
}
Player.Chatted:Connect(function(message)
local randomResponce = messages[message][math.random(1, #messages[message))
-- chat the randomResponce variable here
end)
So let’s say that the player says “hi” what the script will do is choosing a random string from the messages table by searching in the “hi” subtable
If the player says hi it will do this: messages[message] will be like messages[“hi”] where the [] are used as a find function
messages[message][math.random(1, #messages[message]) will choose a random string from the “hi” table as every string In a table has an index. Since the “hi” table has 2 strings it will do math.random(1, 2) if there are 5 string it would do math.random(1, 5)