title: Lua script error when run from stackexchange.redis
body: <p>We have a lengthy lua script. Please find it below after the problem description.</p>
  
  <p>The logic of the script is to return true/false if all the 3 parameters match (UserName, UserCategory, UserCategoryItem)</p>
  
  <p>The first part of the script splits the RedisKeys to fetch the UserName, UserCategory, UserCategoryItem.  There is also a random function to test if ‘functions’ works in the Redis lua script. It works.</p>
  
  <p>The second part of the script compares this values against the values in the Database. The values are hard-coded in the script. If all the values match it returns true else it returns false.</p>
  
  <p>The two scripts run perfect when ran individually. But they throw an error when run together.</p>
  
  <p><strong>ISSUE/PROBLEM</strong></p>
  
  <p>All the three scripts run perfectly when run in an IDE. I use the SciTE IDE (an IDE for Lua for Windows envrionment)</p>
  
  <p>The script creates an issue when  run from the Stackexchange.Redis client. </p>
  
  <p>The first two parts of the script (Part One and Part Two) run perfectly from the Stackexchange.Redis. It is only when they are combined in the third script (Part Three) does it throw an error. 
  As the error is generic in nature hence I am not able to further investigate the issue.</p>
  
  <p>I have divided the script in Three parts. The final script combines the first two scripts.</p>
  
  <pre><code>var redis = ConnectionMultiplexer.Connect("localhost");
  var db = redis.GetDatabase();
  
  //PART ONE OF THE SCRIPT
  
              var doesExist1 = (string)db.ScriptEvaluate(@" 
   local function isDateInRange(DateStart, DateEnd, GivenDate)
      if (GivenDate &gt; DateStart and GivenDate &lt; DateEnd)
      then
          return(true)
      else
          return(false)
      end
  end
  
  --Fetch User's Category's StartDate and EndDate
  local function IsCategoryDateValid()
  local givenDate, DateStart, DateEnd
  
  GivenDate = '2017-01-09'
  StartDate = '2015-01-01'
  EndDate = '2018-01-01'
  
  local isDateValid
  isDateValid = isDateInRange(StartDate, EndDate, GivenDate)
  return(isDateValid)
  end
  
  -- Pass User, UserCategory and UserCategoryItem as parameters
  local userNameKey = 'UserDetails:DummyName'
  local userCategoryKey = 'UserCategories:Book'
  local userCategoryItemKey = 'UserCategoryItems:Harry-Potter'
  
  local userNameKeySplit = {}
  local a = 1
  for i in string.gmatch(userNameKey, '([^'..':'..']+)') do
    userNameKeySplit[a] = i
    a = a + 1
  end
  local userName = userNameKeySplit[2]
  
  
  local userCategoryKeySplit = {}
  local b = 1
  for j in string.gmatch(userCategoryKey, '([^'..':'..']+)') do
    userCategoryKeySplit[b] = j
    b = b + 1
  end
  local userCategory = userCategoryKeySplit[2]
  
  
  
  local userCategoryItemKeySplit = {}
  local c = 1
  for k in string.gmatch(userCategoryItemKey, '([^'..':'..']+)') do
    userCategoryItemKeySplit[c] = k
    c = c + 1
  end
  local userCategoryItem = userCategoryItemKeySplit[2]
  
  return(userName..' '..userCategory..' '..userCategoryItem)
  
  ",
  
                  new RedisKey[] { "UserCategoryItemNames:" + userModel.UserId });
  
  
  //PART TWO OF THE SCRIPT
  
              var doesExist2 = (bool)db.ScriptEvaluate(@" 
  local userName = 'DummyName'
  local userCategory = 'Book'
  local userCategoryItem = 'Harry-Potter'
  
  -- Fetch Users, UserCategories and UserCategoryItems from the Redis DB
  local userData = {'DummyName', 'User1', 'User2', 'User3'}
  local userCategoryData = {'Book', 'Movie', 'Journals'}
  local userCategoryItemData = {'Hardy-Boys', 'Harry-Potter', 'Shawshank-Redemption', 'The-Terminal'}
  
  local msg
  -- Loop through the fetched values and compare them with parameters; if all the parameters are found return true else return false
  for i=1,#userData,1
          do
              if(userData[i] == userName)
              then
                  for i=1,#userCategoryData,1
                  do
                      if(userCategoryData[i] == userCategory)
                      then
                          for i=1,#userCategoryItemData,1
                          do
                              if(userCategoryItemData[i] == userCategoryItem)
                              then
                                  msg = true
                              break
                              else
                                  msg = false
                              end
                          end
                      break
                      else
                          msg = false
                      end
                  end
              break
              else
                  msg = false
                  break
              end
          end
  
  
  
  return(msg)
  
  //PART ONE &amp; TWO COMBINED OF THE SCRIPT
  
                                          ",
  
                  new RedisKey[] { "UserDetails:" + "DummyName", "UserCategories:" + "Book", "UserCategoryItems:" + "Harry-Potter" });
  
              var doesExist3 = (bool)db.ScriptEvaluate(@" 
  
  local function isDateInRange(DateStart, DateEnd, GivenDate)
      if (GivenDate &gt; DateStart and GivenDate &lt; DateEnd)
      then
          return(true)
      else
          return(false)
      end
  end
  
  --Fetch User's Category's StartDate and EndDate
  local function IsCategoryDateValid()
  local givenDate, DateStart, DateEnd
  
  GivenDate = '2017-01-09'
  StartDate = '2015-01-01'
  EndDate = '2018-01-01'
  
  local isDateValid
  isDateValid = isDateInRange(StartDate, EndDate, GivenDate)
  return(isDateValid)
  end
  
  -- Pass User, UserCategory and UserCategoryItem as parameters
  local userNameKey = 'UserDetails:DummyName'
  local userCategoryKey = 'UserCategories:Book'
  local userCategoryItemKey = 'UserCategoryItems:Harry-Potter'
  
  local userNameKeySplit = {}
  local a = 1
  for i in string.gmatch(userNameKey, '([^'..':'..']+)') do
    userNameKeySplit[a] = i
    a = a + 1
  end
  local userName = userNameKeySplit[2]
  
  
  local userCategoryKeySplit = {}
  local b = 1
  for j in string.gmatch(userCategoryKey, '([^'..':'..']+)') do
    userCategoryKeySplit[b] = j
    b = b + 1
  end
  local userCategory = userCategoryKeySplit[2]
  
  
  
  local userCategoryItemKeySplit = {}
  local c = 1
  for k in string.gmatch(userCategoryItemKey, '([^'..':'..']+)') do
    userCategoryItemKeySplit[c] = k
    c = c + 1
  end
  local userCategoryItem = userCategoryItemKeySplit[2]
  
  -- Fetch Users, UserCategories and UserCategoryItems from the Redis DB
  local userData = {'DummyName', 'User1', 'User2', 'User3'}
  local userCategoryData = {'Book', 'Movie', 'Journals'}
  local userCategoryItemData = {'Hardy-Boys', 'Harry-Potter', 'Shawshank-Redemption', 'The-Terminal'}
  
  local msg
  -- Loop through the fetched values and compare them with parameters; if all the parameters are found return true else return false
  for i=1,#userData,1
          do
              if(userData[i] == userName)
              then
                  for i=1,#userCategoryData,1
                  do
                      if(userCategoryData[i] == userCategory)
                      then
                          for i=1,#userCategoryItemData,1
                          do
                              if(userCategoryItemData[i] == userCategoryItem)
                              then
                                  msg = true
                              break
                              else
                                  msg = false
                              end
                          end
                      break
                      else
                          msg = false
                      end
                  end
              break
              else
                  msg = false
                  break
              end
          end
  
  
  
  return(msg)
  
                                          ",
  
                  new RedisKey[] { "UserDetails:" + "DummyName", "UserCategories:" + "Book", "UserCategoryItems:" + "Harry-Potter" });
  </code></pre>
  
  <p>Thank you for your patience and reading this much. I hope I have clarified the problem in detail. Please provide some help in solving the issue. Thank you.</p>