I tried to do it myself but I failed. I don't understand this I/O Model in lua.
I need this script to determine whether my server is still running or not. As a result I can restart automatically my server with another script.
function updateUptimeFile(path) 	local path = path or "sys/lua/uptime.txt" 	local file = io.open(path, "w") 	file:write(os.date("%H:%M:%S")) 	file:close() end
local i = 0; local temp = "a"; addhook("second", "test") function test() 	i = i + 1; 	if a > 100 then a = 1; end 	if i%10 == 0 then 		local file = io.open("path/to/file", "w"); 		if (temp == "a") then temp = "b"; 		else temp = "a"; 		end 		file:write(temp); 		file:close(); 	end end
-- Use locals and upvalues, it's faster local file = io.open("sys/lua/file.txt", "rb+") local write_a = false function updateTextFile() 	-- Start writing on beginning of file 	file:seek("set") 	-- Write either "a" or "b" 	file:write(write_a and "a" or "b") 	-- Apply the changes 	file:flush() 	-- Flip the switch to write either "a" or "b" in next write 	write_a = not(write_a) end -- Finally call timer, set count to 0 so it's called infinite times. timer(10000, "updateTextFile", "", 0)
local file = io.open("sys/lua/file.txt", "rb+")
local file = io.open("sys/lua/file.txt", "wb+")
-- Use locals and upvalues, it's faster local file = io.open("sys/lua/file.txt", "wb+") local write_a = false function updateTextFile() 	-- Start writing on beginning of file 	file:seek("set") 	-- Write either "a" or "b" 	file:write(write_a and "a" or "b") 	-- Apply the changes 	file:flush() 	-- Flip the switch to write either "a" or "b" in next write 	write_a = not(write_a) end -- Finally call timer, set count to 0 so it's called infinite times. timer(10000, "updateTextFile", "", 0)
file:close()at the end of the
updateTextFile()function which is after 13 line.
LUA ERROR: sys/lua/server.lua:7: attempt to use a closed file
updateTextFileis used. It is never opened afterwards.
fileused as a variable for the whole block scope is shitty practice - even if it's faster. Faster does not always mean better.
updateTextFileis used. It is never opened afterwards.
fileused as a variable for the whole block scope is shitty practice - even if it's faster. Faster does not always mean better.
local write_a = false function updateTextFile() 	-- Start writing on beginning of file 	local file = io.open("sys/lua/file.txt", "wb+") 	-- Write either "a" or "b" 	file:write(write_a and "a" or "b") 	-- Close the file. Will flush it automatically 	file:close() 	-- Flip the switch to write either "a" or "b" in next write 	write_a = not(write_a) end timer(10000, "updateTextFile", "", 0)