• 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 [TFS 1.3] Being able to "select" a player through a talkaction param.

aqubjukr

Well-Known Member
Joined
Mar 13, 2013
Messages
200
Solutions
17
Reaction score
79
Location
Paraná, Brazil
I have the following problem: When using a talkaction with param, it always returns "nil" if the player is offline.
(If the player is online everything goes well)

Example: If I type: "/player Maker" (when he is offline), it will be printed on the console: nil.

I was wondering if there is a possibility to correct this, bcos I'm breaking my head and I didn't succeed.

Script that I'm using:
Lua:
local player = TalkAction("/player")

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

    if cid:getAccountType() < ACCOUNT_TYPE_GAMEMASTER then
        return false
    end

    logCommand(Player(cid), words, param)

    if(param == "") then
        cid:sendCancelMessage("Need param.")
    return false
    end

    local player = Player(param)

    if player and player:isPlayer() then
        local text = "Player name: "..Creature(player):getName().."."
        cid:showTextDialog(6528, text)
    else
        print(player)
    end
    return false
end

player:separator(" ")
player:register()
 
Solution
You can use this:
Lua:
local talkAction = TalkAction("/player")

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

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

    logCommand(player, words, param)

    if(param == "") then
        player:sendCancelMessage("Need param.")
    return false
    end

    local target = Player(param, true)
    if target then
        local text = "Player name: "..target:getName()..".\n"
        local storage = "Player storage: " ..target:getStorageValue(666).."."
        local itemLeftHand = target:getSlotItem(CONST_SLOT_LEFT)
        if itemLeftHand then
            text = text .. 'item left...
look how this script handles online/offline player (line 7 to 15):

Lua:
function Player.transferMoneyTo(self, target, amount)
    local balance = self:getBankBalance()
    if amount > balance then
        return false
    end

    local targetPlayer = Player(target)
    if targetPlayer then
        targetPlayer:setBankBalance(targetPlayer:getBankBalance() + amount)
    else
        if not playerExists(target) then
            return false
        end
        db.query("UPDATE `players` SET `balance` = `balance` + '" .. amount .. "' WHERE `name` = " .. db.escapeString(target))
    end

    self:setBankBalance(self:getBankBalance() - amount)
    return true
end
 
Sorry, but I couldn't understand what you wanted to go through. I believe that because a param is used, it works differently.

Even though I shortened the script even more, it gives nil when using :x
Lua:
local player = TalkAction("/player")

function player.onSay(cid, words, param)
    local targetPlayer = Player(param)
    print(targetPlayer)
    return false
end

player:separator(" ")
player:register()

1613506276932.png
 
why not:

Lua:
local talkp = TalkAction("/player")

function talkp.onSay(player, words, param)
    local targetPlayer = Player(param)
    if targetPlayer
        print(targetPlayer)
    end
    return false
end

talkp:separator(" ")
talkp:register()
 
I have the following problem: When using a talkaction with param, it always returns "nil" if the player is offline.
(If the player is online everything goes well)

Example: If I type: "/player Maker" (when he is offline), it will be printed on the console: nil.

I was wondering if there is a possibility to correct this, bcos I'm breaking my head and I didn't succeed.

Script that I'm using:
Lua:
local player = TalkAction("/player")

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

    if cid:getAccountType() < ACCOUNT_TYPE_GAMEMASTER then
        return false
    end

    logCommand(Player(cid), words, param)

    if(param == "") then
        cid:sendCancelMessage("Need param.")
    return false
    end

    local player = Player(param)

    if player and player:isPlayer() then
        local text = "Player name: "..Creature(player):getName().."."
        cid:showTextDialog(6528, text)
    else
        print(player)
    end
    return false
end

player:separator(" ")
player:register()
Remove line 24 (else) and line 25 (print(player))
Post automatically merged:

If you need that it works even with player offline, then it's not possible (unless doing sql queries).
There is an open issue for this Working on offline player from lua · Issue #3298 · otland/forgottenserver (https://github.com/otland/forgottenserver/issues/3298)
 
Last edited:
You can use this:
Lua:
local talkAction = TalkAction("/player")

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

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

    logCommand(player, words, param)

    if(param == "") then
        player:sendCancelMessage("Need param.")
    return false
    end

    local target = Player(param, true)
    if target then
        local text = "Player name: "..target:getName()..".\n"
        local storage = "Player storage: " ..target:getStorageValue(666).."."
        local itemLeftHand = target:getSlotItem(CONST_SLOT_LEFT)
        if itemLeftHand then
            text = text .. 'item left hand: '..itemLeftHand:getName()..'.\n'
        end
        player:showTextDialog(6528, text..storage)
    end
    return false
end

talkAction:separator(" ")
talkAction:register()
if you merge this changes in your sources, this work!
1613518623021.png

in case you modify something in the player do not forget to execute target:save() at the end

Note that the changes are not reviewed by anyone, and it is not confirmed that it works correctly, the only evidence is my own tests, which were all successful, but it is very possible that there are errors that I still do not know
 
Last edited:
Solution
Back
Top