• This is a read only backup of the old Emudevs forum. If you want to have anything removed, please message me on Discord: KittyKaev

Add a table instead of "numbers"

Status
Not open for further replies.

Neth

BETA Tester
Hi, i've been stuck for a good 20 minutes, i've been looking over the internet but I can't seem to type the correct keyword to findthe solution to my problem.

my problem is the following :

bad argument #2 to 'EquipItem' (number expected, got table)

Code:
for k, v in ipairs(On.Menu["Class"]) do
	    player:EquipItem(On.Menu, On.Slots)
	end

but I really want to use my table, not numbers.
Code:
On = {}

On.Menu = {
    ["Class"] = {
        {"Warrior",     1},
		{"PaladOn",     2},
		{"Hunter",      3},
		{"Rogue",       4},
		{"Priest",      5},
		{"DeathKnight", 6},
		{"Shaman",      7},
		{"Mage",        8},
		{"Warlock",     9},
		{"Druid",      11};
	},
	
	[1] = { -- Warrior gear S8
	        {"Wrathful Gladiator's Plate Chestpiece",  51541},
			{"Wrathful Gladiator's Plate Gauntlets",   51542},
			{"Wrathful Gladiator's Plate Helm",        51543},
			{"Wrathful Gladiator's Plate Legguards",   51544},
			{"Wrathful Gladiator's Plate Shoulders",   51545};
		},
I have 9 more of them(empty tho), my question is : how can I add my table on EquipItem method ?
 

Foereaper

Founder
k returns the key, v returns the value of said key. When you iterate On.Menu["Class"], your k would be from 1 to 10, while v would be the table structure of On.Menu["Class"],

ie. logic:

Code:
if k == 1 then v == {"Warrior",     1}
if k == 2 then v == {"PaladOn",     2}

and so on. To get the correct value from v (which is a subtable of On.Menu["Class"]), you would have to do

Code:
v[1] == "Warrior"
v[2] == 1

v[1] == "Paladin"
v[2] == 2

if that makes sense :p

An example for you to try:

Code:
local T = {{"Warrior", 1}, {"Paladin", 2}, {"Hunter", 3}}

for k, v in pairs(T) do
	print("Key: ", k)
	print("Value: ", v)
	print("Subtable values:", v[1], v[2])
	print("------")
end

Paste that here and click run: http://www.lua.org/cgi-bin/demo
 

Rochet2

Moderator / Eluna Dev
Could you give us an example or something?
For example show a table you want to use with EquipItem.
And I mean all of it. We cant see On.Slots in your posts and On.Menu is not an item nor an item entry, so it cant work like you have it in your first code snippet at all.

The method requires a slot. So you need to do like
Code:
local T = {
    ["Warrior"] = { -- Can also use the class IDs as well ofc
        {entry, slot},
        {entry, slot},
        {entry, slot},
        {entry, slot},
    },
    ["Paladin"] = {
        {entry, slot},
        {entry, slot},
        {entry, slot},
        {entry, slot},
    },
}

for k,v in ipairs(T[player:GetClassAsString()]) do -- k is id and v is T[class][id]
    player:EquipItem(v[1], v[2]) -- entry, slot
end
Or you could use the slot as a key
 

Neth

BETA Tester
Code:
--[[
    EmuDevs <http://emudevs.com/forum.php>
    Eluna Lua EngOne <https://github.com/ElunaLuaEngOne/Eluna>
    Eluna Scripts <https://github.com/ElunaLuaEngOne/Scripts>
    Eluna Wiki <http://wiki.emudevs.com/doku.php?id=eluna>

    -= Script Onformation =-
    * Script Type: Custom
--]]

On = {}

On.Menu = {
    ["Class"] = {
        {"Warrior",     1},
		{"PaladOn",     2},
		{"Hunter",      3},
		{"Rogue",       4},
		{"Priest",      5},
		{"DeathKnight", 6},
		{"Shaman",      7},
		{"Mage",        8},
		{"Warlock",     9},
		{"Druid",      11};
	},
	
	--[[ You can start editing below,I added class name in comments so you don't get lost,
	      Example :
	      {"Name of your choice, I use items name, it's easier to remember", ID of item},
		  {"Name of your choice, I use items name, it's easier to remember", ID of item};
		  when you're done the last line should always end by ";".
		  
	  ]]
	[1] = { -- Warrior gear S8
	        {"Wrathful Gladiator's Plate Chestpiece",  51541},
			{"Wrathful Gladiator's Plate Gauntlets",   51542},
			{"Wrathful Gladiator's Plate Helm",        51543},
			{"Wrathful Gladiator's Plate Legguards",   51544},
			{"Wrathful Gladiator's Plate Shoulders",   51545};
		},
	
	[2] = { -- Paladin gear
	        {};
		},
	
	[3] = { -- Hunter gear
	        {};
		},
	
	[4] = { -- Rogue gear
	        {};
		},
	
	[5] = { -- Priest gear
	        {};
		},
	
	[6] = { -- DeathKnight gear
	        {};
		},
	
	[7] = { -- Shaman gear
	        {};
		},
	
	[8] = { -- Mage gear
	        {};
		},
	
	[9] = { -- Warlock gear
	        {};
		},
	
	[11] = { -- Druid gear
	        {};
		},
};


function On.EquipItems(event, player)
    local i = 1
    On.Slots = {0, 1, 2, 14, 4, 3, 18, 8, 9, 5, 6, 7, 10, 11, 12, 13, 15, 16, 17, 19, 20, 21, 22}
    
    for i, v in ipairs(On.Slots) do
	    local Items = player:GetEquippedItemBySlot(i)
        if Items then
                player:RemoveItem(Items, 1)
		end
		i = i + 1
	end
	
	for k, v in ipairs(On.Menu["Class"]) do
	    player:EquipItem(On.Menu, On.Slots)
	end
end


RegisterPlayerEvent(30, On.EquipItems)
here is the whole code, I need to afk again i'll read both post when I can
 

Neth

BETA Tester
You might want to take a look at:
Script
The coding might not be the best but it has the functionalities you want.
I based my function on yours :D, i'm learning Lua for sharing my works on emudevs only, i'm going to take a look at both post now
 

DarkAngel

Wiki Author
You might want to consider not making it work clock work style and instead add the slots to the table. Then if an item already exists in that slot remove it. There are plenty of upgrades which could be done to my script, might not be the best to base it on.

Edit:
Just read Rochet's post, sorry for "double" posting.
 

Neth

BETA Tester
I don't quite understand both, I have another design in my head maybe it will be easier to understand what I want.
Code:
--[[
    EmuDevs <http://emudevs.com/forum.php>
    Eluna Lua Engine <https://github.com/ElunaLuaEngine/Eluna>
    Eluna Scripts <https://github.com/ElunaLuaEngOne/Scripts>
    Eluna Wiki <http://wiki.emudevs.com/doku.php?id=eluna>

    -= Script Information =-
    * Script Type: Custom
--]]

On = {}

local Slots = {
    ["id"] = {
            {"Head", 0},
			{"Neck", 1},
			{"Shoulders", 2},
			{"Shirt", 3},
			{"Chest", 4},
			{"Belt", 5},
			{"Legs", 6},
			{"Feet", 7},
			{"Wrists", 8},
			{"Hands", 9},
			{"Ring", 10},
			{"Ring2", 11},
			{"Trinket", 12},
			{"Trinket2", 13},
			{"Back", 14},
			{"MainHand", 15},
			{"OffHand", 16},
			{"Ranged", 17},
			{"Tabard", 18},
			{"Bag", 19},
			{"Bag2", 20},
			{"Bag3", 21},
			{"Bag4", 22},
			{"Bag5", 23};
		},
local T = {
    --[[ 
	    You can start editing below,I added class name in comments so you don't get lost,
        Example :
        {ID of the Item, Slots of the Item},
        {ID of the Item, Slots of the Item};
        when you're done the last line should always end by ";".
      ]]
	  
    ["1"] = { -- Warrior gear S8
            {51541,  "Head"};
        },
    
    ["2"] = { -- Paladin gear
            {};
        },
    
    ["3"] = { -- Hunter gear
            {};
        },
    
    ["4"] = { -- Rogue gear
            {};
        },
    
    ["5"] = { -- Priest gear
            {};
        },
    
    ["6"] = { -- DeathKnight gear
            {};
        },
    
    ["7"] = { -- Shaman gear
            {};
        },
    
    ["8"] = { -- Mage gear
            {};
        },
    
    ["9"] = { -- Warlock gear
            {};
        },
    
    ["11"] = { -- Druid gear
            {};
        },
};


function On.EquipItems(event, player)
    local i = 1
    
    for i, v in ipairs(On.Slots) do
        local Items = player:GetEquippedItemBySlot(i)
        if Items then
                player:RemoveItem(Items, 1)
        end
        i = i + 1
    end
    
    for k,v in ipairs(T[player:GetClassAsString()]) do -- k is id and v is T[class][id]
        player:EquipItem(v[1], v[2]) -- entry, slot
    end
end


RegisterPlayerEvent(30, On.EquipItems)
as you can see a new local has appeared, it's "Slots" and I would like to use my local Slots in the local T so it works like this :

--[[
You can start editing below,I added class name in comments so you don't get lost,
Example :
{ID of the Item, Slots of the Item},
{ID of the Item, Slots of the Item};
when you're done the last line should always end by ";".
]]

from what I understand I would need to parse 2 local with only one for k v in pairs, but I don't think is it possible, I want to simplify the code for future user as much as possible so if you have new idea etc please do tell
 

Rochet2

Moderator / Eluna Dev
See this:
Code:
local slots = {
    ["Head"] = 0,
    ["Neck"] = 1,
    ["Shoulders"] = 2,
    ["Shirt"] = 3,
    ["Chest"] = 4,
    ["Belt"] = 5,
    ["Legs"] = 6,
    ["Feet"] = 7,
    ["Wrists"] = 8,
    ["Hands"] = 9,
    ["Ring"] = 10,
    ["Ring2"] = 11,
    ["Trinket"] = 12,
    ["Trinket2"] = 13,
    ["Back"] = 14,
    ["MainHand"] = 15,
    ["OffHand"] = 16,
    ["Ranged"] = 17,
    ["Tabard"] = 18,
    ["Bag"] = 19,
    ["Bag2"] = 20,
    ["Bag3"] = 21,
    ["Bag4"] = 22,
}
local items = {
    --[[ 
        You can start editing below,I added class name in comments so you don't get lost,
        Example :
        ["Slot"] = entry,
        ["Slot"] = entry,
    ]]
	  
    [1] = { -- Warrior gear S8
        ["Head"] = 51541,
    },
    
    [2] = { -- Paladin gear
    },
    
    [3] = { -- Hunter gear
    },
    
    [4] = { -- Rogue gear
    },
    
    [5] = { -- Priest gear
    },
    
    [6] = { -- DeathKnight gear
    },
    
    [7] = { -- Shaman gear
    },
    
    [8] = { -- Mage gear
    },
    
    [9] = { -- Warlock gear
    },
    
    [11] = { -- Druid gear
    },
}

-- player:GetItemByPos(bag, slot)
-- bag -1 inventory and backpack:
--  slots 0-18 equipment
--  slots 19-22 bags
--  slots 23-38 backpack
-- bag 19-22 other bags:
--  slots 0-35 bag slots
local function OnFirstLogin(event, player)
    for slotname, slotid in pairs(slots) do
        local item = player:GetItemByPos(-1, slotid)
        if (item) then
            player:RemoveItem(item, item:GetCount())
        end
    end
    
    for class, gear in ipairs(items[player:GetClass()]) do
        for slotname, itementry in ipairs(gear) do
            player:EquipItem(itementry, slots[slotname]) -- entry, slot
        end
    end
end

RegisterPlayerEvent(30, OnFirstLogin)
 

DarkAngel

Wiki Author
You are overcomplicating, you could just do:
Code:
     local items = {
            [classId] =  {{entryid, slotid}, {entryid, slotid}},
            [classId] =  {{entryid, slotid}, {entryid, slotid}},
     }
Then to get the values:
Code:
    for class, classTable in pairs(items) do
        if player:GetClass() == class then
              for _, item in pairs(classTable) do
                   local ItemGet = player:GetEquippedItemBySlot(item[2])
                   if ItemGet then
                        player:RemoveItem(ItemGet, 1)
                   end
                   player:EquipItem(item[1], item[2])
              end
        end
    end

To make it user friendly you can just make a comment asigning different slotids to slot names.

Edit: Gotta say, I was creating this post before I saw Rochet post... Omg you and Foe -.-
 
Last edited:

Neth

BETA Tester
You are overcomplicating, you could just do:
Code:
     local items = {
            [classId] =  {{entryid, slotid}, {entryid, slotid}},
            [classId] =  {{entryid, slotid}, {entryid, slotid}},
     }
Then to get the values:
Code:
    for class, classTable in pairs(items) do
        if player:GetClass() == class then
              for _, item in pairs(classTable) do
                   local ItemGet = player:GetEquippedItemBySlot(item[2])
                   if ItemGet then
                        player:RemoveItem(ItemGet, 1)
                   end
                   player:EquipItem(item[1], item[2])
              end
        end
    end

To make it user friendly you can just make a comment asigning different slotids to slot names.

Edit: Gotta say, I was creating this post before I saw Rochet post... Omg you and Foe -.-

I understand Rochet2 code but not yours, how come you can add an _ in a loop?

ps: thanks to everyone
 

DarkAngel

Wiki Author
I understand Rochet2 code but not yours, how come you can add an _ in a loop?

ps: thanks to everyone

What Foe said and:
'k' & 'v' are just shorting for key and value, you could literally write anything.
I just used 'item' instead of v as the value is the table of the item.
It is mostly done to make the script eh make sense.

I think the problem is, that you do not know exactly what the for loop is doing.
It is getting all keys and values in the table 'classTable' or table of choice.
In this case, I have a key inside the table which I defined as [classId], the key is the 'identifier' for the table which is also the value.
So when I get key, value I get the key which would be classId and the value which would be the table.

Edit: Wow that sounded confusing. Here is the code I posted, commented:
Code:
-- The table which contains our classes
local items = {
	--[[
	--Key		 -----------------Value---------------- Now this value is a table
	[classId] =  {{entryid, slotid}, {entryid, slotid}},
			--Value[1]	--Value[2]	  --Value[1]	--Value[2]
	[classId] =  {	{entryid, 	  slotid}, 	 {entryid, 	  slotid}		},
	]]--
}
--For each key and it's value in pairs in the table items we do the following
    --Key  --Value            --MainTable
for class, classTable in pairs(items) do
    --If the player class is the same as the key identifier
	if player:GetClass() == class then
	          --NotUsed    --itemTbl		--the class table
		for _, 		item 		in pairs(classTable) do
			--item[2] would be the slot id, item[1] would be the items entryid.
			local ItemGet = player:GetEquippedItemBySlot(item[2])
			--If the item exists we
			if ItemGet then
				--Remove it
				player:RemoveItem(ItemGet, 1)
			end
			--We will equip the item, we do that by using the entryid and the slotid
					--entry	   --slotid
			player:EquipItem(item[1],    item[2])
		end
	end
end

Oh god, posting messes up the indention...
 
Last edited:
Status
Not open for further replies.
Top