Last active
September 14, 2021 23:30
-
-
Save Nexxed/e3041137da591a7a791a6db555fcd1f3 to your computer and use it in GitHub Desktop.
[FiveM] Search players based on string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- search param must be a string thats either #<server_id> or a partial player name | |
-- e.g SearchPlayers("#1361") or SearchPlayers("test") | |
-- compatible with command arguments | |
-- 2nd example above will match "testuser", "userTest", "TeSt", "TEST" etc | |
-- server-side only, you *could* modify it for client-side rather easily, though | |
local function SearchPlayers(search) | |
-- initialize our return table | |
local ret = {} | |
-- loop through each player | |
for _, player in ipairs(GetPlayers()) do | |
-- if player isn't nil, then continue (sanity-check) | |
if (player ~= nil) then | |
-- get the players name | |
local player_name = GetPlayerName(player) | |
-- check if the search term is for a Server ID (prefixed with a #) or for a partial name match | |
if(string.sub(search, 1, 1) == "#") then | |
-- extract the player id from the search term | |
local searchId = string.sub(search, 2) | |
-- if the current player in the loop has the same ID as the one given in the search | |
-- then insert the player into the return object | |
if (player == searchId) then | |
table.insert(ret, player) | |
break | |
end | |
elseif (string.match(player_name:lower(), search:lower())) then | |
table.insert(ret, tonumber(player)) | |
end | |
end | |
end | |
return ret | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment