Need help with finding player on test mode

I need help with finding player in local test. I have the script that can find player only by part of its name. When I launch the local test with 2 players and run script with “Player2” it just returns nil. The script:

function getPlayer(plr)
	if plr then
		local lowerName = string.lower(plr)
		
		for _, player in pairs(players:GetPlayers()) do
			if lowerName == string.sub(string.lower(player.Name), 1, #lowerName) then
				return player
			end
			break
		end
	end
end

plr variable is a string
Thanks in advance

Not sure if this is what you mean but:

for i, Plr in pairs(game.Players:GetPlayers()) do -- gets the Players

-- code here
end

If you want a Table do:

pairs({game.Players:GetPlayers()})

--or this:
Table = {}
table.insert(Table, Plr)

(Nope lol)

Does it work with Player1? What does it return? Have you tried printing lowerName and the result of string.sub(string.lower(player.Name), 1, #lowerName)? Print debugging works 90% of the time.

1 Like

break stops the loop since it’s outside the if statement, causing it to only check one player, simply remove the break and the function will work fine and return already stops the function so no need to worry about that.

function getPlayer(plr)
	if plr then
		local lowerName = string.lower(plr)

		for _, player in pairs(players:GetPlayers()) do
			if lowerName == string.sub(string.lower(player.Name), 1, #lowerName) then
				return player
			end
		end
	end
end

image

2 Likes

Yes, when i type player1 it works