Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
TheEpicGamer909

Attempt to compare number with nil

Question

Hi, so I'm making a program that draws some buttons and teleports you to specific coordinates when you click on their respective buttons. I'm getting an error of something along the lines of "attempt to compare number with nil" at line 112. I checked the code and there shouldn't be anything wrong. Any help would be appreciated.

 

teleport.txt

component = require("component")
gpu = component.gpu
term = require("term")
command_block = component.command_block
event = require("event")

local users = {"TheEpicGamer909","Skutten2002"}
local authorized = false

local places = {}
places.station = {}
places.station["x"] = 218
places.station["y"] = 151
places.station["z"] = 60
places.library = {}
places.library["x"] = 205
places.library["y"] = 112
places.library["z"] = 59
places.roomComputer = {}
places.roomComputer["x"] = 206
places.roomComputer["y"] = 106
places.roomComputer["z"] = 71
places.docks = {}
places.docks["x"] = 218
places.docks["y"] = 104
places.docks["z"] = 77
places.roomWarLeft = {}
places.roomWarLeft["x"] = 221
places.roomWarLeft["y"] = 99
places.roomWarLeft["z"] = 70
places.roomWarRight = {}
places.roomWarRight["x"] = 221
places.roomWarRight["y"] = 99
places.roomWarRight["z"] = 79
places.emergency = {}
places.emergency["x"] = 227
places.emergency["y"] = 99
places.emergency["z"] = 79
places.projector = {}
places.projector["x"] = 207
places.projector["y"] = 99
places.projector["z"] = 76
places.roomBed = {}
places.roomBed["x"] = 212
places.roomBed["y"] = 91
places.roomBed["z"] = 72

local function Button (x,y,width,height,colorText,colorBackground,text,isPressed)
  local button = {}
  button.x = x
  button.y = y
  button.width = width
  button.height = height
  button.colorText = colorText
  button.colorBackground = colorBackground
  button.text = text
  function button.draw ()
    gpu.setForeground(button.colorText)
    gpu.setBackground(button.colorBackground)
    gpu.fill(x,y,width,height," ")
    gpu.set(button.x + math.floor((button.width - #text) / 2),button.y + math.floor((button.height-1) / 2),button.text)
    gpu.setForeground(0xFFFFFF)
    gpu.setBackground(0x000000)
  end
  --[[function button.isClicked(x,y)
    return (x >= button.x and x < button.x + button.width and y >= button.y and y < button.y + button.height)
  end]] --the error first appeared in this function, i commented this out and tried putting the code directly into line 112, where the error appears now
  return button
end

local function teleport (place,name)
  for _,user in pairs(users) do
    if name == user then authorized = true end
  end
  if authorized == true then
    command_block.setCommand("/tp" .. " " .. name .. " " .. places[place["x"]] .. " " .. places[place["y"]] .. " " .. places[place["z"]])
    command_block.run()
    authorized = false
    command_block.setCommand("")
  end
end

command_block.setCommand("")
term.clear()
--[[gpu.setResolution(17,8)]] --so you can see the errors

local buttons = {}
buttons.station = Button(1,1,17,1,0x000000,0xFFA500,"Station")
buttons.library = Button(1,2,17,1,0x000000,0xFFFF00,"Library")
buttons.roomComputer = Button(1,3,17,1,0x000000,0xFFA500,"Computer Room")
buttons.docks = Button(1,4,17,1,0x000000,0xFFFF00,"Docks")
buttons.roomWarLeft = Button(1,5,9,1,0xFFFFFF,0x404040,"War")
buttons.roomWarRight = Button(10,5,8,1,0x000000,0xbfbfbf,"Room")
buttons.emergency = Button(1,6,17,1,0x000000,0xFFFF00,"Emergency")
buttons.projector = Button(1,7,17,1,0x000000,0xFFA500,"Projector")
buttons.roomBed = Button(1,8,17,1,0x000000,0xFFFF00,"Bedroom")

buttons.station.draw()
buttons.library.draw()
buttons.roomComputer.draw()
buttons.docks.draw()
buttons.roomWarLeft.draw()
buttons.roomWarRight.draw()
buttons.emergency.draw()
buttons.projector.draw()
buttons.roomBed.draw()
term.setCursor(17,8)

while true do
  local touch = {event.pull("touch")}
  for buttonName,buttonContent in pairs(buttons) do
    if ((touch.x >= buttonContent.x and touch.x < buttonContent.x + buttonContent.width) and (y >= buttonContent.y and y < buttonContent.y + buttonContent.height)) == true then --here is the error
      teleport(buttonName,touch.playerName)
    end
  end
  --[[if touch.y == 1 then teleport("station",touch.playerName)
  elseif touch.y == 2 then teleport("library",touch.playerName)
  elseif touch.y == 3 then teleport("roomComputer",touch.playerName)
  elseif touch.y == 4 then teleport("docks",touch.playerName)
  elseif (touch.y == 5 and (touch.x >= 1 and touch.x <= 9)) then teleport("roomWarLeft",touch.playerName)
  elseif (touch.y == 5 and (touch.x >= 10 and touch.x <=17)) then teleport("roomWarRight",touch.playerName)
  elseif touch.y == 6 then teleport("emergency",touch.playerName)
  elseif touch.y == 7 then teleport("projector",touch.playerName)
  elseif touch.y == 8 then teleport("roomBed",touch.playerName)
  end]] --if i use this (less portable) code instead of the for loop above, it doesn't do anything at all when i click a button
end
Link to post
Share on other sites

5 answers to this question

Recommended Posts

  • 0

The problem: event.pull returns multiple arguments, not a table with the names put into it. So "touch" will actually contain touch[2], touch[3] instead of touch.x, touch.y.

Step 1: Tidy up your places table:

 

    local places = {}
    places.station = {x = 218, y = 151, z = 60}
    places.library = {x = 205, y = 112, z = 59}
    places.roomComputer = {x = 206, y = 106, z = 71}
    places.docks = {x = 218, y = 104, z = 77}
    places.roomWarLeft = {x = 221, y = 99, z = 70}
    places.roomWarRight = {x = 221, y = 99, z = 79}
    places.emergency = {x = 227, y = 99, z = 79}
    places.projector = {x = 207, y = 99, z = 76}
    places.roomBed = {x = 212, y = 91, z = 72}

Step 2: Fix the loop. Here's one way to do it which mostly maintains the code style:

 

    while true do
      local touch = {}
      touch.screenAddress, touch.x, touch.y, touch.button, touch.playerName = event.pull("touch")
      for buttonName,buttonContent in pairs(buttons) do
        if ((touch.x >= buttonContent.x and touch.x < buttonContent.x + buttonContent.width) and (y >= buttonContent.y and y < buttonContent.y + buttonContent.height)) == true then --here is the error
          teleport(buttonName,touch.playerName)
        end
      end
    end
Link to post
Share on other sites
  • 0

First parameter event.pull returns is always a name of event. Here's the loop that should work.

while true do
  local touch = {}
  touch.eventName, touch.screenAddress, touch.x, touch.y, touch.button, touch.playerName = event.pull("touch")
  for buttonName,buttonContent in pairs(buttons) do
    if ((touch.x >= buttonContent.x and touch.x < buttonContent.x + buttonContent.width) and (y >= buttonContent.y and y < buttonContent.y + buttonContent.height)) == true then --here is the error
      teleport(buttonName,touch.playerName)
    end
  end
end
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.