This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
austinv11's profile picture

Getting "unable To Concatenate A Nil With A String"

Started by austinv11, 30 September 2013 - 07:19 PM
austinv11 #1
Posted 30 September 2013 - 09:19 PM
Please help, it has to do with the variable "playerName"

Here's the code

local DOOR_OPEN_DURATION = 2.5

local hour = os.time()

p = peripheral.wrap("right")

local doorSide = "top"
local logFilePath = "/doorOpenLog"

local function welcome()
  if playerName ~=  "Grand_Panda" or "MajorCooke" or "austinv11" then
    p.say("Welcome "..playerName.." to AustinCorp11")
	
  elseif playerName == "Grand_Panda" then
    p.say("Hey Panda, welcome to AustinCorp11 and have some Bamboo!")
  
  elseif playerName == "MajorCooke" then
    p.say("Salutes to the Major Cookie!")
  
  elseif playerName == "austinv11" then
    p.say("Welcome the owner of AustinCorp11!")
    sleep(0.5)
    p.say("*Claps")
    end
  end

local function logPlayerName (logFilePath, playerName)
  local fileHandle = fs.open ("/doorOpenLog", "a")

  if fileHandle then
    fileHandle.writeLine(playerName.." at "..hour)
    fileHandle.close()
  
    return true
  
    end
  
  return false
  end

local function openDoor()
  rs.setBundledOutput(doorSide, 0)
  sleep(DOOR_OPEN_DURATION)
  rs.setBundledOutput(doorSide, 1)
end

local function main (logFilePath)
  while true do
    local _, playerName = os.pullEvent("player")
  
    openDoor() 
    logPlayerName(logFilePath, playerName)
    welcome()
    p.say("Remember to finish the welcome() function!")
    end
  end

rs.setBundledOutput(doorSide, 1)
main()

Any response is appreciated, thanks!
willwac #2
Posted 30 September 2013 - 09:21 PM
At what line are you getting this error?
austinv11 #3
Posted 30 September 2013 - 09:23 PM
line 12, but it doesn't recognize my username (austinv11) and use my own specific welcome
willwac #4
Posted 30 September 2013 - 09:28 PM
line 12, but it doesn't recognize my username (austinv11) and use my own specific welcome

Are you using MiscPeripherals Player Detector?
Did you try making the program in the lua shell?

Just a post of mine
http://www.computercraft.info/forums2/index.php?/topic/15445-using-g-how-to-use-varibles/
campicus #5
Posted 30 September 2013 - 11:09 PM
It looks like it isn't setting any value to "playerName", which means that line 12 isn't the problem, this is:


local _, playerName = os.pullEvent("player")

I don't know anything about player detection though sorry

EDIT: Try defining the playerName outside of the function (at the beginning of your program)
Inumel #6
Posted 01 October 2013 - 12:06 AM

  if playerName ~=  "Grand_Panda" or "MajorCooke" or "austinv11" then
Shouldent that be

  if playerName ~=  "Grand_Panda" or playerName ~= "MajorCooke" or playerName ~= "austinv11" then
?
Inumel #7
Posted 01 October 2013 - 12:10 AM
Here ill post some code I use for a player detector door, It's probably not perfect but.. it works

http://pastebin.com/TLnJbvrM

Mine uses a table in order to get its allowed players, but that can be changed to your method if you prefer

It can help you figure out what you need to do to get your own working :)/>
immibis #8
Posted 01 October 2013 - 02:02 AM

local _, playerName
"local" means that the following variables (in this case _ and playerName) are only visible inside the while loop.
plazter #9
Posted 01 October 2013 - 10:20 AM
wouldn't it be easier with a tabel like:


local whitelist = {
['name here'] = true,
}



and then if its not a whitelisted 1


if not whitelist[ playerName:lower() ] then
  print("Welcome ".. playerName .." to the corp")
elseif whitelist[ playerName:lower() ] then
  print("something")
end

just wondered :D/>

//Plazter
Engineer #10
Posted 01 October 2013 - 02:03 PM
wouldn't it be easier with a tabel like:


local whitelist = {
['name here'] = true,
}



and then if its not a whitelisted 1


if not whitelist[ playerName:lower() ] then
  print("Welcome ".. playerName .." to the corp")
elseif whitelist[ playerName:lower() ] then
  print("something")
end

just wondered :D/>

//Plazter
ehh.. Why do is the if the same as the elseif? 0.0
I think you meant something like:

local wl = {
   [ "playerName" ] = true
}

if wl[ player ] then -- Without the lower cuz names can contain upper characters
    print(string.format("Welcome %s!", player))
else
    print(string.format("Im sorry, you are not allowed here, %s :(/>", player))
end
austinv11 #11
Posted 01 October 2013 - 09:03 PM
Thanks guys for the responses! Especially to Immibis, as his advice helped me the most!
plazter #12
Posted 02 October 2013 - 02:41 AM
wouldn't it be easier with a tabel like:


local whitelist = {
['name here'] = true,
}



and then if its not a whitelisted 1


if not whitelist[ playerName:lower() ] then
  print("Welcome ".. playerName .." to the corp")
elseif whitelist[ playerName:lower() ] then
  print("something")
end

just wondered :D/>/>

//Plazter
ehh.. Why do is the if the same as the elseif? 0.0
I think you meant something like:

local wl = {
   [ "playerName" ] = true
}

if wl[ player ] then -- Without the lower cuz names can contain upper characters
    print(string.format("Welcome %s!", player))
else
    print(string.format("Im sorry, you are not allowed here, %s :(/>/>", player))
end

The lower() is easy'st if u ask me since you put the whole name in lowercase, atleast that my experiance :)/> btw "%s" what does that do?