• 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 to all players

henkas

Well-Known Member
Joined
Jul 8, 2015
Messages
989
Solutions
5
Reaction score
54
Hi does anyone have or can convert this scrip into TFS 1.2? Basically it gives item to all but it doesnt give item to MC players
Lua:
function onSay(cid, words, param, channel)
    local t = string.explode(param, ",")
    if t[1] ~= nil and t[2] ~= nil then
        doBroadcastMessage(getPlayerName(cid) .. " give: " .. t[2] .." ".. getItemNameById(t[1]) .. " for all players online!")
        local list = {}
        for i, player in ipairs(getPlayersOnline()) do
            local playerIp = getPlayerIp(player)
            if not list[tostring(playerIp)] then
                list[tostring(playerIp)] = player
            end
        end
        if next(list) then
            for ip, pid in pairs(list) do
                if isPlayer(pid) then
                    doPlayerAddItem(pid, t[1], t[2])
                end
            end
        end
    else
        doPlayerPopupFYI(cid, "No param...\nSend:\n /itemadd itemid,how_much_items\nexample:\n /itemadd 2160,10")
    end
    return true
end
 
Solution
if it helps you.
this command sends articles to all players online, only 1 player per ip.

Lua:
-- <talkaction words="/addItemAllPjs" separator=" " script="add_item_allpjs.lua" />
-- /addItemAllPjs itemId, count

local function getPlayerDiffIps()
    local players = Game.getPlayers()
    local ipList = {}
    for index, player in pairs(players) do
        if not ipList[player:getIp()] then
            ipList[player:getIp()] = player
        end
    end
    return ipList
end

function onSay(player, words, param)
    if not player:getGroup():getAccess() then
        return true
    elseif player:getAccountType() < ACCOUNT_TYPE_GOD then
        return false
    end
    local split = param:split(",")
    local itemId =...
I'll teach you a small trick to identify where the issue is when there's no errors. The old print trick xD

Each time you have a new block (if, while, for) you insert a print. It goes like this:
Lua:
print(0) -- this will print 0 when the script is loaded, meaning it is installed correctly

function onSay(cid, words, param, channel)
    print(1) -- printing the function is being called correctly in the talkaction
    local t = string.split(param, ",")
    if t[1] ~= nil and t[2] ~= nil then
        print(2) -- printing the parameters aren't nil
        doBroadcastMessage(getPlayerName(cid) .. " give: " .. t[2] .." ".. getItemNameById(t[1]) .. " for all players online!")
        local list = {}
        print("3. ".. #getPlayersOnline()) -- printing the number of online players
        for i, player in ipairs(getPlayersOnline()) do           
            local playerIp = getPlayerIp(player)
            if not list[tostring(playerIp)] then
                print("ip not listed, adding to table")
                list[tostring(playerIp)] = player
            end
        end
        print("for executed, now let's check the list")
        if next(list) then
            print(4, #list) -- checking if list has elements and printing how many
            for ip, pid in pairs(list) do
                if isPlayer(pid) then
                    print("player found, adding item")
                    doPlayerAddItem(pid, t[1], t[2])
                end
            end
        end
    else
        doPlayerPopupFYI(cid, "No param...\nSend:\n /itemadd itemid,how_much_items\nexample:\n /itemadd 2160,10")
    end
    return true
end
Now just run this once and see the console. Send me a print of what it shows in console and then we'll have a clue where the error might be.
 
I'll teach you a small trick to identify where the issue is when there's no errors. The old print trick xD

Each time you have a new block (if, while, for) you insert a print. It goes like this:
Lua:
print(0) -- this will print 0 when the script is loaded, meaning it is installed correctly

function onSay(cid, words, param, channel)
    print(1) -- printing the function is being called correctly in the talkaction
    local t = string.split(param, ",")
    if t[1] ~= nil and t[2] ~= nil then
        print(2) -- printing the parameters aren't nil
        doBroadcastMessage(getPlayerName(cid) .. " give: " .. t[2] .." ".. getItemNameById(t[1]) .. " for all players online!")
        local list = {}
        print("3. ".. #getPlayersOnline()) -- printing the number of online players
        for i, player in ipairs(getPlayersOnline()) do         
            local playerIp = getPlayerIp(player)
            if not list[tostring(playerIp)] then
                print("ip not listed, adding to table")
                list[tostring(playerIp)] = player
            end
        end
        print("for executed, now let's check the list")
        if next(list) then
            print(4, #list) -- checking if list has elements and printing how many
            for ip, pid in pairs(list) do
                if isPlayer(pid) then
                    print("player found, adding item")
                    doPlayerAddItem(pid, t[1], t[2])
                end
            end
        end
    else
        doPlayerPopupFYI(cid, "No param...\nSend:\n /itemadd itemid,how_much_items\nexample:\n /itemadd 2160,10")
    end
    return true
end
Now just run this once and see the console. Send me a print of what it shows in console and then we'll have a clue where the error might be.
Okay thanks (y). So it prints 1 when i go /itemadd but if i go /itemadd 2160, 10 it prints nothing
 
show me how it is declared in XML and right below
local t = string.split(param, ",")
insert this line here:

for i,v in ipairs(t) print(i,t) end
XML:
    <talkaction words="/itemadd" script="itemadd.lua" />
Ok

Edit: Can not load script 'do' expected near 'print'
 
You need to add the following to the XML element above:
XML:
separator=" "
 
if it helps you.
this command sends articles to all players online, only 1 player per ip.

Lua:
-- <talkaction words="/addItemAllPjs" separator=" " script="add_item_allpjs.lua" />
-- /addItemAllPjs itemId, count

local function getPlayerDiffIps()
    local players = Game.getPlayers()
    local ipList = {}
    for index, player in pairs(players) do
        if not ipList[player:getIp()] then
            ipList[player:getIp()] = player
        end
    end
    return ipList
end

function onSay(player, words, param)
    if not player:getGroup():getAccess() then
        return true
    elseif player:getAccountType() < ACCOUNT_TYPE_GOD then
        return false
    end
    local split = param:split(",")
    local itemId = tonumber(split[1]) or 0
    local count = tonumber(split[2]) or 1
    local itemType = ItemType(itemId)
    if itemType:getId() == 0 then
        itemType = ItemType(tostring(split[1]))
        if itemType:getId() == 0 then
            player:sendCancelMessage("ID or Name of the wrong item.")
            return false
        end
    end
    count = math.floor(count)
    count = math.max(1, count)
    if itemType:isStackable() then
        count = math.min(100, count)
    else
        count = math.min(1, count)
    end
    for index, pla in pairs(getPlayerDiffIps()) do
        if pla:getFreeCapacity() >= itemType:getWeight(count) then
            pla:addItem(itemType:getId(), count)
        end
    end
    Game.broadcastMessage(string.format("All the players have received %u %s.", count, itemType:getName()))
    return false
end
 
Last edited:
Solution
You need to add the following to the XML element above:
XML:
separator=" "
Yes now it works but i has error attempt to call global 'getItemNameById' btw what that separator does?

if it helps you.
Lua:
-- <talkaction words="/addItemAllPjs" separator=" " script="add_item_allpjs.lua" />
-- /addItemAllPjs itemId, count

local function getPlayerDiffIps()
    local players = Game.getPlayers()
    local ipList = {}
    for index, player in pairs(players) do
        if not ipList[player:getIp()] then
            ipList[player:getIp()] = player
        end
    end
    return ipList
end

function onSay(player, words, param)
    if not player:getGroup():getAccess() then
        return true
    elseif player:getAccountType() < ACCOUNT_TYPE_GOD then
        return false
    end
    local split = param:split(",")
    local itemId = tonumber(split[1]) or 0
    local count = tonumber(split[2]) or 1
    local itemType = ItemType(itemId)
    if itemType:getId() == 0 then
        itemType = ItemType(tostring(split[1]))
        if itemType:getId() == 0 then
            player:sendCancelMessage("ID or Name of the wrong item.")
            return false
        end
    end
    count = math.floor(count)
    count = math.max(1, count)
    if itemType:isStackable() then
        count = math.min(100, count)
    else
        count = math.min(1, count)
    end
    for index, pla in pairs(getPlayerDiffIps()) do
        if pla:getFreeCapacity() >= itemType:getWeight(count) then
            pla:addItem(itemType:getId(), count)
            pla:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, string.format("You have received an [ %u %s ] from the server.", count, itemType:getName()))
        end
    end
    return false
end
Yes this one works but im pretty sure mc players will get items to? And how to add broadcast instead of sendTextmessage
 
separator is what separates the talkaction you said to the param you send.
example:

"/addskill experience, 100"

using separator as " " (space), we will simply consider everything besides the first space found. So our parameter "param" will be "experience, 100"
that way when you split it using "," you'll get two values in a table: t = {"experience", "100"}.
The code from sarah will work as you want
 
separator is what separates the talkaction you said to the param you send.
example:

"/addskill experience, 100"

using separator as " " (space), we will simply consider everything besides the first space found. So our parameter "param" will be "experience, 100"
that way when you split it using "," you'll get two values in a table: t = {"experience", "100"}.
The code from sarah will work as you want
Appreciate your support guys
 
Back
Top