• 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
that's not calling the function, you're editing the definition of the function
put this in data/global.lua
Lua:
function writeLog(text)
    local file = io.open([[data\talkactions\logs.txt]], "w")
    file:write(text .. '\n')
    file:flush()
    file:close()
end

example with addskill: (look at the end where i call writeLog)
Lua:
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 not split[2] then
        player:sendCancelMessage("Insufficient parameters.")
        return false
    end

    local target = Player(split[1])
    if not target 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] 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
    writeLog(("%s: %s %s"):format(player:getName(), words, param)) -- example: Vulcan: /addskill Vulcan, axe, 50
    return false
end

edit: should probably call io.open with write parameter
 
that's not calling the function, you're editing the definition of the function
put this in data/global.lua
Lua:
function writeLog(text)
    local file = io.open([[data\talkactions\logs.txt]], "w")
    file:write(text .. '\n')
    file:flush()
    file:close()
end

example with addskill: (look at the end where i call writeLog)
Lua:
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 not split[2] then
        player:sendCancelMessage("Insufficient parameters.")
        return false
    end

    local target = Player(split[1])
    if not target 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] 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
    writeLog(("%s: %s %s"):format(player:getName(), words, param)) -- example: Vulcan: /addskill Vulcan, axe, 50
    return false
end

edit: should probably call io.open with write parameter
that's not calling the function, you're editing the definition of the function
put this in data/global.lua
Lua:
function writeLog(text)
    local file = io.open([[data\talkactions\logs.txt]], "w")
    file:write(text .. '\n')
    file:flush()
    file:close()
end

example with addskill: (look at the end where i call writeLog)
Lua:
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 not split[2] then
        player:sendCancelMessage("Insufficient parameters.")
        return false
    end

    local target = Player(split[1])
    if not target 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] 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
    writeLog(("%s: %s %s"):format(player:getName(), words, param)) -- example: Vulcan: /addskill Vulcan, axe, 50
    return false
end

edit: should probably call io.open with write parameter


I did those two things. Nothing in logs.txt.
 
works fine for me exactly as i wrote that example
GaFDjsc.png

are you checking data/talkactions/logs.txt? or are you checking data/logs/
 
no need to reload if you restarted the server
all you need to do is use /addskill on a character and it will write to logs.txt
 
It's not working. Dunno what to say. Thanks for trying lol. My skills still went up though xD

Global.lua isn't picking up the information from logs.txt because the code doesn't work to do that in add_skill.lua
Aka
Lua:
writeLog(("%s: %s %s"):format(player:getName(), words, param)) -- example: Vulcan: /addskill Vulcan, axe, 50
its not writing the log.
 
Last edited:
Back
Top