• Comparing two strings that are the same returns false
    3 replies, posted
Hello. I am currently working on a votemap command and came across a possible lua error? I am comparing two strings, one is from the maps file and one is put in by the user. The code is supposed to check if the map the user put in is available or not, however when comparing the two strings it always returns false. This is the code for the command: hook.Add("PlayerSay","votemaptest",function(ply,Str)     local MSG = string.Split(Str," ")     if MSG[1] ~= nil then MSG[1] = string.lower(MSG[1]) end     if MSG[1] == "!votemap" or MSG[1] == "/votemap" or MSG[1] == ".votemap" or MSG[1] == "-votemap" then         if MSG[2] ~= nil then             local Fr = file.Read("map config/1 votemap maps.txt","LUA")             if Fr ~= nil then                 local FoundMap = 0                 Fr = string.Split(Fr,"\n")                 for k,v in pairs(Fr) do                     chat.BroadcastMSG("List: '"..string.lower(v).."' | UserInput: '"..string.lower(MSG[2]).."' > "..tostring(string.lower(v) == string.lower(MSG[2])))                     chat.BroadcastMSG("List: is a.. "..type(v).." UserInput: is a.. "..type(MSG[2]))                     if string.lower(v) == string.lower(MSG[2]) then                         FoundMap = 1; break                     end                 end                                  if FoundMap == 0 then                     ArgumentError(ply,"Invalid argument (2): The map you provided is not available"); return false                 else                     VoteMap(MSG[2]); return true                 end             end         else             ArgumentError(ply,"Missing argument (2): Map name"); return false         end     end end) This is the content of the maps file: rp_industrial17_v1 gm_flatgrass gm_construct Test1 Test2 Test3 Test4 Test5 Test6 Test7 Test8 Test9 Test10 When entering the command "!votemap gm_flatgrass" this is what it returns: https://cdn.discordapp.com/attachments/440241086980685824/460824036461510666/unknown.png This is the string.Split table: https://cdn.discordapp.com/attachments/440241086980685824/460824824948719636/unknown.png I never had this issue before and i can't figure out what i am doing wrong here.
The strings must be different in some way. Try using string.byte to inspect the values of each character of the string to see where they differentiate. string.match will have some false positives if you have something like string.match("gm_flatgrass_final2", "gm_flatgrass")
Yeah i noticed that this method can have false positives, i changed the code to this: local Fr = file.Read("map config/1 votemap maps.txt","LUA") if string.match(Fr,"\""..string.lower(MSG[2]).."\"",1) then --Votemap stuff end I have put the map names in the maps.txt in " characters to ensure there are no false positives, it also prevents inputs like 'm_flat'
My bet is that MSG[2] or v got the whole filename. As in: "gm_flatgrass" == "gm_flatgrass.bsp" You can remove it with something like: local mapname = MSG[2] --If mapname ends with .bsp if string.match(mapname,".+.bsp$") then --Remove the last 4 letters/symbols mapname = string.sub(mapname,1,string.len(mapname)- 4) end
Sorry, you need to Log In to post a reply to this thread.