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

How do i make it so it finds the players name with a shortened version of it?

Asked by 6 years ago

Heres my code:

01if msg:sub(1,3) == "/w " then
02            msg = msg:sub(4)
03 
04            for _,v in pairs(game.Players:GetPlayers()) do
05                if v.Name:lower() == msg:sub(1,v.Name:len()):lower() then
06                    msg = msg:sub(v.Name:len() + 1)
07 
08                    if msg:sub(1,1) == " " then
09                        msg = msg:sub(2)
10                        whisper(plr,v,msg)
11                    end
12                end
13            end
0
you should use string.find or string.match herrtt 387 — 6y

1 answer

Log in to vote
0
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

1function GetWords(sentence)
2    Words = {}
3    for word in string.gmatch(sentence, '%S+') do
4        table.insert(Words, word)
5    end
6    return Words
7end

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

1for index, plr in pairs(game.Players:GetPlayers()) do
2    -- Code
3end

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

1for index, plr in pairs(game.Players:GetPlayers()) do
2    if plr.Name:lower():sub(1,PlrName:len()) == PlrName:lower() then
3        return plr
4    end
5end

(: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'

01function FindTarget(PlrName)
02    if PlrName == nil then
03        return nil
04    end
05    if PlrName:lower() == 'everyone' then
06        return 'Everyone'
07    end
08 
09    if PlrName:lower() == 'all' then
10        return 'Everyone'
11    end
12 
13    if PlrName:lower() == 'me' then
14        return nil
15    end
View all 35 lines...

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.

Ad

Answer this question