2
votes

This is the code:

local function scanwhite (str, pos)
  while true do
    pos = strfind (str, "%S", pos)
    if not pos then return nil end
    local sub2 = strsub (str, pos, pos + 1)
    if sub2 == "\239\187" and strsub (str, pos + 2, pos + 2) == "\191" then
      -- UTF-8 Byte Order Mark
      pos = pos + 3
    elseif sub2 == "//" then
      pos = strfind (str, "[\n\r]", pos + 2)
      if not pos then return nil end
    elseif sub2 == "/*" then
      pos = strfind (str, "*/", pos + 2)
      if not pos then return nil end
      pos = pos + 2
    else
      return pos
    end
  end
end

I'm not an experienced "scripter" so i can't really figure out how to solve this.

1
How do you invoke scanwhite ? - Egor Skriptunoff
I mean this is a downloaded script that i messed around with a little bit then it told me where the error was. pastebin.com/qbkmQaqx There's the whole script. error is at row 397 - Emil Björkman
How do you invoke json.decode? Give usage example so that we could reproduce the error. BTW, there exist other JSON libraries. - Egor Skriptunoff

1 Answers

0
votes

Bottom line up front:

  • Problem: 'str' in scanwhite(str, pos) is nil
  • Why?
    • You may have written scanwhite() instead of scanwhite("something goes here")
    • You may have passed an empty variable instead of a variable containing a string

Detailed answer

From the lua 2.4 manual:

strfind (str, substr, [init, [end]])
  • The square brackets mean optional, the ones outside square brackets are mandatory.
  • Your error message says "string expected, got nil" so a mandatory one was empty
  • In your code, substr is clearly not empty (it is "%S")
  • By deduction, str was the nil value

Solution:

local myString = "This is a string"
scanwhite(mystring)