• Is it possible to turn a string into userdata?
    6 replies, posted
Say I have a string of a players nickname, for instance mine. CallMePyro. How would I turn that into userdata to slay or kick or whatever? As always, help is very much appreciated.
Loop ( for id,ply in pairs(table) do ) through all players (player.GetAll) and compare each name to your string using string.find(ply:Name(), mystring).
edit: -snip-
[LUA] local function GetPlayersByNick( nick ) local FoundPlayers = {} for k,v in pairs( player.GetAll() ) do if v:Nick() == nick then table.insert( FoundPlayers, v ) end end return FoundPlayers end local function KillPlayerName( name ) local FoundPlayers = GetPlayersByNick( name ) if #FoundPlayers == 1 then FoundPlayers[1]:Kill() else print( "found more than one player, choosing first" ) FoundPlayers[1]:Kill() end end [/LUA] written quickly, should do
[QUOTE=rejax;42582768][LUA] local function GetPlayersByNick( nick ) local FoundPlayers = {} for k,v in pairs( player.GetAll() ) do if v:Nick() == nick then table.insert( FoundPlayers, v ) end end return FoundPlayers end local function KillPlayerName( name ) local FoundPlayers = GetPlayersByNick( name ) if #FoundPlayers == 1 then FoundPlayers[1]:Kill() else print( "found more than one player, choosing first" ) FoundPlayers[1]:Kill() end end [/LUA] written quickly, should do[/QUOTE] I actually ended up doing something pretty similar to that. Thank you!
If you're writing chat or console admin commands it's better to use multiple identifiers instead of relying on nick matching, in case you have griefers with identical SF names: [lua] local Util = UP.Util local player = player local string = string -- Find a player by user ID; returns one exact match or nothing function Util.FindByUserID(num) num = tonumber(num) if not num then return {} end for k, v in pairs(player.GetAll()) do if v:UserID() == num then return {v} end end return {} end -- Find a player by SteamID; can return multiple partial matches function Util.FindBySteamID(str) local rec, R = {}, 1 for k, v in pairs(player.GetAll()) do -- equality always has priority if v:SteamID() == str then return {v} elseif string.find(v:SteamID(), str, nil, true) then rec[R], R = v, R + 1 end end return rec end -- Find a player by nickname; can return multiple partial matches function Util.FindByNick(str) local rec, R = {}, 1 for k, v in pairs(player.GetAll()) do -- equality always has priority if v:Nick() == str then return {v} elseif string.find(v:Nick(), str, nil, true) then rec[R], R = v, R + 1 end end return rec end -- Try to find a player or players matching a given identifier -- Valid identifiers: "#userid", "$steamid", "*" <player you're looking at>, "nickname" function Util.FindPlayer(str, ply) local pref = string.sub(str, 1, 1) -- UserID if pref == "#" then return Util.FindByUserID(string.sub(str, 2)) -- SteamID elseif pref == "$" then return Util.FindBySteamID(string.sub(str, 2)) -- Eyetrace elseif str == "*" then local ent = ply:GetEyeTrace().Entity if IsValid(ent) and ent:IsPlayer() then return {ent} end return {} -- We assume anything else is a nick else return Util.FindByNick(str) end end [/lua]
edit: -snip-
Sorry, you need to Log In to post a reply to this thread.