• 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!

Solved Talkaction Problem

Lava Titan

Developer
Joined
Jul 25, 2009
Messages
1,529
Solutions
1
Reaction score
85
Location
Portugal
Hey, I've been trying to make a query to get player points in TFS 1.1 since theres no functions to getPoints...

Code:
print(db.storeQuery("SELECT `premium_points` FROM `accounts` WHERE `id` = "..player:getAccountId()..""))

I just don't know how to get the result from the stored query, can any1 explain me?
 
Hey, I've been trying to make a query to get player points in TFS 1.1 since theres no functions to getPoints...

Code:
print(db.storeQuery("SELECT `premium_points` FROM `accounts` WHERE `id` = "..player:getAccountId()..""))

I just don't know how to get the result from the stored query, can any1 explain me?

<wrong tfs version>
 
Last edited:
I'm trying to add points to player by name

so far I got this

Code:
function addPointsToPlayer(target, points)
    local target = Player(cid)
    local points = points
    local query = "UPDATE `accounts` SET `premium_points` = "..target:getPoints() + points.." WHERE `accounts`.`id` = "..target:getAccountId()

    if creature:isPlayer() then
        db.query(query)
    else
        player:say("Error, report this to an Admin...", TALKTYPE_SAY)
    end

    return true
end
 
Why do you need to use the account id to get the player name here?
You can use Player(name) to get player userdata based on the name, then use it same way with :getAccountId() if you want to get the account id based on player name.
 
In the fuction you posted, there is no player name used, so what exactly do you want to do with a player name, why you need it and is it supposed to get the player name based on something or something based on the player name?
If you need to get the account id based on player name you can use this.
Code:
Player(name):getAccountId()
 
I'm sorry I just noticed I was doing it all wrong...

I dont need a function to add points to target in talkactions...

I created this:

Code:
function onSay(player, words, param)
    if(param == '') then
        player:sendTextMessage(MESSAGE_STATUS_WARNING, "Param...")
        return false
    end

    local t = string.explode(param, ",")
    if(not t[3] and t[2] ~= 'get') then
            player:sendTextMessage(MESSAGE_STATUS_WARNING, "Not enought params...\nParam List: add, get, remove...")
        return false
    end

    local target = Creature(param[1]) -- /points playername
    if target == nil then
            player:sendTextMessage(MESSAGE_STATUS_WARNING, "Creature not found.")
        return false
    end

    if(param[2] == 'add') then -- /points playername, add
        target:addPoints(param[3]) -- /points playername, add, amount
        player:sendTextMessage(MESSAGE_STATUS_WARNING, "You added "..param[3].." points to "..target:getName()..".")
        print(""..player:getName().." added "..param[3].." points to "..target:getName()..".")
    elseif(param[2] == 'get') then -- /points playername, get
        player:sendTextMessage(MESSAGE_STATUS_WARNING, ""..target:getName().." has "..target:getPoints().." points.")
        print(""..player:getName().." checked "..target:getName().." points. Result was: "..target:getPoints().."...")
    elseif(param[2] == 'remove') then -- /points playername, remove
        target:removePoints(param[3]) -- /points playername, remove, amount
        player:sendTextMessage(MESSAGE_STATUS_WARNING, "You removed "..param[3].." points from "..target:getName()..".")
        print(""..player:getName().." removed "..param[3].." points from "..target:getName()..".")
    else
        player:sendTextMessage(MESSAGE_STATUS_WARNING, "ERROR ROCKET CRASHED")
        print("ERROR ROCKET CRASHED")
    end
    return true
end

Code:
function getPoints()
    -- print(getPoints())
     local resultx = db.storeQuery("SELECT `premium_points` FROM `accounts` WHERE `id` = "..player:getAccountId())
     if resultx then
         points = result.getDataString(resultx, 'premium_points')
         result.free(resultx)
         return points
     end
     return LUA_ERROR
end

function addPoints(points)
    local points = points
    local query = "UPDATE `accounts` SET `premium_points` = "..getPoints() + points.." WHERE `accounts`.`id` = "..player:getAccountId()

    if creature:isPlayer() then
        db.query(query)
    else
        player:say("Error, report this to an Admin...", TALKTYPE_SAY)
    end

    return true
end

function removePoints(points)
    local points = points
    local query = "UPDATE `accounts` SET `premium_points` = "..getPoints() - points.." WHERE `accounts`.`id` = "..player:getAccountId()

    if creature:isPlayer() then
        db.query(query)
    else
        player:say("Error, report this to an Admin...", TALKTYPE_SAY)
    end

    return true
end

I got no errors in console but command is not working XD

Any tips?
 
Last edited:
Always useful to add what exactly happens and what should be different so people know where to look and what to change.
 
Change Creature(param[1]) to Player(param[1])

And replace the functions you added in compat.lua with these:
Code:
function Player.getPoints(self)
     local resultId = db.storeQuery("SELECT `premium_points` FROM `accounts` WHERE `id` = " .. self:getAccountId())
     if resultId ~= false then
         local points = result.getDataInt(resultId, 'premium_points')
         result.free(resultId)
         return points
     end

     return 0
end

function Player.addPoints(self, points)
    if not points or type(points) ~= 'number' then
        points = 0
    end

    db.query("UPDATE `accounts` SET `premium_points` = `premium_points` + " .. points .. " WHERE `id` = " .. self:getAccountId()))
    return true
end

function Player.removePoints(self, points)
    if not points or type(points) ~= 'number' then
        points = 0
    end

    db.query("UPDATE `accounts` SET `premium_points` = `premium_points` - " .. points .. " WHERE `id` = " .. self:getAccountId())
    return true
end
 
ok I rewrote the command based on other commands from tfs 1.1

but I can't get it to work...

when I say: /command --it sends msg Insufficient parameters.
when I say: /command playername --it doesn't do anything

Anyone know why?

Code:
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:sendTextMessage(MESSAGE_STATUS_WARNING, "Insufficient parameters.")
        return false
    end

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

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

    if split[2] == "add" then -- /points playername, add
        target:addPoints(count) -- /points playername, add, amount
        player:sendTextMessage(MESSAGE_STATUS_WARNING, "You added X points to "..target:getName()..".")
        print(""..player:getName().." added X points to "..target:getName()..".")
    elseif split[2] == "get" then -- /points playername, get
        player:sendTextMessage(MESSAGE_STATUS_WARNING, ""..target:getName().." has "..target:getPoints().." points.")
        print(""..player:getName().." checked "..target:getName().." points. Result was: "..target:getPoints().."...")
        -- print(""..player:getName().." checked "..target:getName().." points. Result was: "..target:getPoints().."...")
    elseif split[2] == "remove" then -- /points playername, remove
        target:removePoints(count) -- /points playername, remove, amount
        player:sendTextMessage(MESSAGE_STATUS_WARNING, "You removed points from "..target:getName()..".")
        print(""..player:getName().." removed points from "..target:getName()..".")
    end
        print("PRINT THIS")
return true
end
 
Wow I feel so stupid I didn't check that...

Just tested with separator, split[1] works but split[2] and split[3] not working...

Code:
function Player.getPoints(self)
     local resultId = db.storeQuery("SELECT `premium_points` FROM `accounts` WHERE `id` = " .. self:getAccountId())
     if resultId ~= false then
         local points = result.getDataInt(resultId, 'premium_points')
         result.free(resultId)
         return points
     end

     return 0
end

function Player.addPoints(self, points)
    if not points or type(points) ~= 'number' then
        points = 0
    end

    db.query("UPDATE `accounts` SET `premium_points` = `premium_points` + " .. points .. " WHERE `id` = " .. self:getAccountId())
    return true
end

function Player.removePoints(self, points)
    if not points or type(points) ~= 'number' then
        points = 0
    end

    db.query("UPDATE `accounts` SET `premium_points` = `premium_points` - " .. points .. " WHERE `id` = " .. self:getAccountId())
    return true
end

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

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

    if split[2] == "add" then -- /points playername, add
        target:addPoints(count) -- /points playername, add, amount
    elseif split[2] == "get" then -- /points playername, get
        player:sendTextMessage(MESSAGE_STATUS_WARNING, ""..target:getName().." has "..target:getPoints().." points.")
    elseif split[2] == "remove" then -- /points playername, remove
        target:removePoints(count) -- /points playername, remove, amount
    end
        print("PRINT THIS")
return true
end
 
Last edited:
Instead of "not working", can you explain what happens? How do you type the talkaction ingame and what is the result of that, so descriibe what happens and post the full script.
 
If your having trouble with split just use this function on your string :)
Code:
function gSplit(str)
    local s = {}
    for x in str:gmatch('[0-9a-zA-Z]+') do
        s[#s+1] = x
    end
    return s
end

-- [[ 
string "playername, add, 1"
output :
playername
add
1
]]
 
This command is to add premium points to players, for example:

Code:
/points Limos, add, 10
/points Limos, remove, 10
/points Limos, get

When I execute the command with 1 param (/points Limos) it returns:

Code:
Insufficient parameters.

When I execute the command with 2 param (/points Limos, get) it returns:

Code:
A player with that name is not online.

But if I write the name of a player online (/points Lava Titan, get) nothing happens, not even console errors...

These are the scripts:

Compat.lua
Code:
function Player.getPoints(self)
     local resultId = db.storeQuery("SELECT `premium_points` FROM `accounts` WHERE `id` = " .. self:getAccountId())
     if resultId ~= false then
         local points = result.getDataInt(resultId, 'premium_points')
         result.free(resultId)
         return points
     end

     return 0
end

function Player.addPoints(self, points)
    if not points or type(points) ~= 'number' then
        points = 0
    end

    db.query("UPDATE `accounts` SET `premium_points` = `premium_points` + " .. points .. " WHERE `id` = " .. self:getAccountId())
    return true
end

function Player.removePoints(self, points)
    if not points or type(points) ~= 'number' then
        points = 0
    end

    db.query("UPDATE `accounts` SET `premium_points` = `premium_points` - " .. points .. " WHERE `id` = " .. self:getAccountId())
    return true
end

Command.lua
Code:
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[3] == nil then
            player:sendTextMessage(MESSAGE_STATUS_WARNING, "Insufficient parameters.")
        return false
    end

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

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

    if split[2] == "add" then
        target:addPoints(amount) -- /points playername, add, amount
    elseif split[2] == "get" then -- /points playername, get
        player:sendTextMessage(MESSAGE_STATUS_WARNING, ""..target:getName().." has "..target:getPoints().." points.")
    elseif split[2] == "remove" then
        target:removePoints(amount) -- /points playername, remove, amount
    end
return true
end

This is the script(addskill.lua) where I took the code from to build mine

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

@Codex NG all other commands are using split so I don't think the error is from there, I think I just builded wrong the script
 
You can do this.
Code:
local split = param:split(", ")
This way it also takes the space so split[2] will be "get" instead of " get", or you can also add the spaces in the if/elseif statements.
 
Back
Top