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

Authentication program not returning output

Started by profjb, 07 July 2015 - 12:52 PM
profjb #1
Posted 07 July 2015 - 02:52 PM
The following script aims to check if a person has clicked a player detector from peripherals++.
i'm trying to print the username of the person clicking the block by returning the statement loginread but it dosen't seem to work. I've asked one of my friends who knows a fair amount about computercraft and he isn't sure either. Thanks :)/>.



--Authentication--
function login_func(loginread)
  reset()
  term.setCursorPos(10,4)
  print("Login")
  setpos2()
  term.write("Username:")
  login_input = read()
  loginread = string.lower(login_input)
  term.clear()
  peripheral.wrap("right")
  setpos1()
  print("Please right click the peripheral")
  local event, players = os.pullEvent( "player" )
  term.clear()
  if players == loginread then
    term.setTextColor(colors.green)
    setpos1()
    textutils.slowPrint("Authentication accepted!")
    sleep(2)
    return loginread
  else
    term.clear()
    setpos1()
    term.setTextColor(colors.red)
    textutils.slowPrint("Authentication denied.")
    sleep(3)
    term.clear()
    login_func()
  end
end   

--Reset balance--

--Create/Read balance accounts--

  
		  
--[Read]--

login_func(loginread)
local loginprint = login_func(loginread)
print(loginprint)
meigrafd #2
Posted 07 July 2015 - 05:27 PM
Where do you set the variable 'loginread' ? You first need to set it before you can pass it to your function on the first call after your –[Read]– line..

Try something like:

--[Read]--

login_func('')
local loginprint = login_func(loginread)
print(loginprint)
..but your function confuses me anyway ;)/>
Edited on 07 July 2015 - 03:31 PM
profjb #3
Posted 07 July 2015 - 05:44 PM
Yeah, this is my first time coding so I feel like a headless chicken.

I thought i'd set the function loginread here:

loginread = string.lower(login_input)

I assume thats not how to correctly set a variable :P/>.
The_Cat #4
Posted 07 July 2015 - 05:55 PM
Try returning 'players' from the pullEvent?
You are also calling the function twice on your program.
Also within the function, login_func('Take the 'loginread' away from the function, should still work(if not define the loginread at the start of the function 'loginread = ' '))
HPWebcamAble #5
Posted 07 July 2015 - 06:19 PM
It looks like you have more code than what you have here, could you post the rest?
If it is much longer, you can put it on pastebin and put a link here.



loginread = string.lower(login_input)

I assume thats not how to correctly set a variable :P/>.

Well, that is a perfectly valid way to set a variable, but in this specific case, it doesn't do what you'd like.



--Authentication--
function login_func(loginread)
  ...
end  

login_func(loginread)
local loginprint = login_func(loginread)
print(loginprint)

Passing 'loginread' to your 'login_func' doesn't actually do anything, but it's a good idea to remove that:

function login() --# You can also shorten the name of some of your variables
  ...
end

print( login() ) --# Simply prints the result of 'login()'


function login_func(loginread)
  ...
  print("Please right click the peripheral")
  local event, players = os.pullEvent( "player" )
  ...
end
The Player Sensor only returns a single player when someone right clicks it (You can also use it to get all nearby players)

So 'event' will always be "player" (since you filter that in 'os.pullEvent("player")'
and 'players' will just be the name of the player that right-clicked the block

There is no need to use 'peripheral.wrap(side)' for the Player Sensor.
Edited on 07 July 2015 - 04:22 PM
profjb #6
Posted 08 July 2015 - 01:01 AM
Ok. So firstly, thanks for the help :)/>. Really appreciate it.
Secondly, all of that has helped to shorten my code a lot and clear a lot of miss-conceptions about functions but when I do print(login()) it just restarts the login() function again. Is their a way to only return the output as you said of the players name?

(Don't worry too much about the last bit, working on some rednet stuff :P/>)
-Themeing–
function setpos1()
term.setCursorPos(6,4)
end

function setpos2()
term.setCursorPos(6,6)
end

function reset()
term.setCursorPos(6,4)
term.setTextColor(colors.white)
end

–Commandblock Commands–
function withdraw_cmd()
if username == stored_username() then
commands.exec( "sudo pay @p[" .. amount .. "]")
else
print("You don't have permission to withdraw from this account")
end
end

–Authentication–
function user_func()
local username = ""
term.setCursorPos(1,1)
term.write("Please enter your username")
end


function login()
reset()
term.setCursorPos(10,4)
print("Login")
setpos2()
term.write("Username:")
local loginprint = string.lower(read())
term.clear()
setpos1()
print("Please right click the peripheral")
local event, players = os.pullEvent( "player" )
term.clear()
if players == loginprint then
term.setTextColor(colors.green)
setpos1()
textutils.slowPrint("Authentication accepted!")
sleep(2)
term.clear()
else
term.clear()
setpos1()
term.setTextColor(colors.red)
textutils.slowPrint("Authentication denied.")
sleep(3)
term.clear()
login_func()
end
end

–Reset balance–

–Create/Read balance accounts–



–[Read]–
–Rednet Connection–

–Main script–

peripheral.wrap("back")
rednet.open("back")
login()
print(login())
rednet.send(6,"test message",uqcn5chdpxrbp4p)

Oh, and the first function after –authentication– is obsolete. Sorry that was my first try, should've removed that.