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

How can I find a player with just a couple letters of the username?

Asked by 9 years ago

I need to create a pm script that returns a player from a for i, v in pairs(table) loop Example: /pm Gui;How are you doing. It will make a box appear on GuiHelperSince2014's screen with How are you doing typed on it. The following code explains what my unfinished code is. (It's unfinished because I can not think of what to do)

for i, v in pairs(game.Players:GetPlayers()) do
                    if string.lower(string.find(string.sub(msg, 5,string.find(msg, ";") - 1))) then
                        CreatePMMessage(plr, v, msg, string.sub(msg, string.find(msg, ";") + 1))
                    end
                end

The code is a chat script so plr is the plr that is added and msg is the message that the player chats. And CreatePMMessage is a function. The following code is the syntax of the function

CreatePMMessage(plr, plr2, msg, message)

plr is the sender, plr2 is the target, msg is for it to work (for some odd reason it needs this), and message is the pm message

1 answer

Log in to vote
3
Answered by
ImageLabel 1541 Moderation Voter
9 years ago

You could simplify your work by creating a function that takes care of analyzing all of that string matching. In your function, iterate through the Players service in search for a player with a matching name, and then return the corresponding player.

I'm using the find function of the string library to check for any matching sequences in names of players and the passed argument.

also, using string.upper, I am ensuring that the analyzing isn't case sensitive.


local getPlayer = function(playerName)
    local players = game.Players:GetPlayers()

    for index = 1, #players do
        local name = players[index].Name:upper()

        if name:find(playerName:upper()) then
            return players[index] -- returns player at `index`
        end
    end
end

Finally, based on the results of the calling of function getPlayer, you could display the "personal message".

0
Okay, thank you, i forgot my email for my main account (Z9R) That I put on here so I use my other account to ask questions GuiHelperSince2014 70 — 9y
0
Yep just what I wanted, Thank you very much! GuiHelperSince2014 70 — 9y
0
you're welcome ImageLabel 1541 — 9y
Ad

Answer this question