Jump to content




Reading Files to check for word.


1 reply to this topic

#1 Waterspark63

  • Members
  • 13 posts

Posted 18 June 2018 - 01:30 PM

Hey, a bit of a stupid question, so I'm trying to make a 'user' creator that when you input a name, it checks a file that has a full list of all the currently created user names, to prevent duplicates, and if it doesn't then it adds it to the file later when creating the account, but I can't figure out how to search the file for the string, any help?

#2 Bomb Bloke

    Hobbyist Coder

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

Posted 19 June 2018 - 02:45 AM

The easy way is to put your usernames in a table:

local usernames = {["Waterspark63"] = true, ["Bomb Bloke"] = true}

print("Enter a username:")

local user = read()

if usernames[user] then
  -- Username exists
else
  -- Username does not exist
end

You can then save / load this table using textutils.serialise() / textutils.unserialise():

-- Save table:
local outFile = fs.open("users.txt", "w")
outFile.writeLine( textutils.serialise( usernames ) )
outFile.close()

-- Load table:
local inFile = fs.open("users.txt", "r")
local usernames = textutils.unserialise( inFile.readAll() )
inFile.close()

Edited by Bomb Bloke, 19 June 2018 - 02:46 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users