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

Multiple items in table

Ratser

Retired // Making time for yoloswagmod
Joined
Feb 11, 2009
Messages
482
Reaction score
36
How can I use more than 1 itemid in a table?

I tried with this...and failed:
Lua:
function onUse(cid, item, fromPosition, itemEx, toPosition)
	local ITEMS =
	{
		[{8977, 9002}] = {"PUDDI PUDDI PUDDI PUDDI"}
	}

	local items = ITEMS[{item.itemid}]
	if(not items) then
		return false
	end
	
	doCreatureSay(cid, items[2], TALKTYPE_MONSTER, false, 0, fromPosition.x ~= CONTAINER_POSITION and fromPosition or getThingPos(cid))

	return true
end
 
Uhm, what about if you use a for?
for k, v in pairs
And use k[1] and k[2] for the items?
 
Ok, I tried with this:

Lua:
function onUse(cid, item, fromPosition, itemEx, toPosition)
	local ITEMS =
	{
		[{8977, 9002}] = {"DESU DESU", "PUDDI PUDDI"},
		[{8981, 9004}] = {"MUD", "KIP"}
	}
	
	for k, v in pairs(ITEMS) do
		doCreatureSay(cid, v[2], TALKTYPE_MONSTER, false, 0, fromPosition.x ~= CONTAINER_POSITION and fromPosition or getThingPos(cid))
	end

	return true
end

But 'v[2]' is making all of the items above say the second sound of each item in the table. (e.g. When I use itemid = 8977 it says "PUDDI PUDDI" and "KIP" at the same time)
 
Last edited:
But you are not checking the k, example:
Lua:
function onUse(cid, item, fromPosition, itemEx, toPosition)
	local ITEMS =
	{
		[{8977, 9002}] = {"DESU DESU", "PUDDI PUDDI"},
		[{8981, 9004}] = {"MUD", "KIP"}
	}
 
	for k, v in pairs(ITEMS) do
            if item.itemid == k[1] then
		doCreatureSay(cid, v[2], TALKTYPE_MONSTER, false, 0, fromPosition.x ~= CONTAINER_POSITION and fromPosition or getThingPos(cid))
	end
 
	return true
end
 
Ok, I got it.

BTW, in this one...
Lua:
function onUse(cid, item, fromPosition, itemEx, toPosition)
	local DIRECT =
	{
		[{8977, 9002}] = {"DESU DESU", "PUDDI PUDDI"},
		[{8981, 9004}] = {"MUD", "KIP"}
	}
	
	for k, v in pairs(DIRECT) do
		if item.itemid == k[1] or item.itemid == k[2] then
			doCreatureSay(cid, v[2], TALKTYPE_MONSTER, false, 0, fromPosition.x ~= CONTAINER_POSITION and fromPosition or getThingPos(cid))
		end
	end

	return true
end
...is there any way to shorten this:
Lua:
if item.itemid == k[1] or item.itemid == k[2] then

EDIT:
Doesn't matter anymore...I made it with
Lua:
if(isInArray(k, item.itemid)) then

kthxbai
 
Uhm I think that using isInArray shud work

Lua:
if isInArray({k[1],k[2]},item.itemid) then

But remember, that if you put at doCreatureSay only v[2]
Then if use either first or second item, it will say PUDDI PUDDI and it'll never say DESU DESU :p
 
Ok, I got it.

BTW, in this one...
Lua:
function onUse(cid, item, fromPosition, itemEx, toPosition)
	local DIRECT =
	{
		[{8977, 9002}] = {"DESU DESU", "PUDDI PUDDI"},
		[{8981, 9004}] = {"MUD", "KIP"}
	}
	
	for k, v in pairs(DIRECT) do
		if item.itemid == k[1] or item.itemid == k[2] then
			doCreatureSay(cid, v[2], TALKTYPE_MONSTER, false, 0, fromPosition.x ~= CONTAINER_POSITION and fromPosition or getThingPos(cid))
		end
	end

	return true
end
...is there any way to shorten this:
Lua:
if item.itemid == k[1] or item.itemid == k[2] then

EDIT:
Doesn't matter anymore...I made it with
Lua:
if(isInArray(k, item.itemid)) then

kthxbai

Lua:
n = max
for i = k[1], k[n] do
for k, v in pairs(DIRECT) do
		if isInArray({k[i]},item.itemid) then
doCreatureSay(cid, v[i], TALKTYPE_MONSTER, false, 0, fromPosition.x ~= CONTAINER_POSITION and fromPosition or getThingPos(cid))
try som like that, sorry, i got no time today.. :X
 
it looks like you're trying to have 4 items give 4 different messages, so why use a table w/ 2 items as a key? why not just a normal 1 key and 1 value table?
 
2 messages for each pair of items.

e.g.
If the itemid is 8977 or 9002 then the message will be "DESU DESU" or "PUDDI PUDDI".
If the itemid is 8981 or 9004 then the message will be "MUD" or "KIP".
 
Last edited:
PHP:
items = {[8977] = {"DESU DESU", "PUDDI PUDDI"}, [9002] = items[8977],
[8981] = {"MUD", "KIP"}, [9004] = items[8981]}

for i=1,#items do
print(items[i][math.random(1,#items[i])])
end
Not tested ;)
 
Back
Top