GetPlayerFromPartialName

From Onset Developer Wiki
GetPlayerFromPartialName

Type: Lua Helper
Context: Server
Introduced: vWumbaloo

Description

Get player from a partial name.

Syntax

GetPlayerFromPartialName(player)

Parameters

  • player
    The player identifier.

Return Value

  • Returns the player identifier if found. Returns nil otherwise.

Code

function GetPlayerFromPartialName(name)
    local name = name and name:gsub("#%x%x%x%x%x%x", ""):lower() or nil
    if name then
        for _, player in ipairs(GetAllPlayers()) do
            local playerName = GetPlayerName(player):gsub("#%x%x%x%x%x%x", ""):lower()
            if playerName:find(name, 1, true) then
                return player
            end
        end
    end
end

Example Server

function GetPlayerFromPartialName(name)
    local name = name and name:gsub("#%x%x%x%x%x%x", ""):lower() or nil
    if name then
        for _, player in ipairs(GetAllPlayers()) do
            local playerName = GetPlayerName(player):gsub("#%x%x%x%x%x%x", ""):lower()
            if playerName:find(name, 1, true) then
                return player
            end
        end
    end
end

AddCommand("findplayer", function (player, _name)
	local otherPlayer = GetPlayerFromPartialName(_name) or "not found"
	AddPlayerChat(player, "Player " .. _name .. " is " .. tostring(otherPlayer))
end)

See also