Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to use onChatted to check if a player said a phrase within a table?

So I have a table of words, these words correspond to a model in-game. The idea is the player can say "regen ..ModelNameFromTable" and that model will regen.

RegenModule = require(game.ServerScriptService.RegenModule)

 local Models= {
            "ModelTest",
            "Model",

}

function onChatted(msg, recipient, Player)
 msg = string.lower(msg)
  if (msg == "regen"..Models.ModelName) then
   RegenModule.Regen(Player)
 end
end

As you can see I put an example here "if (msg == "regen"..Models.ModelName) then", but obviously this does not work.

Also, it would have to carry over as an argument to the ModuleScript, as that's where the main script is located.

2 comments

0

Also The onChatted function has to be called...

0

This isn't the entire code.

2 answers

Here's an easier way that works:

game.Players.PlayerAdded:Connect(function(player)
    game.Players[player.Name].Chatted:Connect(function(msg)
    	if msg == "Your Phrase" then
    	   -- Your code here
    	end
    end
end

Please put [SOLVED] in your title if it helped and leave me an Upvote :)

1 comment

0

If I was to do that I would have to repeat that code for every single model. At least, that's how it looks, which is extremely inefficient.

Managed to solve my own problem, just took some trial and error.

RegenModule = require(game.ServerScriptService.RegenModule)

  

local Models= {

         "Model",
         "Model2",

}

  

function onChatted(msg, recipient, Player)
msg = string.lower(msg)
 for index = 1, #Models do
 local value = Models[index]
  if (msg == "regen"..value) then
   RegenModule.Regen(Player, value)
  end
 end
end

This worked perfectly, and I can use the value as an argument for my ModuleScript.

This discussion is preserved as it was. New answers, comments, and votes are closed.