Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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.

0
Also The onChatted function has to be called... Oskar2266001 3 — 5y
0
This isn't the entire code. ConnorThomp 87 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago

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 :)

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. ConnorThomp 87 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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.

Answer this question