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.
To accomplish this, you would the string.sub
function. sub
stands for substring. Here's a little explanation by Roblox about this function:
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)