• 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 Table issues

gabriel28

Member
Joined
Mar 16, 2012
Messages
199
Solutions
6
Reaction score
24
I'm trying to learn how to make tables now with a simple talkaction that change the outfit, but I need to know how to get values from the index.

My table is:

local table = {
[1, 5] = {out = 121, e = 2},
[2, 6] = {out = 122, e = 2},
[3, 7] = {out = 123, e = 2},
[4, 8] = {out = 124, e = 2}
}

I'm try to get the vocation info on the index with: table[getPlayerVocation(cid)], but I see that it only work with only one information inside the index.
My question is: How can I make a index with more than one value and how can I get the informations inside?

Thanks in advance.
 
Solution
Lua:
local level = 25
local cfg = {
    [{1, 5}] = {    --just for test
        [1] = 121,
        [2] = 125,
        [3] = 129,
        effect = 2
    },
    [{2, 6}] = {
        [1] = 122,
        [2] = 126,
        [3] = 130,
        effect = 2
    },
    [{3, 7}] = {
        [1] = 123,
        [2] = 127,
        [3] = 131,
        effect = 2
    },
    [{4, 9}] = {
        [1] = 124,
        [2] = 128,
        [3] = 132,
        effect = 2
    }
}

function onSay(cid, words, param, channel)
    if(param == '') then
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Say the number of outfit that you want.")
        return 1
    end

    param = tonumber(param)
       if not param or (math.floor(param) ~= param) then...
An array index cannot have more than one value.
Technically if the index is an array, it can have infinite values : p

Lua:
local table = {
    [{1, 5}] = {out = 121, e = 2},
    [{2, 6}] = {out = 122, e = 2},
    [{3, 7}] = {out = 123, e = 2},
    [{4, 8}] = {out = 124, e = 2}
}

for k, v in pairs(table) do 
    if isInArray(k, getPlayerVocation(cid)) then
        -- do something etc
        break
    end
end

Then iterate the table with for .. pairs

Or else copy the values to a new index as you suggest.
 
Thanks both.
And thanks @cbrm this is what I wanted.

Now, if you can teach me how to do, I tried it in a more complex table, changing another one script.
The original one:
Lua:
local level = 25
local cfg = {
[1] = {[1] = 71, [2] = 490, [3] = 496, [4] = 491, [5] = 499, [6] = 671, [7] = 575, effect = 2},
[10] = {[1] = 522, [2] = 528, [3] = 529, [4] = 530, [5] = 534, [6] = 532, [7] = 533, effect = 2},
[20] = {[1] = 237, [2] = 245, [3] = 244, [4] = 271, [5] = 274, [6] = 275, [7] = 276, effect = 2},
[30] = {[1] = 650, [2] = 651, [3] = 652, [4] = 653, [5] = 654, [6] = 655, [7] = 656, [8] = 657, [9] = 658, [10] = 659, effect = 2},
[40] = {[1] = 627, [2] = 628, [3] = 404, [4] = 632, [5] = 631, [6] = 633, [7] = 630, effect = 2}
}


function onSay(cid, words, param, channel)
if(param == '') then
    doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Digite o número da saga que você deseja.")
return 1
end

    if not cfg[getPlayerVocation(cid)] then    --what I need to change
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Você não pode mudar de saga.")
    return 1
    end

        local x = string.explode(param, ",")
        if(x[2]) then
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Não estou entendendo..")
        return 1
        end

            if not (tonumber(x[1])) then
                doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Digite o número da saga que você deseja.")
            return 1
            end

        if tonumber(x[1]) > #cfg[getPlayerVocation(cid)] or tonumber(x[1]) < 1  then --what I need to change
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Essa saga não existe.")
        return true
        end

    local outfit = {lookType = cfg[getPlayerVocation(cid)][tonumber(x[1])]}       --what I need to change
    if getPlayerLevel(cid) >= (tonumber(x[1])*level) then
        doCreatureChangeOutfit(cid, outfit)
        doSendMagicEffect(getThingPos(cid), cfg[getPlayerVocation(cid)].effect)  --what I need to change
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Você mudou de Saga!!")
        doPlayerSay(cid, "New Saga!!", TALKTYPE_ORANGE_1)
    else
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Desculpe, você precisa de level "..(tonumber(x[1]) * level).." para essa saga.")
    end
return 1
end

My try:
Lua:
local level = 25
local cfg = {
[1, 5] = {[1] = 121, [2] = 125, [3] = 129, effect = 2}, --just for test
[2, 6] = {[1] = 122, [2] = 126, [3] = 130, effect = 2},
[3, 7] = {[1] = 123, [2] = 127, [3] = 131, effect = 2},
[4, 9] = {[1] = 124, [2] = 128, [3] = 132, effect = 2}
}


function onSay(cid, words, param, channel)
if(param == '') then
    doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Say the number of outfit you want.")
return 1
end
    for k, v in pairs(cfg) do
    if not isInArray(k, getPlayerVocation(cid)) then --change
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You cant change your outfit")
    return 1
    end

        local x = string.explode(param, ",")
        if(x[2]) then
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "I dont understand.")
        return 1
        end

            if not (tonumber(x[1])) then
                doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Say the number of outfit you want.")
            return 1
            end

        if tonumber(x[1]) > #(k, getPlayerVocation(cid)) or tonumber(x[1]) < 1  then --change
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "This outfit dont exist.")
        return true
        end

    local outfit = {lookType = isInArray(k, getPlayerVocation(cid))[tonumber(x[1])]}        --change
    if getPlayerLevel(cid) >= (tonumber(x[1])*level) then
        doCreatureChangeOutfit(cid, outfit)
        doSendMagicEffect(getThingPos(cid), isInArray(k, getPlayerVocation(cid)).effect)   --change
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You changed your outfit!")
        doPlayerSay(cid, "New Saga!!", TALKTYPE_ORANGE_1)
    else
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Sorry, you need level "..(tonumber(x[1]) * level).." to this outfit.")
    end
    end
return 1
end

But I get this error: ....lua:72: ')' expected near ','. I tried other things, but without sucess.
If you can teach me what I made wrong, you will help me alot.
Edit: Just to you know what this script do, when you say !outfit 1, the outfit is changed to what index [1] is, !outfit 2, etc.
 
Lua:
local level = 25
local cfg = {
    [{1, 5}] = {    --just for test
        [1] = 121,
        [2] = 125,
        [3] = 129,
        effect = 2
    },
    [{2, 6}] = {
        [1] = 122,
        [2] = 126,
        [3] = 130,
        effect = 2
    },
    [{3, 7}] = {
        [1] = 123,
        [2] = 127,
        [3] = 131,
        effect = 2
    },
    [{4, 9}] = {
        [1] = 124,
        [2] = 128,
        [3] = 132,
        effect = 2
    }
}

function onSay(cid, words, param, channel)
    if(param == '') then
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Say the number of outfit that you want.")
        return 1
    end

    param = tonumber(param)
       if not param or (math.floor(param) ~= param) then
           doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Say the number of the outfit that you want.")
           return 1
    end

    local arr
    for k, v in pairs(cfg) do
        if isInArray(k, getPlayerVocation(cid)) then
            arr = v
            break
        end
    end

    if not arr then
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You can't change your outfit.")
        return 1
    end

    if not arr[param] then --change
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "This outfit doesn't exist.")
        return true
    end

    local lv = (param * level)
    local outfit = getCreatureOutfit(cid)
    outfit.lookType = arr[param]
    if getPlayerLevel(cid) >= lv then
        doCreatureChangeOutfit(cid, outfit)
        doSendMagicEffect(getThingPos(cid), arr.effect)   --change
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You changed your outfit!")
        doPlayerSay(cid, "New Saga!!", TALKTYPE_ORANGE_1)
    else
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Sorry, you need to be at least level ".. lv .." to use this outfit.")
    end
    return 1
end

But I get this error: ....lua:72: ')' expected near ','. I tried other things, but without sucess.
If you can teach me what I made wrong, you will help me alot.
a few tidbits
  1. You didn't add the LUA array/table punctuation marks (braces { }) to the indexes so you get that error.
  2. I find pointless to explode the string if only one parameter will be used.
  3. When you repeat a same value among the script it is more organized to define such value as a constant variable.
  4. When you want to change the outfit it is wiser to get the entire outfit table and then just change the lookType. Or else the outfit colors get their default value, plain white.
  5. When the script uses a player-defined number it is wise to validate that it is a whole number and not decimal to avoid errors. Yeah, on this case the line 54 would avoid that problem anyway.
 
Solution
@cbrm
Thanks alot.
But, if not ask too much, can you tell me why yours worked and mine not? I'm starting on Lua, as I said before, so a "mini" lesson would help me a lot in understanding even more and would make me happy. haha Of course, if you have time to it.
 
@cbrm
Thanks alot.
But, if not ask too much, can you tell me why yours worked and mine not? I'm starting on Lua, as I said before, so a "mini" lesson would help me a lot in understanding even more and would make me happy. haha Of course, if you have time to it.
F5
 
Back
Top