• 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!
  • 2026 staff recruitment is open! Check it out and consider applying!

Lua First items dependent of vocation

CyberShaman21

New Member
Joined
Dec 9, 2017
Messages
52
Reaction score
4
hi
i want give items on first login dependent of vocation
i make this code:
Code:
local firstItems = {2050, 2382}

function onLogin(player)
    if player:getLastLoginSaved() == 0 then
        for i = 1, #firstItems do
            player:addItem(firstItems[i], 1)
        end
        player:addItem(player:getSex() == 0 and 2651 or 2650, 1)
        player:addItem(1987, 1):addItem(2674, 1)
       
        if player:getVocation() == 1 then
            player:addItem(2162, 1)
        end
       
    end
    return true
end
this part is wrong ?
Code:
        if player:getVocation() == 1 then
            player:addItem(2162, 1)

i try make new player with 1 id vocation but dosent work
 
Solution
why this code is working
Code:
player:addItem(1987, 1):addItem(2674, 1)
and this isnt
Code:
player:addItem(1987, 1):addItem(2674, 1):addItem(2120, 1):addItem(2689, 1)
this dont work too
Code:
        player:addItem(1987, 1):addItem(2674, 1)
        player:addItem(2120, 1):addItem(2689, 1)
I broke the script down for you step by step :)
LUA:
local firstItems = {2050, 2382, 1987, 2674} -- firstItems a table of items which give all players the same items
local otherItems = { -- otherItems is a table which gives specific vocations based on their id different items
    [1] = { -- each index of this table corresponds to a specific vocation 1 being sorcerer
        {
            id = 2162, -- id is the itemid of the item to add...
LUA:
player:getVocation():getId()
and then you can do something like this.
LUA:
local firstItems = {2050, 2382}
local otherItems = {
    [1] = {
        {id = 2162, amount = 1},
    },
}

function onLogin(player)
    if player:getLastLoginSaved() == 0 then
        for i = 1, #firstItems do
            player:addItem(firstItems[i], 1)
        end
        player:addItem(player:getSex() == 0 and 2651 or 2650, 1)
        player:addItem(1987, 1):addItem(2674, 1)

        local vocId = player:getVocation():getId()
        for _, items in pairs(otherItems[vocId]) do
            player:addItem(items.id, items.amount)
        end
    end
    return true
end
 
Last edited:
Code:
local firstItems = {2050, 2461, 2467, 2649, 2643}
function onLogin(player)
    if player:getLastLoginSaved() == 0 then
        for i = 1, #firstItems do
            player:addItem(firstItems[i], 1)
        end
      
        if player:getVocation():getId() == 1 then
            player:addItem(2376, 1)
        elseif player:getVocation():getId() == 2 then
            player:addItem(2386, 1)
          
          
        player:addItem(1987, 1):addItem(2674, 1)
     
      
        end
     
    end
    return true
end

i make easy code, i cant understend you code. what is programming language c++?
Linxsis thanks for help
 
Last edited:
Im sorry to say it, but im pretty damn sure that is not your code..
the reason, you think Linxsis code is c++, that shows that you have no idea what so ever..
 
i make easy code, i cant understend you code. what is programming language c++?
Linxsis thanks for help
No its lua.
Im sorry to say it, but im pretty damn sure that is not your code..
the reason, you think Linxsis code is c++, that shows that you have no idea what so ever..
Well at least he/she is trying :)
 
LUA:
local vocations = { --Items given depending on vocation [1] is vocation 1: sorcerer
[1] = {1111, 1111, 1111, 1111},
[2] = {1111, 1111, 1111, 1111},
[3] = {1111, 1111, 1111, 1111},
[4] = {1111, 1111, 1111, 1111}
}

local items_all = {1111, 1111} --Items given to all players no matter vocation

function onLogin(player)
    if player:getLastLoginSaved() == 0 then
        VOC = vocations[player:getVocation():getId()]
      
        for i = 1, #items_all do
            player:addItem(items_all[i], 1)
        end
      
        if not VOC then return true end
      
        for i = 1, #VOC do
            player:addItem(VOC[i], 1)
        end
    end
return true
end
 
why this code is working
Code:
player:addItem(1987, 1):addItem(2674, 1)
and this isnt
Code:
player:addItem(1987, 1):addItem(2674, 1):addItem(2120, 1):addItem(2689, 1)
this dont work too
Code:
        player:addItem(1987, 1):addItem(2674, 1)
        player:addItem(2120, 1):addItem(2689, 1)
 
why this code is working
Code:
player:addItem(1987, 1):addItem(2674, 1)
and this isnt
Code:
player:addItem(1987, 1):addItem(2674, 1):addItem(2120, 1):addItem(2689, 1)
this dont work too
Code:
        player:addItem(1987, 1):addItem(2674, 1)
        player:addItem(2120, 1):addItem(2689, 1)
I broke the script down for you step by step :)
LUA:
local firstItems = {2050, 2382, 1987, 2674} -- firstItems a table of items which give all players the same items
local otherItems = { -- otherItems is a table which gives specific vocations based on their id different items
    [1] = { -- each index of this table corresponds to a specific vocation 1 being sorcerer
        {
            id = 2162, -- id is the itemid of the item to add
            amount = 1 -- amount is the amount of that item to add
        },
        --[[
            to create a new set of both id (itemid) and amount just copy and paste this template
            {id = 1111, amount = 1},
            replace the 1's with the itemid of the item and the amount
        ]]
    },
    [2] = { -- 2 being druid
        {id = 2162, amount = 1},
    },
    [3] = { -- 3 being paladin
        {id = 2162, amount = 1},
    },
    [4] = { -- and 4 being knight
        {id = 2162, amount = 1},
    },
}
function onLogin(player)
    -- the server asks if the player has ever logged in
    if player:getLastLoginSaved() == 0 then
        -- if they haven't then cycle through the firstItems
        for i = 1, #firstItems do
            -- and give the player 1 item based on the itemid in each index of firstItems
            player:addItem(firstItems[i], 1)
        end
        -- next it wants to assign a player another based on its sex
        player:addItem(player:getSex() == 0 and 2651 or 2650, 1)
        -- get the vocation id of the player and store it in vocId
        local vocId = player:getVocation():getId()
        -- assign vocationItems a table which may or may not exist in otherItems
        local vocationItems = otherItems[vocId]
        -- now we will check to see if vocationItems is a table which contains values
        if not next(vocationItems) then
            -- if not return true and end the script
            return true
        end
        -- if it does then cycle through the vocationItems table
        for _, items in pairs(vocationItems) do
            -- and add the items based on its itemid and the amount
            player:addItem(items.id, items.amount)
        end
    end
    -- return true end the script
    return true
end
This has been updated, i had firstItems labeled as just first.
 
Last edited:
Solution
why here is " i= 1"
Code:
for i = 1, #firstItems do
and here is for _,
Code:
 for _, items in pairs(vocationItems) do
i and _ are local variables, they contain a temporary value which will not exist when the for loop ends, this value is either firstItems index which is assigned to i and the _ holds vocationItems index, the reason I used an _ is because its common place in lua to use it when the value is of no significance (not part of the script).

If you found a solution please mark the best answer as it will help others :)
 
Back
Top