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

Give item talkaction for tfs 12.

SixNine

Active Member
Joined
Dec 12, 2018
Messages
442
Reaction score
40
Hello,
how to edit it for tfs 1,2?
Lua:
function onSay(cid, words, param, channel)

local param = string.explode(param, ",")
local item =
{
    player = getPlayerByNameWildcard(param[1]),
    itemid = tonumber(param[2]),
    type = tonumber(param[3]),
    charges = 1
}
local str =
{
    "",
    ""
}

    if(item.player == 0 or item.player == nil) then
        doPlayerSendCancel(cid, "Player " .. param[1] .. " is not online.")
        doSendMagicEffect(getCreaturePosition(cid), CONST_ME_POFF)
        return TRUE
    end

    if(not item.itemid) then
        item.itemid = getItemIdByName(param[2], false)
        if not item.itemid then
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Item wich such name does not exists.")
            doSendMagicEffect(getCreaturePosition(cid), CONST_ME_POFF)
            return TRUE
        end
    end

    if(item.itemid < 1) then
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "No item specified.")
        doSendMagicEffect(getCreaturePosition(cid), CONST_ME_POFF)
        return TRUE
    end

    if(not item.type) then
        if(isItemRune(item.itemid) or isItemStackable(item.itemid)) == TRUE then
            item.type = 100
            item.charges = 1
        else
            item.type = 1
            item.charges = 1
        end
    end

    if(isItemMovable(item.itemid) == FALSE) then
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You cannot give that item.")
        doSendMagicEffect(getCreaturePosition(cid), CONST_ME_POFF)
        return TRUE
    end

local str =
{
    "You give " .. item.type .."x " .. getItemNameById(item.itemid) .. " to " .. param[1] .. ".",
    "You received " .. item.type .. "x " .. getItemNameById(item.itemid) .. " from " .. getCreatureName(cid) .. "."
}

    if(isItemRune(item.itemid) == TRUE) then
        item.charges = item.type
        item.type = 1
        str[1] = "You give " .. item.type .. "x " .. getItemNameById(item.itemid) .. " with " .. item.charges .. "x charges to " .. param[1] .. "."
        str[2] = "You received " .. item.type .. "x " .. getItemNameById(item.itemid) .. " with " .. item.charges .. " charges from " .. getCreatureName(cid) .. "."
    end

    doPlayerGiveItem(item.player, item.itemid, item.type, item.charges)
    doSendMagicEffect(getCreaturePosition(cid), CONST_ME_MAGIC_RED)
    doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, str[1])
    doSendMagicEffect(getCreaturePosition(item.player), CONST_ME_MAGIC_RED)
    doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, str[2])
    return TRUE
end
 
Solution
Here is a small example:
/additem name,id or name [, count]
Code:
function onSay(player, words, param)
    local split = param:split(",")
    local target = split[1] and Player(split[1])
    if not target then
        return player:sendCancelMessage("This player does not exist.")
    end
    local itemType = split[2] and ItemType(split[2])
    if not itemType or itemType:getId() == 0 then
        itemType = split[2] and ItemType(tonumber(split[2]))
        if not itemType or itemType:getId() == 0 then
            return player:sendCancelMessage("This item does not exist.")
        end
    end
    local count = 1
    if split[3] then count = tonumber(split[3]) or 1 end
    if target:addItem(itemType:getId(), count) then...
Post what you've done so far.
Also, you can change getItemNameById to use ItemType(id) instead
So this is what i have made so far
Lua:
function onSay(cid, words, param, channel)

local param = param:split(",")
local item =
{
    player = getPlayerByName(param[1]),
    itemid = tonumber(param[2]),
    type = tonumber(param[3]),
    charges = 1
}
local str =
{
    "",
    ""
}

    if(item.player == 0 or item.player == nil) then
        doPlayerSendCancel(cid, "Player " .. param[1] .. " is not online.")
        doSendMagicEffect(getCreaturePosition(cid), CONST_ME_POFF)
        return TRUE
    end

    if(not item.itemid) then
        item.itemid = getItemIdByName(param[2], false)
        if not item.itemid then
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Item wich such name does not exists.")
            doSendMagicEffect(getCreaturePosition(cid), CONST_ME_POFF)
            return TRUE
        end
    end

    if(item.itemid < 1) then
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "No item specified.")
        doSendMagicEffect(getCreaturePosition(cid), CONST_ME_POFF)
        return TRUE
    end

    if(not item.type) then
        if(isItemRune(item.itemid) or isItemStackable(item.itemid)) == TRUE then
            item.type = 100
            item.charges = 1
        else
            item.type = 1
            item.charges = 1
        end
    end

    if(isItemMovable(item.itemid) == FALSE) then
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You cannot give that item.")
        doSendMagicEffect(getCreaturePosition(cid), CONST_ME_POFF)
        return TRUE
    end

local str =
{
    "You give " .. item.type .."x " .. ItemType(item.itemid) .. " to " .. param[1] .. ".",
    "You received " .. item.type .. "x " .. ItemType(item.itemid) .. " from " .. getCreatureName(cid) .. "."
}

    if(isItemRune(item.itemid) == TRUE) then
        item.charges = item.type
        item.type = 1
        str[1] = "You give " .. item.type .. "x " .. ItemType(item.itemid) .. " with " .. item.charges .. "x charges to " .. param[1] .. "."
        str[2] = "You received " .. item.type .. "x " .. ItemType(item.itemid) .. " with " .. item.charges .. " charges from " .. getCreatureName(cid) .. "."
    end

    doPlayerGiveItem(item.player, item.itemid, item.type, item.charges)
    doSendMagicEffect(getCreaturePosition(cid), CONST_ME_MAGIC_RED)
    doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, str[1])
    doSendMagicEffect(getCreaturePosition(item.player), CONST_ME_MAGIC_RED)
    doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, str[2])
    return TRUE
end
attempt to concatenate a userdata value.
 
So this is what i have made so far
Lua:
function onSay(cid, words, param, channel)

local param = param:split(",")
local item =
{
    player = getPlayerByName(param[1]),
    itemid = tonumber(param[2]),
    type = tonumber(param[3]),
    charges = 1
}
local str =
{
    "",
    ""
}

    if(item.player == 0 or item.player == nil) then
        doPlayerSendCancel(cid, "Player " .. param[1] .. " is not online.")
        doSendMagicEffect(getCreaturePosition(cid), CONST_ME_POFF)
        return TRUE
    end

    if(not item.itemid) then
        item.itemid = getItemIdByName(param[2], false)
        if not item.itemid then
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Item wich such name does not exists.")
            doSendMagicEffect(getCreaturePosition(cid), CONST_ME_POFF)
            return TRUE
        end
    end

    if(item.itemid < 1) then
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "No item specified.")
        doSendMagicEffect(getCreaturePosition(cid), CONST_ME_POFF)
        return TRUE
    end

    if(not item.type) then
        if(isItemRune(item.itemid) or isItemStackable(item.itemid)) == TRUE then
            item.type = 100
            item.charges = 1
        else
            item.type = 1
            item.charges = 1
        end
    end

    if(isItemMovable(item.itemid) == FALSE) then
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You cannot give that item.")
        doSendMagicEffect(getCreaturePosition(cid), CONST_ME_POFF)
        return TRUE
    end

local str =
{
    "You give " .. item.type .."x " .. ItemType(item.itemid) .. " to " .. param[1] .. ".",
    "You received " .. item.type .. "x " .. ItemType(item.itemid) .. " from " .. getCreatureName(cid) .. "."
}

    if(isItemRune(item.itemid) == TRUE) then
        item.charges = item.type
        item.type = 1
        str[1] = "You give " .. item.type .. "x " .. ItemType(item.itemid) .. " with " .. item.charges .. "x charges to " .. param[1] .. "."
        str[2] = "You received " .. item.type .. "x " .. ItemType(item.itemid) .. " with " .. item.charges .. " charges from " .. getCreatureName(cid) .. "."
    end

    doPlayerGiveItem(item.player, item.itemid, item.type, item.charges)
    doSendMagicEffect(getCreaturePosition(cid), CONST_ME_MAGIC_RED)
    doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, str[1])
    doSendMagicEffect(getCreaturePosition(item.player), CONST_ME_MAGIC_RED)
    doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, str[2])
    return TRUE
end
attempt to concatenate a userdata value.
Here's a list of Lua functions you can use (just download documentation.txt)
ItemType(...) returns a userdata, methods can be executed using this userdata to return certain values you want. ItemType(item.itemid):getName() for example.
Honestly what I'd do is copy the /i command and add a 3rd parameter for a player name and just replace the object of player:addItem with the target player & send a message to the target.
Here's an example of what I mean:
Lua:
-- right below "local split"
local target = Player(split[3]) -- another userdata if split[3] is a valid player name that's online, check function list i sent for usable methods
if not target then
    return false
end
-- other code here from /i command
local result = target:addItem(itemType:getId(), count) -- should replace the result variable in /i command
-- rest of the code that handles result in /i command
target:getPosition():sendMagicEffect(CONST_ME_MAGIC_GREEN) -- replacing the /i magic effect to our target player
 
Here is a small example:
/additem name,id or name [, count]
Code:
function onSay(player, words, param)
    local split = param:split(",")
    local target = split[1] and Player(split[1])
    if not target then
        return player:sendCancelMessage("This player does not exist.")
    end
    local itemType = split[2] and ItemType(split[2])
    if not itemType or itemType:getId() == 0 then
        itemType = split[2] and ItemType(tonumber(split[2]))
        if not itemType or itemType:getId() == 0 then
            return player:sendCancelMessage("This item does not exist.")
        end
    end
    local count = 1
    if split[3] then count = tonumber(split[3]) or 1 end
    if target:addItem(itemType:getId(), count) then
        target:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, string.format("You have received an %u %s.", count, itemType:getName()))
        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, string.format("You have given an %u %s to the %s.", count, itemType:getName(), split[1]))
    else
        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, string.format("The player %s did not receive the %u %s.", split[1], count, itemType:getName()))
    end
    return false
end

It is not the best code of all, but it will help you understand several things, I hope it is of your help.
 
Last edited:
Solution
Here's a list of Lua functions you can use (just download documentation.txt)
ItemType(...) returns a userdata, methods can be executed using this userdata to return certain values you want. ItemType(item.itemid):getName() for example.
Honestly what I'd do is copy the /i command and add a 3rd parameter for a player name and just replace the object of player:addItem with the target player & send a message to the target.
Here's an example of what I mean:
Lua:
-- right below "local split"
local target = Player(split[3]) -- another userdata if split[3] is a valid player name that's online, check function list i sent for usable methods
if not target then
    return false
end
-- other code here from /i command
local result = target:addItem(itemType:getId(), count) -- should replace the result variable in /i command
-- rest of the code that handles result in /i command
target:getPosition():sendMagicEffect(CONST_ME_MAGIC_GREEN) -- replacing the /i magic effect to our target player
Okay so if you want to send message to target it should be like this?
Lua:
    return target:addItem(itemType:getId(), count) and target:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, string.format("You received %s.", split[1]))
i added it but it says expected to close function, i added it below player return so its like
Lua:
    return target:addItem(itemType:getId(), count) and player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, string.format("You give item to player %s.", split[1]))
    return target:addItem(itemType:getId(), count) and target:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, string.format("You received %s.", split[1]))
end
Here is a small example:
/additem name,id or name [, count]
Code:
function onSay(player, words, param)
    local split = param:split(",")
    local target = split[1] and Player(split[1])
    if not target then
        return player:sendCancelMessage("This player does not exist.")
    end
    local itemType = split[2] and ItemType(split[2])
    if not itemType or itemType:getId() == 0 then
        itemType = split[2] and ItemType(tonumber(split[2]))
        if not itemType or itemType:getId() == 0 then
            return player:sendCancelMessage("This item does not exist.")
        end
    end
    local count = 1
    if split[3] then
        count = tonumber(split[3]) or 1
    end
    return target:addItem(itemType:getId(), count) and player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, string.format("You give item to player %s.", split[1]))
end

It is not the best code of all, but it will help you understand several things, I hope it is of your help.
Yea your code works but it doesnt work with item name only with id's
 
Okay so if you want to send message to target it should be like this?
Lua:
    return target:addItem(itemType:getId(), count) and target:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, string.format("You received %s.", split[1]))
i added it but it says expected to close function, i added it below player return so its like
Lua:
    return target:addItem(itemType:getId(), count) and player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, string.format("You give item to player %s.", split[1]))
    return target:addItem(itemType:getId(), count) and target:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, string.format("You received %s.", split[1]))
end

Yea your code works but it doesnt work with item name only with id's
You're supposed to replace, not add a 2nd return.
Either way you shouldn't even use a return like that, it's shorter but much messier and isn't 100% explicit about what the actual return value is.
Lua:
local result = target:addItem(...)
if result then
    target:sendTextMessage(...)
end
return true
 
You're supposed to replace, not add a 2nd return.
Either way you shouldn't even use a return like that, it's shorter but much messier and isn't 100% explicit about what the actual return value is.
Lua:
local result = target:addItem(...)
if result then
    target:sendTextMessage(...)
end
return true
No i cant replace to another return it should send message to player and target. So with your option it doesnt send message to target but it sends to player
Lua:
local result = target:addItem(player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, string.format("You give item to player %s.", split[1])))
if result then
    target:sendTextMessage(target:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, string.format("You received %s.", split[1])))
end
end
return true
had to add another end because it said it have to be closed or something like that
 
No i cant replace to another return it should send message to player and target. So with your option it doesnt send message to target but it sends to player
Lua:
local result = target:addItem(player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, string.format("You give item to player %s.", split[1])))
if result then
    target:sendTextMessage(target:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, string.format("You received %s.", split[1])))
end
end
return true
had to add another end because it said it have to be closed or something like that
Why do you have the message inside of addItem? That's not how it works.
You're calling player:sendTextMessage inside of addItem. That sendTextMessage returns true and then you're basically trying to do target:addItem(true), which won't give the target an item because true isn't an item id or name, which results in the "result" variable to be nil because there was no item added to the player, which is why it's sending the message to player and not target.
If you go back to my original post you'll see what the line should look like.
 
Why do you have the message inside of addItem? That's not how it works.
You're calling player:sendTextMessage inside of addItem. That sendTextMessage returns true and then you're basically trying to do target:addItem(true), which won't give the target an item because true isn't an item id or name, which results in the "result" variable to be nil because there was no item added to the player, which is why it's sending the message to player and not target.
If you go back to my original post you'll see what the line should look like.
A okay got you, works now just fine
Lua:
local result = target:addItem(itemType:getId(), count)
if result then
     player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, string.format("You give item to player %s.", split[1]))
     target:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, string.format("You received  %u %s.", count, itemType:getName()))
end
end
return true
Thanks for the help guys
 
I added an improvement in the messages:
 
Can you posted the script?.
Mine version or you can use Sarah
Lua:
function onSay(player, words, param)
    local split = param:split(",")
    local target = split[1] and Player(split[1])
    if not target then
        return player:sendCancelMessage("This player does not exist.")
    end
    local itemType = split[2] and ItemType(split[2])
    if not itemType or itemType:getId() == 0 then
        itemType = split[2] and ItemType(tonumber(split[2]))
        if not itemType or itemType:getId() == 0 then
            return player:sendCancelMessage("This item does not exist.")
        end
    end
    local count = 1
    if split[3] then
        count = tonumber(split[3]) or 1
    end
local result = target:addItem(itemType:getId(), count)
if result then
     player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, string.format("You gave %u %s to player %s.", count, itemType:getName(), split[1]))
     target:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, string.format("You received %u %s.", count, itemType:getName()))
end
end
return true
I added an improvement in the messages:
Btw is it possible to give item offline player i know that it is somehow :D So it would be extra dope
 
Back
Top