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
function GetWords(sentence)
Words = {}
for word in string.gmatch(sentence, '%S+') do
table.insert(Words, word)
end
return Words
end
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
for index, plr in pairs(game.Players:GetPlayers()) do
end
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
for index, plr in pairs(game.Players:GetPlayers()) do
if plr.Name:lower():sub(1,PlrName:len()) == PlrName:lower() then
return plr
end
end
(: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'
function FindTarget(PlrName)
if PlrName == nil then
return nil
end
if PlrName:lower() == 'everyone' then
return 'Everyone'
end
if PlrName:lower() == 'all' then
return 'Everyone'
end
if PlrName:lower() == 'me' then
return nil
end
if PlrName:lower() == 'random' then
return game.Players:GetPlayers()[math.random(1, #game.Players:GetPlayers())]
end
for index, plr in pairs(game.Players:GetPlayers()) do
if plr.Name == PlrName then
return plr
end
end
for index, plr in pairs(game.Players:GetPlayers()) do
if plr.Name:lower():sub(1,PlrName:len()) == PlrName:lower() then
return plr
end
end
return nil
end
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.
1 comment
you should use string.find or string.match