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

OpenCCSensor how do I make it save the table

Started by skydude92, 17 July 2013 - 07:02 PM
skydude92 #1
Posted 17 July 2013 - 09:02 PM
Okay so i've been being griefed on a server my someone breaking my stuff when I'm offline so I wanted a program to tell me who was at my base and I want it to save to a file, but I don't know how to go about that. I have to code that prints player names on the computer, that works but how do I make it save the player names to a file?

Spoileros.loadAPI("ocs/apis/sensor")
local prox = sensor.wrap("right")
local targets = prox.getTargets()

while true do
for name, basicDetails in pairs(targets) do
print("Found: " ..name)
end
sleep(45)
end
Lyqyd #2
Posted 17 July 2013 - 09:14 PM

os.loadApi("ocs/apis/sensor")
local prox = sensor.wrap("right")

while true do
  local targets = prox.getTargets()
  local handle = io.open("playerLog", "a")
  handle:write("\n"..os.time()..": "..os.clock().."\n")
  for target in pairs(targets) do
    local details = prox.getTargetDetails(target)
    if details and details.PlayerName then
      handle:write(details.PlayerName.."\n")
    end
  end
  handle:close()
end
PixelToast #3
Posted 17 July 2013 - 09:32 PM
Lyqyd: you forgot to yeild / sleep
skydude92 #4
Posted 17 July 2013 - 09:42 PM
Thanks, and all i should have to do to open it is h = io.open("playerLog", "r")
h.readAll()
?
Lyqyd #5
Posted 17 July 2013 - 09:51 PM
PixelToast is right, my sample code doesn't sleep. If all you want to do is read the log, you can always just run `edit PlayerLog`.
skydude92 #6
Posted 17 July 2013 - 09:53 PM
lol now I really feel like an idiot. I added sleep to it
skydude92 #7
Posted 17 July 2013 - 10:35 PM
Ok that didn't work entirely as intended but after much trial and error I ended up getting it to work by replacing PlayerName with Username.