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:

if msg:sub(1,3) == "/w " then
            msg = msg:sub(4)

            for _,v in pairs(game.Players:GetPlayers()) do
                if v.Name:lower() == msg:sub(1,v.Name:len()):lower() then
                    msg = msg:sub(v.Name:len() + 1)

                    if msg:sub(1,1) == " " then
                        msg = msg:sub(2)
                        whisper(plr,v,msg)
                    end
                end
            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

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
    -- Code
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())] -- This basicly gets all the player objects in the game and chooses a random one based on the index
    end

    for index, plr in pairs(game.Players:GetPlayers()) do
        if plr.Name == PlrName then -- I do this because if there are 2 players with almost the exact name and someone only has like a number after it it won't select that one
            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.

Ad

Answer this question