Answered by
6 years ago Edited 6 years ago
I see you're trying to make chat commands, first and best thing to do is get all the words from the message, you can easily do this using this
1 | function GetWords(sentence) |
3 | for word in string.gmatch(sentence, '%S+' ) do |
4 | table.insert(Words, word) |
To get the player is easy, you have to use :lower()
:sub()
and:len()
You can find more about that here
You start with looping trough the players
1 | for index, plr in pairs (game.Players:GetPlayers()) do |
Then use :lower() on both so the player can type for example ReDcO and it will still work
Also use :sub() in combination with :len() to make sure that the name you typed is the same lenght as the player, then use your code
1 | for index, plr in pairs (game.Players:GetPlayers()) do |
2 | if plr.Name:lower():sub( 1 ,PlrName:len()) = = PlrName:lower() then |
(:len() gets the lenght of a string)
This is enough.. but you can imrpove the code by making it into a function that searches for things like 'me' and 'random' and 'everyone'
01 | function FindTarget(PlrName) |
02 | if PlrName = = nil then |
05 | if PlrName:lower() = = 'everyone' then |
09 | if PlrName:lower() = = 'all' then |
13 | if PlrName:lower() = = 'me' then |
17 | if PlrName:lower() = = 'random' then |
18 | return game.Players:GetPlayers() [ math.random( 1 , #game.Players:GetPlayers()) ] |
21 | for index, plr in pairs (game.Players:GetPlayers()) do |
22 | if plr.Name = = PlrName then |
26 | for index, plr in pairs (game.Players:GetPlayers()) do |
27 | if plr.Name:lower():sub( 1 ,PlrName:len()) = = PlrName:lower() then |
35 | local Player = FindTarget( 'Red' ) or ThePlayerWhoSendedTheMessage |
If it uses 'me' it will return nil, that's why I did 'or ThePlayerWhoSendedTheMessage', though, I never definded the variable so you'll have to use the person who sended the message as the variable.
If something is unclear or I wrote a typo or so, just ask.