Basically I want to create a function that returns a players full name with correct capitilization from any form of captilization and shortened names.
Ex.
Current code that I have:
local fetchUsername = function() for _,v in next, Players:GetPlayers() do local decode = string.match((v.Name):lower(), RawUsername .. "%w+") return Players[decode].Name end end
RawUsername is just a string given to the function from a variable, i.e a players name. What I wanted it to do was return the players name correctly in perfect capitilization. Could anyone help me out here?
Use string.lower()
on both the player's name and the argument to make them both in lowercase, which will make it ignore capitalization. Btw you don't have to use string.match()
, you can use string.sub()
, it's easier.
local function FetchUsername(name) for i,v in pairs(game:GetService"Players""GetPlayers") do if v.Name:lower():sub(1, #name) == name:lower() then return v end end end
Or if you want it to return a table with all found players, do this:
local function FetchUsername(name) local t = {} for i,v in pairs(game:GetService"Players""GetPlayers") do if v.Name:lower():sub(1, #name) == name:lower() then t[#t + 1] = v --much like table.insert end end return t end
Here it's broken down to step by step, in case you don't understand this strange magic:
local function FetchUsername(name) for i,v in pairs(game:GetService"Players""GetPlayers") do local plrNameLowercase = v.Name:lower() --player's name in lowercase local nameLowercase = name:lower() --name we're searching for in lowercase if plrNameLowercase:sub(1, #nameLowercase) == nameLowercase then --if first x characters of the player's name are the same as nameLowercase, where x is the length of nameLowercase, so i.e. if player name is gmatchOnRoblox and name is gmat, it checks if first 4 characters are "gmat" return v end end end