• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua File logging

Itutorial

Excellent OT User
Joined
Dec 23, 2014
Messages
2,327
Solutions
68
Reaction score
999
Can someone tell me why this doesn't work?

Game Core
Lua:
function Game.logInformation(fileDir, text)
    print("Logging -> "..fileDir..": "..text)
    
    local file = io.open (fileDir, "a+")
    
    if not file then print("Could not load file "..fileDir..". Failed to log.") return false end
    
    file:seek("end")
    file:write(text)
return true
end

Talkaction
Lua:
function onSay(player, words, param)
    if not player:getGroup():getAccess() then
        return true
    end

    if player:getAccountType() < ACCOUNT_TYPE_GOD then
        return false
    end

    local position = player:getPosition()
    local isGhost = not player:isInGhostMode()

    player:setGhostMode(isGhost)
    if isGhost then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You are now invisible.")
        position:sendMagicEffect(CONST_ME_YALAHARIGHOST)
    else
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You are visible again.")
        position.x = position.x + 1
        position:sendMagicEffect(CONST_ME_SMOKE)
    end
    
    Game:logInformation("data/logs/player/"..player:getName()..".txt", "Player used talkaction: /ghost")
    return false
end

The error
Code:
data/lib/core/game.lua:71: attempt to concatenate local 'fileDir' (a table value)
 
Can someone tell me why this doesn't work?

Game Core
Lua:
function Game.logInformation(fileDir, text)
    print("Logging -> "..fileDir..": "..text)
   
    local file = io.open (fileDir, "a+")
   
    if not file then print("Could not load file "..fileDir..". Failed to log.") return false end
   
    file:seek("end")
    file:write(text)
return true
end

Talkaction
Lua:
function onSay(player, words, param)
    if not player:getGroup():getAccess() then
        return true
    end

    if player:getAccountType() < ACCOUNT_TYPE_GOD then
        return false
    end

    local position = player:getPosition()
    local isGhost = not player:isInGhostMode()

    player:setGhostMode(isGhost)
    if isGhost then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You are now invisible.")
        position:sendMagicEffect(CONST_ME_YALAHARIGHOST)
    else
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You are visible again.")
        position.x = position.x + 1
        position:sendMagicEffect(CONST_ME_SMOKE)
    end
   
    Game:logInformation("data/logs/player/"..player:getName()..".txt", "Player used talkaction: /ghost")
    return false
end

The error
Code:
data/lib/core/game.lua:71: attempt to concatenate local 'fileDir' (a table value)
make sure to always close the files.
Lua:
file:close()

Not sure what the error is.. but I've seen this function around for awhile..
Try that instead?
Lua:
function logCommand(player, words, param)
    local file = io.open("data/logs/"..player:getName()..".log", "a+")
    file:write(("[ %s ] %s%s\n"):format(os.date(), words, (param ~= "" and " " or "") .. param))
    file:close()
    return true
end
Lua:
logCommand(player, words, param)
 
Back
Top