• Getting player's name in chat
    8 replies, posted
Using the OnPlayerChat hook, is there a way to compare the message with all the current player's names and then bind the certain player ( the one that was said ) to a variable ( ply2, something like that ) ?
player:Nick() Get's their steam nickname player:SteamID() Get's their steam ID I like to use SteamIDs when binding a player to a feature because those are constant.
[QUOTE=LegoGuy;42201310]player:Nick() Get's their steam nickname player:SteamID() Get's their steam ID I like to use SteamIDs when binding a player to a feature because those are constant.[/QUOTE] Right. Is there a way to make it bind to a variable that points to a player ( somewhat reversed process ) Basically, I want to make it so that when someone says another player's name, the player who's name was said is bound to a variable, like ply2.
OnPlayerChat already passes a player. Look into function arguments.
[QUOTE=Chessnut;42204083]OnPlayerChat already passes a player. Look into function arguments.[/QUOTE] Yes, I know. It passes the player who chats. I want to get the text he said ( function argument ) and compare that to all player names abd if it's a match with any, set that player as a second variable.
You do it exactly as you are describing it, do you need help with comparing the text they entered with the other players names? You can get all the players using player.GetAll() You can loop through that using for ... in pairs(...) do If you want an exact comparison you can do ==, or for partial matches use string.find [lua] --Completely untested code, example only, may have errors local function FindPlayerByName( searchFor ) local found = {} --a table with all matching players for k,v in pairs(player.GetAll()) do --loop all players local pname = v:Nick() --get their name if string.find(pname, searchFor) then --their name contains the text we entered, add them to our found list table.insert(found, v) end end if #found==1 then --we only found 1 person return found[1] --return the person we found else --we found more than 1 matching person, dunno what to do now return false end end hook.Add("OnPlayerChat", function(ply,txt) local searchFor = txt --we are searching for what they entered. this could modified to be, for example, only the second word they typed, etc local targetPly = FindPlayerByName(searchFor) if targetPly then print("Found ", targetPly) else print("Couldn't match", searchFor, "to a player") end end) [/lua]
[QUOTE=wh1t3rabbit;42207104]You do it exactly as you are describing it, do you need help with comparing the text they entered with the other players names? You can get all the players using player.GetAll() You can loop through that using for ... in pairs(...) do If you want an exact comparison you can do ==, or for partial matches use string.find [lua] --Completely untested code, example only, may have errors local function FindPlayerByName( searchFor ) local found = {} --a table with all matching players for k,v in pairs(player.GetAll()) do --loop all players local pname = v:Nick() --get their name if string.find(pname, searchFor) then --their name contains the text we entered, add them to our found list table.insert(found, v) end end if #found==1 then --we only found 1 person return found[1] --return the person we found else --we found more than 1 matching person, dunno what to do now return false end end hook.Add("OnPlayerChat", function(ply,txt) local searchFor = txt --we are searching for what they entered. this could modified to be, for example, only the second word they typed, etc local targetPly = FindPlayerByName(searchFor) if targetPly then print("Found ", targetPly) else print("Couldn't match", searchFor, "to a player") end end) [/lua][/QUOTE] So, Found[1] would return only the player who's name was said?
Yes. Unless it found more players who said it. The number 1 signifies the first entry in the array.
[QUOTE=Flamingsword;42210276]Yes. Unless it found more players who said it. The number 1 signifies the first entry in the array.[/QUOTE] Thank you!
Sorry, you need to Log In to post a reply to this thread.