• Function returns nil, loop outside function returns normal var.
    8 replies, posted
Hi, could someone help me to figure why this function returns nil value? praca1check returns nil; loop outside function works well. https://files.facepunch.com/forum/upload/151637/85bc5197-82e9-454a-8d01-17b2e6260b4b/jeju.PNG
you should call the argument "name" something else. atleast not name. Because it probably conflicts with jobTable.name so the if statement isnt true, and it doesnt return anything, so its nil. So simply change the argument name to something else, the letter n or so
First off, you don't need 2 different functions if they're going to do the exact same thing. Just call pracaCheck with the two different names. Secondly, it is probably returning nil because its not finding any job names that match, so it gets all the way through for loop without returning anything, and your stuck with a variable you've declared but the function isn't populating it with anything. Most languages won't let you get this sort of unexpected behavior by forcing you to return _something_ after the loop has tried everything but Lua is... special.
Still returning nill.
This is incorrect, jobTable.name will not conflict with an argument just called name.
This loops returning good value outside function. I changed these two strings into correct names, and its still returning nil value.
Could you include that code as well?
The only way your functions will return nil is if the names aren't found. That means there is probably something wrong with the names you are passing into the functions. Look at it this way: function praca1check(name) for id, jobTable in pairs(RPExtraTeams) do if jobTable.name == name then return id end -- Name is found, we return the ID end -- If the function gets here, it means the name wasn't found -- Even though there is no return here, Lua functions still return nil end
Well, this works now. And i know i can have there one function but it's easier for me to have two. function praca1check(namepraca) for id, jobTable in pairs(RPExtraTeams) do  if jobTable.name == namepraca then  return id end  jedynka_wiezienie = id end end function praca2check(namepraca) for id, jobTable in pairs(RPExtraTeams) do  if jobTable.name == namepraca then  return id end  dwojka_wiezienie = id end end function ENT:Use(ply, caller) local random = math.random(1,2) local praca1 = praca1check("Hobo") local praca2 = praca2check("Medic")
Sorry, you need to Log In to post a reply to this thread.