Can you string.lower a player if you're doing game:GetService("Players"):FindFirstChild(name)

Terribly explained but I’m working on a report user system which relies on checking if the user from the first field is actually ingame, bit of it would be this

elseif not game:GetService("Players"):FindFirstChild(fieldOneInput) then
			pcall(fieldOneFail)

it does fieldOneFail even though the user is ingame, but the name is just different lower/upper case, how woud I make it so it doesn’t matter whether it’s upper or lower case so you don’t have to be exact for it?

1 Like

You can create an array with all of the players names in it which have been converted to lower case, and then search through that table to see if theres any matches using either table.find or your own function.

1 Like

I mean I have 0 idea how I can actually do that as I’m relatively new to scripting so is there anything else I could do

1 Like
for i, v in pairs(game.Players:GetPlayers()) do
    if string.lower(v.Name) == string.lower(input) then
        --do something
    end
end
2 Likes

I’ve tried that already and it doesn’t work because if it finds 1 username that it isn’t equal to then it will just display the error

1 Like

How? it should not print any error if it didn’t find anything.

1 Like

I don’t think you understand what I mean then

1 Like

How did you even try this? Send the code you tried.

What you would want to do is create a variable called Found, and then loop through the players and if found, set it to true (or the player that was found). Then after the loop is done, check if the variable is true, if not, then do the fail thing.

Like so:

local found = false
for k, v in pairs(game.Players:GetPlayers()) do
    if v.Name:lower() == input:lower() then
		found = v
		break
    end
end
if not found then
	pcall(fieldOneFail)
end
2 Likes

You must emit the error after the for loop.
What I think you are doing:

for i, v in pairs(game.Players:GetPlayers()) do
    if string.lower(v.Name) == string.lower(input) then
        --do something
    else
        error("") -- Here
    end
end

What you must do:

for i, v in pairs(game.Players:GetPlayers()) do
    if string.lower(v.Name) == string.lower(input) then
        --do something
        return
    end
end
error("") -- Here

What? This code will always give a error.

1 Like

Yes that’s true. Before, I forgot to put a return statement inside the if-check, I now edited it.

Hope this helps:

local Players = game:GetService("Players")

local fieldOneInput = "PlayerName"

function short(array)
	local result = {table.unpack(array)} --copy the array
	table.sort(result, function(a, b)
		return a:len() > b:len()
	end)
	return result 
end

function getPlayers(text)
	local results = {}
	for i, player in pairs(Players:GetPlayers()) do 
		if string.find(player.Name:lower(), text:lower()) then 
			table.insert(results, player.Name)
		end
	end
	return results 
end

local found = getPlayers(fieldOneInput)

for i, v in pairs(found) do 
	print(v)
end

--short actual names based on their length
local shorted = short(found)

local best_matching = shorted[1]
local less_matching = shorted[#shorted]

print(best_matching, less_matching)
for _, player in ipairs(game.Players:GetPlayers()) do
	if string.lower(player.Name) == "player1" then
		--do action
	end
end

You can also use lower as a method with the colon operator.

for _, player in ipairs(game.Players:GetPlayers()) do
	if player.Name:lower() == "player1" then
		--do action
	end
end
2 Likes