Jump to content

Quick help here


mgdmgd

Recommended Posts

  • Moderators

It does, you just have to search better.

https://wiki.multitheftauto.com/wiki/FileCreate

In the Netherlands we would call a you a 'blind chicken', although it is just a way of announcing somebody who doesn't search very well. (don't take this personal)

  
-- on the top of your script 
local newFile = fileCreate("command.log")       
fileFlush ( newFile ) -- log files require buffers to reduce hard-drive writing. (more performance)  
-- 
  
-- when a player writes a command 
if (newFile) then                                        
    fileWrite(newFile, "Log the blind chicken!")        
end 
-- 
  
-- on resource stop > 
if (newFile) then   
    fileClose(newFile) 
end 
-- 

Link to comment

I wrote you a little script.

local filepath = "command-log.txt" 
local file 
  
addEventHandler("onPlayerCommand", root, function (cmdname) 
    if (not fileExists(filepath)) then 
        fileCreate(filepath) 
    end 
    file = fileOpen(filepath) 
    fileSetPos(file, fileGetSize(file)) 
    fileWrite(file, getPlayerName(source).." typed command "..cmdname.."\n") 
    fileFlush(file) 
    fileClose(file) 
end) 

Just because I was interested in testing this out.

Text document looks sorta like this:

Viruz typed command stop 
Viruz typed command start 
Viruz typed command debugscript 

or this format I've just made;

local filepath = "command-log.txt" 
local file 
local months = { 
    "January", 
    "February", 
    "March", 
    "April", 
    "May", 
    "June", 
    "July", 
    "August", 
    "September", 
    "October", 
    "November", 
    "December", 
} 
  
addEventHandler("onPlayerCommand", root, function (cmdname) 
    if (not fileExists(filepath)) then 
        fileCreate(filepath) 
    end 
    file = fileOpen(filepath) 
    fileSetPos(file, fileGetSize(file)) 
    if (cmdname ~= "stopanim") then 
        local time = getRealTime() 
        fileWrite(file,"["..time.monthday.." "..months[time.month].." "..(time.year+1900).." | "..time.hour..":"..time.minute..":"..time.second.."] "..getPlayerName(source)..": "..cmdname.."\n") 
        fileFlush(file) 
        fileClose(file) 
    end 
end) 

looks like this:

[24 April 2016 | 11:27:35] Viruz: debugscript 

Link to comment
Opening and closing a file for every command isn't a good idea. (when the file gets larger you might experience lagg)

That's why the fileFlush function is so important.

fileFlush 

So I'd would be like this right;

local filepath = "command-log.txt" 
local file 
local months = { 
    "January", 
    "February", 
    "March", 
    "April", 
    "May", 
    "June", 
    "July", 
    "August", 
    "September", 
    "October", 
    "November", 
    "December", 
} 
  
addEventHandler( "onResourceStart", getResourceRootElement(getThisResource()), function () 
    if (not fileExists(filepath)) then 
        fileCreate(filepath) 
    else 
        file = fileOpen(filepath) 
    end 
end) 
  
addEventHandler("onPlayerCommand", root, function (cmdname) 
    fileSetPos(file, fileGetSize(file)) 
    if (cmdname ~= "stopanim") then 
        local time = getRealTime() 
        fileWrite(file,"["..time.monthday.." "..months[time.month].." "..(time.year+1900).." | "..time.hour..":"..time.minute..":"..time.second.."] "..getPlayerName(source)..": "..cmdname.."\n") 
        fileFlush(file) 
    end 
end) 
  
addEventHandler("addEventHandler", getResourceRootElement(getThisResource()), function () 
    fileClose(file) 
end) 

Link to comment
  • Moderators

Jup, yup, yep,

(Except for the typo of the event on line 35 > onResourceStop)

And this line:

fileSetPos(file, fileGetSize(file))  

It only has to be called ones, when the file already exist. But is not a critical thing. After writing lines, the position will also change, so no need to call it every time you add content.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...