Need help assigning a player's name to their player object

I have had an admin script for a while that I have constantly been frustrated with it’s lack of partial name support, so I tried to fix it. With the current script, I will return a string of the player’s name but I want it to return a player object. I don’t really know what to try.

local function findPlayer(name)
	local foundName = nil
	local Players = game.Players:GetPlayers()
	for i = 1, #Players do
		local PossiblePlayer = Players[i]
		if string.find(string.lower(PossiblePlayer.Name), string.lower(name)) then
			foundName = PossiblePlayer.Name
			
		end
	end
	
	if not foundName then
		return nil
	else
		return foundName
	end
end

Thank you to anyone that helps.

local playerInstance = game.Players[foundName]

1 Like