You're not doing anything wrong, or at least not exactly :P/>. I would create a function that you send the deck number to, and then inside there it checks for your variable.
local function deckTP(deckNum)
if deckNum == "1" then
commandBlock.setCommand("tp @p[r=2] -211 106 405")
commandBlock.runCommand()
elseif deckNum == "2" then
commandBlock.setCommand("tp @p[r=2] -211 113 405")
commandBlock.runCommand()
elseif deckNum == "3" then
commandBlock.setCommand("tp @p[r=2] -211 120 405")
commandBlock.runCommand()
else
term.clear()
term.setCursorPos(1,1)
print("Invalid Destination Entered")
end
end
And then you could swap your getdeck function to be this
local function getdeck()
term.clear()
term.setCursorPos(1,1)
term.write("Turbo Lift")
print()
write("Enter Deck Number: ")
deckTP(read())
end
Now for your buttons with the touchpoint API, any button that you want to add (and i'm going to leave the names and such, however i'm giving 2 examples) You should try and format them like this
t:add("Floor 1",function() deckTP("1") end, 2, 2, 14, 11, colors.red, colors.lime)
t:add("Floor 2",function() deckTP("2") end, 16, 2, 28, 11, colors.red, colors.lime)
If you notice in that second part where it says function() you notice right after that it's got deckTP("1") and deckTP("2") this allows you to have it so when you click the button, you teleport like you should. If you're going to have multiple buttons, that's the only part you need to keep the same, except that number inside deckTP, change that for whatever floor the button should take you to.
Now for your button handling, i would put that all in a function and then whenever a button is pressed, flash it, and execute the function inside of it.
local function buttonPress()
while true do
t:draw()
--# handleEvents will convert monitor_touch events to button_click if it was on a button
local event, p1 = t:handleEvents(os.pullEvent())
if event == "button_click" then
t:flash(p1) --# This will cause it to look green for a second and then back to red
t.buttonList[p1].func() --# This will call the function in the button, That thing earlier where it says function() deckTP("1") end.
break
end
end
end
And then at the bottom in your while loop you should use the parallel API so that it's reading input from the screen, and at the same time, looking for a button input.
while true do
parallel.waitForAny(buttonPress,getdeck) --#it runs both of the functions, please note the lack of () next to the function names. and will continue on whenever one of them ends.
--#However we're in a while true loop, which means that it will restart when one finishes :P/>/>/>/>/>/>
end
So all in all, to get your code working, you would need something like this
Spoiler
--# load the touchpoint API
os.loadAPI("touchpoint")
--# initialize a new button set on the top monitor
local t=touchpoint.new("top")
local monitor = peripheral.wrap("top")
monitor.setTextScale(1)
local commandBlock = peripheral.wrap("left")
t:add("Floor 1",function() deckTP("1") end, 2, 2, 14, 11, colors.red, colors.lime)
t:add("Floor 2",function() deckTP("2") end, 16, 2, 28, 11, colors.red, colors.lime)
local function deckTP(deckNum)
if deckNum == "1" then
commandBlock.setCommand("tp @p[r=2] -211 106 405")
commandBlock.runCommand()
elseif deckNum == "2" then
commandBlock.setCommand("tp @p[r=2] -211 113 405")
commandBlock.runCommand()
elseif deckNum == "3" then
commandBlock.setCommand("tp @p[r=2] -211 120 405")
commandBlock.runCommand()
else
term.clear()
term.setCursorPos(1,1)
print("Invalid Destination Entered")
end
end
local function getdeck()
term.clear()
term.setCursorPos(1,1)
term.write("Turbo Lift")
print()
write("Enter Deck Number: ")
deckTP(read())
end
local function buttonPress()
while true do
t:draw()
--# handleEvents will convert monitor_touch events to button_click if it was on a button
local event, p1 = t:handleEvents(os.pullEvent())
if event == "button_click" then
t:flash(p1) --# This will cause it to look green for a second and then back to red
t.buttonList[p1].func() --# This will call the function in the button, That thing earlier where it says function() deckTP("1") end.
break
end
end
end
while true do
parallel.waitForAny(buttonPress,getdeck) --#it runs both of the functions, please note the lack of () next to the function names. and will continue on whenever one of them ends.
--#However we're in a while true loop, which means that it will restart when one finishes :P/>/>/>/>/>/>
end
Make sure you of course have the touchpoint API installed in the file touchpoint