Jump to content




Program Not Reading File Correctly?


  • You cannot reply to this topic
2 replies to this topic

#1 popdog15

  • Members
  • 82 posts

Posted 15 October 2013 - 05:45 PM

Here's my current code:
Spoiler
The authorized file simply has the word "test" on the first line, though it still gives me access granted. Also it says "Access granted nil, welcome in!" rather than my username. Any ideas?

#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 15 October 2013 - 06:21 PM

You define "getTable" as a table, and you open the "authorised" file, but you still never load anything from the file into the table.

When your checkAuthority function first fails it calls itself without an arguement (note that having a function call itself is, in itself, generally bad practise). Because there's nothing in the table, and that second call doesn't give it a name to check against, it then compares nothing to nothing and finds them to be the same: "nil" hence gets welcomed to the system.

A somewhat improved table loader compared to the one I gave you earlier:

local getTable = {}

.
.
.

local function loadTableFromFile()
  local temp
  local loadFile = fs.open("authorized", "r")
  while true do
    temp = loadFile.readLine()
    if temp ~= nil then getTable[temp] = true else break end
  end
  loadFile.close()
end

This creates one key in the table for each player in the file, named the same as those players.

Later, when you want to check if a given name exists in the table, you can simply perform:

if getTable[player] then
  print("You're authorised!")
else
  print("I don't know you.")
end

I recommend reviewing some documentation about Lua tables. This should be a decent start.

#3 popdog15

  • Members
  • 82 posts

Posted 15 October 2013 - 06:30 PM

View PostBomb Bloke, on 15 October 2013 - 06:21 PM, said:

You define "getTable" as a table, and you open the "authorised" file, but you still never load anything from the file into the table.

When your checkAuthority function first fails it calls itself without an arguement (note that having a function call itself is, in itself, generally bad practise). Because there's nothing in the table, and that second call doesn't give it a name to check against, it then compares nothing to nothing and finds them to be the same: "nil" hence gets welcomed to the system.

A somewhat improved table loader compared to the one I gave you earlier:

local getTable = {}

.
.
.

local function loadTableFromFile()
  local temp
  local loadFile = fs.open("authorized", "r")
  while true do
	temp = loadFile.readLine()
	if temp ~= nil then getTable[temp] = true else break end
  end
  loadFile.close()
end

This creates one key in the table for each player in the file, named the same as those players.

Later, when you want to check if a given name exists in the table, you can simply perform:

if getTable[player] then
  print("You're authorised!")
else
  print("I don't know you.")
end

I recommend reviewing some documentation about Lua tables. This should be a decent start.
Thank you. I spent an unnecessary amount of time trying to get this working, when I totally forgot about the previous table loader you put up. Thanks, again.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users