Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Find a player with only a part of their username?

I am attempting to find a player when only typing in a part of their username in chat, however what I have managed to do is print all the players names if their name had a 'h' in it, if I typed the letter 'h', no matter where in the name it is. I am trying to make it so it reads from the start of the name, not just look for if the name has that specific string anywhere in it.

plr.Chatted:Connect(function(msg)
	for _, v in pairs(game.Players:GetPlayers()) do
		if v.Name:lower():find(msg:lower(), 1, true) then
			print(v.Name)
		end
	end
end)

Any help is appreciated, thanks.

1 answer

✓ Accepted answer

To accomplish this, you would the string.sub function. sub stands for substring. Here's a little explanation by Roblox about this function:

#####"Returns the substring of s that starts at i and continues until j; i and j may be negative. If j is absent, then it is assumed to be equal to the length of s. In particular, the call string.sub(s,1,j) returns s until as many characters as j, and string.sub(s, -i) returns a suffix of s with length i."

plr.Chatted:Connect(function(msg)
    local Msg = msg:lower()

    for _, v in pairs(game.Players:GetPlayers()) do

        if Msg == v.Name:lower():sub(1, #Msg) then
            print(v.Name)
        end
    end
end)

4 comments

0

Msg:sub(1) That's redundant: It's the same as without using :sub(1). Also, why not put line 3 before the loop?

0

i forgot lol, and what is wrong with me

0

Probably the wheather. lol

0

Worked perfectly, thank you.

This discussion is preserved as it was. New answers, comments, and votes are closed.