✓ Accepted answer
##Problem
What you're trying to do with your script is compare the length of a user's name to an incomplete string of the target. What I am saying is when you target a person, let's say Person299, and you type in pers then the script reads it as you're comparing 'person299' == 'pers '. The script is having to compare the name to a partial name with spaces.
##Solution
Do the opposite. Essentially what you want to do is compare the partial name of input to the partial name of the physical player. Also, you can use the functions :sub() and :len() on a string and you will get the exact same results as string.sub() and string.len()
##Final Script
####By the way, I modified the function to return the physical player if existent that way you may find it easier. You would need to show a modified script if you want to target multiple players in a single string. However, if you do not like this change I will leave a fixed version of the original for use.
function GetPlayer(PartialName)
for _,v in pairs(game:GetService('Players'):GetPlayers()) do
if v.Name:lower():sub(1,PartialName:len()) == PartialName:lower() then
return v
end
end
return nil
end
Original script:
function find(short)
local plyr = "invalid"
for _,v in pairs(game.Players:GetPlayers()) do
if string.lower(string.sub(v.Name,1,length)) == string.sub(short,1,length) then
plyr = v.Name
end
end
return plyr
end
######Hopefully this answered your question. If so, do not forget to hit the accept answer button. If you have any questions feel free to comment below.