• string.find/match not working with v:Nick() players who have spaces in name
    3 replies, posted
Background: I am new to Gmod Networking, I decided to make a simple hook (PlayerSay) command that uses networking. This command is supposed to send pms to other players, Problem: My command works fine when the player you are trying to send the message to has no spaces involving their name but if someone joins with a name like "john foo" and you !pm john foo message, string.find(string.lower(text), string.lower(v:Nick())) will not find the players nickname sub-string within the text from the player, SIDE NOTE : I am also having trouble making the command work with ignoring case for the players name EX: "John" will work but "john" will not, one Possible solution I had was to parse the text from the player as lower and also parsing the v:Nick() as lower as you can see with my code snippet above and my code blocks below.,but it was to no avail. I have also tried using find.match() for the compare statement what player types: !pm name text Code: serverside autorun: util.AddNetworkString("PmCustomServer") hook.Add("PlayerSay", "pmsystemCustom", function(ply, text, boolteam) if(string.sub(string.lower(text), 1,3) == "!pm") then local isFound = false for k,v in pairs(player.GetAll()) do if string.find(string.lower(text), string.lower(v:Nick()), 3) then local plystart,plyend = string.find(text, v:Nick()) local message = string.sub(text, plyend+1) local sender = ply:Nick() net.Start("PmCustomServer") net.WriteString(message) net.WriteString(sender) net.Send(v) isFound = true ply:ChatPrint("pm sent to"..v:Nick()) return "" end end if isFound == false then ply:ChatPrint("player not found") return "" end end end) clientside autorun: net.Receive("PmCustomServer", function(len) local ply = LocalPlayer() local message = net.ReadString() local sender = net.ReadString() ply:ChatPrint("PM: "..sender..": "..message) end)
https://wiki.garrysmod.com/page/string/find Try setting the noPatterns parameter to true
I just tried this, it does not seem to be affecting spaced names, I'm wondering if I should have the user put " " around their name to simplify it or use the text the user puts in for the name, after !pm to check if that sub string is occurring in only 1 v:Nick() and use the closest one that it finds itself in, that sounds like a better abstraction than what I'm trying to do. i did just find out something about this script though.My second issue(SideNote) i was having Whenever I make a pm to a person, lets just say Bot01, with correct casing, it sends them the pm, but when i use the exact string of text, but change casing in any way ex(BOT01 || bot01) instead of chat printing "not a player", it is giving me error nil value for my plyend variable (because i am trying to preform logic on it when making the local message variable) local plystart,plyend = string.find(text, v:Nick()) I had to make this code this --->local plystart, plyend = string.find(string.lower(text), string.lower(v:Nick())) because my first selection statement was checking lower on the two strings(text and v:Nick()), i had to continue that to my other selection statements otherwise it wouldn't find the same correlation because it only worked on the strings when lower was applied, this fixed my case sensitive issue, not the one i was addressing in the main forum post, I thought i would share this solution I found for my SideNote.
local plystart,plyend = string.find(text, v:Nick()) i jumped the gun, firstly you are not lowering the strings here
Sorry, you need to Log In to post a reply to this thread.