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

[Lua] How to make simple tables

Dantarrix

Member
Joined
Aug 27, 2010
Messages
546
Reaction score
18
Location
Stgo, Chile
Well, i want to know how to make a table....
Because i need a script like this:
When u use a certain item, u summon a monster depending on vocation and level.....
So i think is better to use a table.... Like this:
local sorcerer = {
[x] = "y"
}
Where x is level and y name of the monster.... But i dont know how to use it on the script....

Hope i explained well.....
 
LUA:
local array = 
    {
        [1] =        -- voc id
            {
                [1] = 'Rat',        -- level
                [2] = 'Demon'
            },
        [2] = 
            {
                [1] = 'Hydra',
                [2] = 'Cave rat'
            }
    }
    
function onUse(cid, ...)
    local voc = getPlayerVocation(cid)        -- dont remember if correct
    local arr = array[voc]
    
    if not arr then 
        return false 
    end
    
    local sum = arr[getPlayerLevel(cid)]
    
    if not sum then
        return false
    end
    
    dosummon(sum)    -- check correct function and params
    return true
end

maybe you will need index the level array by ranges
 
LUA:
local level = --local table containing information
{
-table position is a table too
--------------------value of table is a string
--------this is 'k'----this is 'v'-----------
	[{1, 50}] = "Minotaur Guard",
	[{51, 100}] = "Dragon Lord",
	[{101, 120}] = "Behemoth",
	[{121, 999}] = "Demon"
}

function onUse(cid, words, param, channel)

	for k, v in ipairs(level) do
					---- k = {1, 50} (example) and v = "Minotaur Guard"----
					---- so--[1], [2]----
					---then k[1] = 1 and k[2] = 50
		if getPlayerLevel(cid) >= k[1] and getPlayerLevel(cid) <= k[2] then --this is the same as (if getPlayerLevel(cid) >= 1 and getPlayerLevel(cid) <= 50 then
			doSummonCreature(v, getCreaturePosition(cid)) ---and then this summon v = "Minotaur Guard"
			break --and break the loop (so it won't check the next table ([{51, 100}]....), if this doesn't happen (player level isn't >= 1 and <= 50) then it jumps to the next table value, si it will start again (k = [{51, 100}]... v = "Dragon Lord".. k[1] = 51 and bla bla bla..)
		end
	end
	return true
end
 
But i need it to check vocations too.... :/
Maybe including something like this???
LUA:
local table = {
[1] = {
[{1,50}] = "Minotaur Guard",
etc...
}
[2] = {
[{1, 50}] = "Demon",
etc...
}
etc...
}
local voc = table[getPlayerVocation(cid)]
function onUse (cid, item, fromPos, itemEx, toPos)

for k, v in ipairs(voc) do
if getPlayerLevel(cid) >= k[1] and getPlayerLevel(cid) <= k [2] then
local creature = doSummonCreature(v, getCreaturePosition(cid))
break
doConvinceCreature(cid, creature) ---dont forget it...
doPlayerSay(cid, "Go "..getCreatureName(creature).."!!!, 1)
end
end
return true
end
 
Last edited:
LUA:
local t = {}

t[1] = {
	[{1, 50}] = 'Minotaur Guard',
	[{51, 100}] = 'Dragon Lord',
	[{101, 120}] = 'Behemoth',
	[{121, math.huge}] = 'Demon'
}
t[2] = {
	[{1, 50}] = 'Demon Skeleton',
	[{51, 100}] = 'Giant Spider',
	[{101, 120}] = 'Warlock',
	[{121, math.huge}] = 'Juggernaut'
}

function onUse (cid, item, fromPos, itemEx, toPos)
	local l, voc = getPlayerLevel(cid), getPlayerVocation(cid)
	while voc > 4 do
		voc = voc - 4
	end
	for k, v in pairs(t[voc]) do
		if l >= k[1] and l <= k [2] then
			doSummonMonster(cid, v)
			doCreatureSay(cid, 'Go, ' .. v .. '!!!', TALKTYPE_ORANGE_1)
			break
		end
	end
	return true
end
ipairs is wrong.
 
Back
Top