Jump to content

[HELP] id system


Recommended Posts

Hello, i want find player with similar names and print names in a list !

My find player function :

function findPlayer( partofname )
    local player = getPlayerFromName ( tostring(partofname) )
    if player then
        return player
    end
	for _, player in pairs( getElementsByType 'player' ) do
		if tonumber(getElementData(player,"id")) == tonumber(partofname) then
			if getElementData(player, "loggedIn") == true then
				return player
            end
        end
    end
		
	for _, player in pairs( getElementsByType 'player' ) do
		if string.find ( string.gsub ( getPlayerName ( player ):lower( ), "#%x%x%x%x%x%x", "" ), partofname:lower( ), 1, true ) then
			if getElementData(player, "loggedIn") == true then
				return player
			end
		end
    end
end

Do you have an idea or opinion ?

Edited by xXGhostXx
Link to post
1 minute ago, nikitafloy said:

Use regulars:


string.match

For example

No i want when use this function player name field if several similar names are found, they should be in the table.

Link to post

Just insert the player's element into a table instead of return it immediately. And return this table at the end.

-- SHARED

function findPlayers(part)
    local found   = {}
    local players = getElementsByType("player")

    for i = 1, #players do
        local player = players[i]

        if tonumber(part) and getElementData(player, "id") == tonumber(part) and getElementData(player, "loggedIn") then
            table.insert(found, player)

        else
            local part = tostring(part):lower()
            local name = getPlayerName(player):lower():gsub("#%x%x%x%x%x%x", "")
            
            if name:find(part) then
                table.insert(found, player)
            end
        end
    end
	
    return found
end

 

Edited by Patrick
  • Like 1
Link to post
1 hour ago, Patrick said:

Just insert the player's element into a table instead of return it immediately. And return this table at the end.


-- SHARED

function findPlayers(part)
    local found   = {}
    local players = getElementsByType("player")

    for i = 1, #players do
        local player = players[i]

        if tonumber(part) and getElementData(player, "id") == tonumber(part) and getElementData(player, "loggedIn") then
            table.insert(found, player)

        else
            local part = tostring(part):lower()
            local name = getPlayerName(player):lower():gsub("#%x%x%x%x%x%x", "")
            
            if name:find(part) then
                table.insert(found, player)
            end
        end
    end
	
    return found
end

 

getElementData(player, 'id') can be nil

Link to post

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...