Jump to content

getPlayersFromPartOfName


Recommended Posts

Hello, I'm trying to make a function that would return a table with players that contain a letter or pattern or whatever in their name. I've "inspired" myself from this: https://wiki.multitheftauto.com/wiki/GetPlayerFromPartialName , however that only returns one player and I want a table with multiple players.

Here's what I've done on serverside:

-- serverside

function getPlayersFromPartOfName(name) 
	if name then
		local playerlist = {}
		local checkplayer = getPlayerFromName(name)
		if checkplayer then
			local checkname = getPlayerName(checkplayer)
			if checkname == name then
				table.insert(playerlist, checkplayer)
			end
		else
			for i, player in ipairs(getElementsByType("player")) do 
				if string.find(getPlayerName(player):lower(), tostring(name):lower(), 1, true) then 
					table.insert(playerlist, player)
				end
			end 
		end
		return playerlist
	end
	return nil
end 


function getFullName (source, cmd, thename) -- for testing
	if thename then
		local playermatchtable = getPlayersFromPartOfName(thename)
		for i, v in ipairs(playermatchtable) do
			outputChatBox(getPlayerName(v), source)
		end
	end
end

addCommandHandler("name", getFullName, false, false)

 

The function "getPlayersFromPartOfName" is supposed to return a table with all the players that contain the specified argument. If the specified pattern matches the player name then the table should only contain that player. However, there is a problem:

When the pattern matches the name of the player but does not match the case of the name (i.e unchiudawg ~= UnchiuDawg), the table will be returned empty.

Here's an example: There are 2 players online, Player and Playertwo.

/name Player will output in the chatbox "Player", which is correct.

/name Playertwo will output in the chatbox "Playertwo", which is correct.

/name pla (or whatever pattern that exists in both names) will output in the chatbox "Player" and "Playertwo", which is correct

/name player (or playertwo) won't output anything. It should output "Player" and "Playertwo" if the argument is player, and just "Playertwo" if the argument is playertwo. But it's not doing it.

Why is that happening?

Thank you for your help.

Edited by UnchiuDawg
typo
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...