How do I make trigger words that, if spoken in chat, trigger an event?

I want to do something similar to what theDevKing does in his “funny roblox ideas” videos.
I’m fairly new to scripting, and I can’t figure out just how to do it.
I copied the code he wrote exactly (I think) but it didn’t work.
The only thing I can do is

`local Players = game:GetService(“Players”)
local Client = Players.LocalPlayer or Players:GetPropertyChangedSignal(“LocalPlayer”):Wait()
local module = require(game.ServerStorage.KosAndAosSettings)
local prefix = module.prefix

game.Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Message)
		if Message == "Hello World"then
			print("Hello World from "..Player.Name)
		end
	end)
end)

But that only works if the message is exactly what is in the script. I want it so any one of the trigger words said anywhere in a sentence will trigger the event.

1 Like

You’ll have to use some sort of array and loop through all entries to check if the message is what you’re looking for. There are several ways you can do this, though, so don’t threat. The most simple and easy way to do this is through table.find(allowedKeywords, Message) or alternatively allowedKeywords:find(Message). You can also use for loops to go through all the entries but that’s not necessary.

1 Like

Are you going to have a consistent events? For example, will every keyword trigger a text response? Or will they do random things?

If you are going to need different code for every keyword, you are going to want to organize it some way. Perhaps have the key phrase as a stringvalue, then a module script child with instructions what to do next. Then you simply search all the StringValues, and if you find your text, execute the module script child.

Each keyword would trigger random things.

local triggerwords = {} --table of trigger words
game.Player.PlayerAdded:Connect(function(plr)
   plr.Chatted:Connect(function(msg)
      for i = 1, #triggerwords do --I don't know how to use ipairs and pairs
         local word = triggerwords[i]
         local matchcheck = string.match(msg, word)
         if matchcheck ~= nil then
            if word == "test word" then
               --do whatever
            elseif word == "test word 2" then
               --do other stuff
            end
         end
      end
   end)
end)

Gets a table of trigger words like ‘Hello world’ and checks for a match. If the match isn’t nil, (you should) check what the current word is and do the rest.

1 Like

Would this work if a trigger word is said anywhere in a sentence?

Yes it would, I tested it myself.