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

Receiving error (attempt to write to global!)

Started by greasysock, 15 May 2012 - 11:07 PM
greasysock #1
Posted 16 May 2012 - 01:07 AM
Ok, so i am making an application for a factory in a server, and I use one computer to register for a login and another to login. I have the register script down good, but when i try reading and transforming the data into a table. I get an error, i am extremely new to lua and coding. so if you could do a breakdown it would be highly appreciated.
this is the login script… tell me what needs to be done to get it working.

term.clear()
term.setCursorPos(12,1)
print("  __________________________")
term.setCursorPos(12,2)
print(" /  CLOUDfactory		   /")
term.setCursorPos(12,3)
print("/	   GreasyLogin0.0.5  /")
term.setCursorPos(12,4)
print("--------------------------")
term.setCursorPos(12,5)
print("|						|")
term.setCursorPos(12,6)
print("|						|")
term.setCursorPos(12,7)
print("|						|")
term.setCursorPos(12,8)
print("|						|")
term.setCursorPos(12,9)
print("--------------------------")
term.setCursorPos(13,6)
term.write("Login: ")
login = read()
term.setCursorPos(13,7)
term.write("Password: ")
pass = read("*")
time = os.time()
local file = fs.open("disk/users/"..login, "r")
input = file.readAll()
file.close()
function string:split(sep)
		local sep, fields = sep or ":", {}
		local pattern = string.format("([^%s]+)", sep)
		self:gsub(pattern, function(c) fields[#fields+1] = c end)
		return fields
end
t = split(input, ",")
rednet.open("bottom")
print(t)
x = 4 --Server ID
if t[1] == login and t[2] == pass then
  Print("Welcome, "..user)
  print("The time is: "..time)
  sleep(1)
  rednet.send(x, t.." Entered Factory")
  os.shutdown()
else
print(t)
print("Incorrect Username/Password!")
sleep(1)
rednet.send(x, login..": minecraft time: "..time.." denied entry password used: ".. pass)
os.shutdown()
end

this is my registration code:


-- Register Program --
term.clear()
term.setCursorPos(1, 1)
print("__________________________")
term.setCursorPos(1, 2)
print("CloudFactory Regristation")
term.setCursorPos(1, 3)
print("--------------------------")
while true do
term.setCursorPos(12, 5)
term.write("*Username: ")
user = read()
if user == empty then
print("Please enter a username!")
  else
   break
end
end
term.setCursorPos(12, 6)
term.write("Email: ")
email = read()
while true do
  term.setCursorPos(12, 7)
  term.clearLine()
  term.write("*Password: ")
  pass = read("*")
  term.setCursorPos(12, 8)
  term.clearLine()
  term.write("*Re-type Password: ")
  pass2 = read("*")
   if pass == pass2 then
	term.clear()
term.setCursorPos(15, 15)
	print("Register Succesful")

	break
  end
if pass2 == pass then
  print("Regristation Succeful!")
else
  print("Error: Passwords don't match!")
  print("Please re-enter password!")
end
end
function readLines(sPath)
  local file = fs.open(sPath, "r")
  if file then
		local tLines = {}
		local sLine = file.readLine()
		while sLine do
		  table.insert(tLines, sLine)
		  sLine = file.readLine()
		end
		file.close()
		return tLines
  end
end
function writeLines(tLines, sPath)
  local file = fs.open(sPath, "w")
  if file then
		for _, line in ipairs(tLines) do
		  file.writeLine(line)
		end
		file.close()
  end
end
t = user..","..pass..","..email
fs.makeDir("disk/users")
local file = io.open("disk/users/"..user, "w")
file:write(t) -- Write the user's name/pass/email
file:close()

i still need to make a few adjustments to the signup code, but if you could tell me how to get this working itd be great… also some advice would be helpful as well. the login and signup computers are connected to the same disk.
MysticT #2
Posted 16 May 2012 - 01:16 AM
You can't modify API's, they are protected. You can't do:

function string:split(sep)
Change it's name, and you'll have to change the function to:

function split_string(s, sep)
  local sep, fields = sep or ":", {}
  local pattern = string.format("([^%s]+)", sep)
  string.gsub(s, pattern, function(c) fields[#fields+1] = c end)
  return fields
end
and then change every part of the code that uses the function.