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

How would I find a players username without having to worry about caps?

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.

  • Player named gmatchOnRoblox
  • I'd give a function GmAtch
  • It'd return gmatchOnRoblox

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?

2 comments

0

I am confused what this is supposed to do.... You want a name that has correct capitalization... But, all the players' names have correct capitalizations. What are you trying to do, it doesn't make sense.

0

Okay lets say the player types your name like this "KiNglone". I want the function to return "KingLoneCat". Does that clear things up?

1 answer

✓ Accepted answer

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

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