Skip to content

Instantly share code, notes, and snippets.

@Nexxed
Last active September 14, 2021 23:30
Show Gist options
  • Save Nexxed/e3041137da591a7a791a6db555fcd1f3 to your computer and use it in GitHub Desktop.
Save Nexxed/e3041137da591a7a791a6db555fcd1f3 to your computer and use it in GitHub Desktop.
[FiveM] Search players based on string
-- 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