• 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 Addskill Logs

belal112

Basic Lua scripter
Joined
Dec 20, 2012
Messages
96
Solutions
1
Reaction score
11
Hello guys!
I'm running an OTX 3.7 server (codename: SAILOR) -which is based on TFS 1.3- and I was trying to add command logs for the server; however, I failed. Instead I started adding the function
Code:
function onSay(player, words, param)

    print("> " .. player:getName() .. " added skill: \"" .. param .. "\".")
    end
The function so far worked very will with the broadcast command yet it wouldn't want to work with /addskill. It either shows the msg in the command prompt or executes the command, it wouldn't do both. Here's the script for the /addskill
Code:
local function getSkillId(skillName)
    if skillName == "club" then
        return SKILL_CLUB
    elseif skillName == "sword" then
        return SKILL_SWORD
    elseif skillName == "axe" then
        return SKILL_AXE
    elseif skillName:sub(1, 4) == "dist" then
        return SKILL_DISTANCE
    elseif skillName:sub(1, 6) == "shield" then
        return SKILL_SHIELD
    elseif skillName:sub(1, 4) == "fish" then
        return SKILL_FISHING
    else
        return SKILL_FIST
    end
end
local function getExpForLevel(level)
    level = level - 1
    return ((50 * level * level * level) - (150 * level * level) + (400 * level)) / 3
end

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 split = param:split(",")
    if split[2] == nil then
        player:sendCancelMessage("Insufficient parameters.")
        return false
    end

    local target = Player(split[1])
    if target == nil then
        player:sendCancelMessage("A player with that name is not online.")
        return false
    end

    -- Trim left
    split[2] = split[2]:gsub("^%s*(.-)$", "%1")

    local count = 1
    if split[3] ~= nil then
        count = tonumber(split[3])
    end

    local ch = split[2]:sub(1, 1)
    for i = 1, count do
        if ch == "l" or ch == "e" then
            target:addExperience(getExpForLevel(target:getLevel() + 1) - target:getExperience(), false)
        elseif ch == "m" then
            target:addManaSpent(target:getVocation():getRequiredManaSpent(target:getBaseMagicLevel() + 1) - target:getManaSpent())
        else
            local skillId = getSkillId(split[2])
            target:addSkillTries(skillId, target:getVocation():getRequiredSkillTries(skillId, target:getSkillLevel(skillId) + 1) - target:getSkillTries(skillId))
        end
    end
    return false
end

I also tried to make the command execute 2 LUAs at the same time but that didn't seem to work so anybody has any idea how that should work? I'd also appreciate it if you could help with just general command logs for each player.
 
Solution
you should use a function for logs so you dont have to copy/paste the same thing multiple times, something like
Code:
function writeLog(text)
    local file = io.open([[data\talkactions\logs.txt]])
    file:write(text .. '\n')
    file:flush()
    file:close()
end
Hello guys!
I'm running an OTX 3.7 server (codename: SAILOR) -which is based on TFS 1.3- and I was trying to add command logs for the server; however, I failed. Instead I started adding the function
Code:
function onSay(player, words, param)

    print("> " .. player:getName() .. " added skill: \"" .. param .. "\".")
    end
The function so far worked very will with the broadcast command yet it wouldn't want to work with /addskill. It either shows the msg in the command prompt or executes the command, it wouldn't do both. Here's the script for the /addskill
Code:
local function getSkillId(skillName)
    if skillName == "club" then
        return SKILL_CLUB
    elseif skillName == "sword" then
        return SKILL_SWORD
    elseif skillName == "axe" then
        return SKILL_AXE
    elseif skillName:sub(1, 4) == "dist" then
        return SKILL_DISTANCE
    elseif skillName:sub(1, 6) == "shield" then
        return SKILL_SHIELD
    elseif skillName:sub(1, 4) == "fish" then
        return SKILL_FISHING
    else
        return SKILL_FIST
    end
end
local function getExpForLevel(level)
    level = level - 1
    return ((50 * level * level * level) - (150 * level * level) + (400 * level)) / 3
end

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 split = param:split(",")
    if split[2] == nil then
        player:sendCancelMessage("Insufficient parameters.")
        return false
    end

    local target = Player(split[1])
    if target == nil then
        player:sendCancelMessage("A player with that name is not online.")
        return false
    end

    -- Trim left
    split[2] = split[2]:gsub("^%s*(.-)$", "%1")

    local count = 1
    if split[3] ~= nil then
        count = tonumber(split[3])
    end

    local ch = split[2]:sub(1, 1)
    for i = 1, count do
        if ch == "l" or ch == "e" then
            target:addExperience(getExpForLevel(target:getLevel() + 1) - target:getExperience(), false)
        elseif ch == "m" then
            target:addManaSpent(target:getVocation():getRequiredManaSpent(target:getBaseMagicLevel() + 1) - target:getManaSpent())
        else
            local skillId = getSkillId(split[2])
            target:addSkillTries(skillId, target:getVocation():getRequiredSkillTries(skillId, target:getSkillLevel(skillId) + 1) - target:getSkillTries(skillId))
        end
    end
    return false
end

I also tried to make the command execute 2 LUAs at the same time but that didn't seem to work so anybody has any idea how that should work? I'd also appreciate it if you could help with just general command logs for each player.
are you wanting to save logs to a file or just print to the console?
 
are you wanting to save logs to a file or just print to the console?

Preferably to a file. I tried adding Admin Logs in the Config.lua but I think I added it incorrectly or it's just that the server doesnt support it. this is what I added:
Code:
-- Admin Logs
adminLogsEnabled = true
displayPlayersLogging = true
prefixChannelLogs = "default"
runFile = "LOGS.txt"
outLogName = "LOGS.txt"
errorLogName = "B-LOGS"
truncateLogsOnStartup = false
 
Code:
local function getSkillId(skillName)
    if skillName == "club" then
        return SKILL_CLUB
    elseif skillName == "sword" then
        return SKILL_SWORD
    elseif skillName == "axe" then
        return SKILL_AXE
    elseif skillName:sub(1, 4) == "dist" then
        return SKILL_DISTANCE
    elseif skillName:sub(1, 6) == "shield" then
        return SKILL_SHIELD
    elseif skillName:sub(1, 4) == "fish" then
        return SKILL_FISHING
    else
        return SKILL_FIST
    end
end
local function getExpForLevel(level)
    level = level - 1
    return ((50 * level * level * level) - (150 * level * level) + (400 * level)) / 3
end

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 split = param:split(",")
    if split[2] == nil then
        player:sendCancelMessage("Insufficient parameters.")
        return false
    end

    local target = Player(split[1])
    if target == nil then
        player:sendCancelMessage("A player with that name is not online.")
        return false
    end

    -- Trim left
    split[2] = split[2]:gsub("^%s*(.-)$", "%1")

    local count = 1
    if split[3] ~= nil then
        count = tonumber(split[3])
    end

    local ch = split[2]:sub(1, 1)
    for i = 1, count do
        if ch == "l" or ch == "e" then
            target:addExperience(getExpForLevel(target:getLevel() + 1) - target:getExperience(), false)
        elseif ch == "m" then
            target:addManaSpent(target:getVocation():getRequiredManaSpent(target:getBaseMagicLevel() + 1) - target:getManaSpent())
        else
            local skillId = getSkillId(split[2])
            target:addSkillTries(skillId, target:getVocation():getRequiredSkillTries(skillId, target:getSkillLevel(skillId) + 1) - target:getSkillTries(skillId))
        end
    end

    -- Write logs to file
    local file = io.open([[data\talkactions\logs.txt]])
    file:write(('> %s added skill %s to %s with a value of %d.\n'):format(player:getName(), split[2], split[1], split[3]))
    file:flush()
    file:close()
    -- End logs
   
    return false
end
just create logs.txt in data\talkactions
 
Code:
local function getSkillId(skillName)
    if skillName == "club" then
        return SKILL_CLUB
    elseif skillName == "sword" then
        return SKILL_SWORD
    elseif skillName == "axe" then
        return SKILL_AXE
    elseif skillName:sub(1, 4) == "dist" then
        return SKILL_DISTANCE
    elseif skillName:sub(1, 6) == "shield" then
        return SKILL_SHIELD
    elseif skillName:sub(1, 4) == "fish" then
        return SKILL_FISHING
    else
        return SKILL_FIST
    end
end
local function getExpForLevel(level)
    level = level - 1
    return ((50 * level * level * level) - (150 * level * level) + (400 * level)) / 3
end

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 split = param:split(",")
    if split[2] == nil then
        player:sendCancelMessage("Insufficient parameters.")
        return false
    end

    local target = Player(split[1])
    if target == nil then
        player:sendCancelMessage("A player with that name is not online.")
        return false
    end

    -- Trim left
    split[2] = split[2]:gsub("^%s*(.-)$", "%1")

    local count = 1
    if split[3] ~= nil then
        count = tonumber(split[3])
    end

    local ch = split[2]:sub(1, 1)
    for i = 1, count do
        if ch == "l" or ch == "e" then
            target:addExperience(getExpForLevel(target:getLevel() + 1) - target:getExperience(), false)
        elseif ch == "m" then
            target:addManaSpent(target:getVocation():getRequiredManaSpent(target:getBaseMagicLevel() + 1) - target:getManaSpent())
        else
            local skillId = getSkillId(split[2])
            target:addSkillTries(skillId, target:getVocation():getRequiredSkillTries(skillId, target:getSkillLevel(skillId) + 1) - target:getSkillTries(skillId))
        end
    end

    -- Write logs to file
    local file = io.open([[data\talkactions\logs.txt]])
    file:write(('> %s added skill %s to %s with a value of %d.\n'):format(player:getName(), split[2], split[1], split[3]))
    file:flush()
    file:close()
    -- End logs
 
    return false
end
just create logs.txt in data\talkactions

The logs still wouldn't work, there are no errors or anything but the logs.txt is never updated, it's still empty
EDIT: I used your idea of "io." and put the code
Code:
    -- Write logs to file
    local file = io.open([[data\talkactions\logs.txt]])
    io.stderr:write(('> %s added skill %s to %s with a value of %d.\n'):format(player:getName(), split[2], split[1], split[3]))
    local temp = io.input
   --file:flush()
    --file:close()
    -- End logs
instead of the one you used and now it posts on the cmd, THANK YOU SO MUCH
 
Last edited:
The logs still wouldn't work, there are no errors or anything but the logs.txt is never updated, it's still empty
EDIT: I used your idea of "io." and put the code
Code:
    -- Write logs to file
    local file = io.open([[data\talkactions\logs.txt]])
    io.stderr:write(('> %s added skill %s to %s with a value of %d.\n'):format(player:getName(), split[2], split[1], split[3]))
    local temp = io.input
   --file:flush()
    --file:close()
    -- End logs
instead of the one you used and now it posts on the cmd, THANK YOU SO MUCH
you should still use file:flush() and file:close()
it will save the file and close
i forgot the mode in io.open, it defaults to read so it wouldn't write to the file
here's the code i was trying to give
Code:
   local str = ('> %s added skill %s to %s with a value of %d.\n'):format(player:getName(), split[2], split[1], split[3])
   local file = io.open([[data\talkactions\logs.txt]], 'a+')
    file:write(str)
    file:flush()
    file:close()
    print(str)
this should write to the file, save, close and then print to your console.

a+ should append (and save all previous data) to the file when writing to it
 
you should still use file:flush() and file:close()
it will save the file and close
i forgot the mode in io.open, it defaults to read so it wouldn't write to the file
here's the code i was trying to give
Code:
   local str = ('> %s added skill %s to %s with a value of %d.\n'):format(player:getName(), split[2], split[1], split[3])
   local file = io.open([[data\talkactions\logs.txt]], 'a+')
    file:write(str)
    file:flush()
    file:close()
    print(str)
this should write to the file, save, close and then print to your console.

a+ should append (and save all previous data) to the file when writing to it
OH great! Now it's working perfectly! Thanks so much man
 
@Xeraphus , sorry to bother you again.. I have something concerning that code. Now I have used the same code for various commands like /i , /owner , and so on but I kind of want it to show the date and time too, is that possible? like.. what would I type in the format in order for it to fetch the date and time? I specifically want it to be added to this:
Code:
 local str = ('> %s unbanned %s.\n'):format(player:getName(), param)
   local file = io.open([[data\talkactions\logs\.txt]], 'a+')
    file:write(str)
    file:flush()
    file:close()
    print(str)
making it say for example: "Vixgo unbanned Paulo on (date)" instead of just "Vixgo unbanned Paulo"
Thank you! :D EDIT: if there is a way to show position too, would be amazing!
 
just google lua date format
Nevermind I figured it out myself :D Thanks though!

For any one who needs it, it is "os.date()"

I still have a problem with the position format. can you help with that? I can't seem to find it by googling it. :/
 
Nevermind I figured it out myself :D Thanks though!

For any one who needs it, it is "os.date()"

I still have a problem with the position format. can you help with that? I can't seem to find it by googling it. :/
position format?
like the player's current position?
you can add to the string (X: %d, Y: %d, Z: %d) and use .x .y .z positions
 
what you mean position format? to get the position values u write pos.x pos.y pos.z if that's what u meant


Also please write Lua not LUA. It's not an abbreviation. Hurts my eyes to read.
 
Last edited:
position format?
like the player's current position?
you can add to the string (X: %d, Y: %d, Z: %d) and use .x .y .z positions
what you mean position format? to get the position values u write pos.x pos.y pos.z if that's what u meant


Also please write Lua not LUA. It's not an abbreviation. Hurts my eyes to read.

Thank you guys! and Sorry @Colandus , my laptop corrects it that way I don't know why so I thought it was like that xD
 
you should use a function for logs so you dont have to copy/paste the same thing multiple times, something like
Code:
function writeLog(text)
    local file = io.open([[data\talkactions\logs.txt]])
    file:write(text .. '\n')
    file:flush()
    file:close()
end
 
Solution
you should use a function for logs so you dont have to copy/paste the same thing multiple times, something like
Code:
function writeLog(text)
    local file = io.open([[data\talkactions\logs.txt]])
    file:write(text .. '\n')
    file:flush()
    file:close()
end
I really don't know where to add that xD
 
Lua:
Function writeLog(player:getName() ..": ".. words .. param)
    local file = io.open([[data\talkactions\logs.txt]])
    file:write(text .. '\n')
    file:flush()
    file:close()
end

Doesn't work either. TFS 1.2.

Code:
[Warning - Event::checkScript] Can not load script:  scripts/add_skill.lua
data/talkactions/scripts/add_skill.lua:67: '=' expected near 'writeLog'
 
Back
Top