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

Transform a script in table

president vankk

Web Developer & AuraOT Owner
Joined
Jul 10, 2009
Messages
5,719
Solutions
9
Reaction score
339
Hi folks! How I transform the script below to a table?

Code:
if item.uid == 2213 then
    doPlayerAddExp(player:getId(),250000, true)
elseif item.uid == 4015 or item.uid == 4016 or item.uid == 4017 or item.uid == 4018 then
    doPlayerAddExp(player:getId(), 150000, true)
elseif item.uid == 4030 or item.uid == 4031 or item.uid == 4032 or item.uid == 4032 or item.uid == 4035 or item.uid == 4036 or item.uid == 4037 or item.uid == 4040 or item.uid == 4041 or item.uid == 4042 then
    doPlayerAddExp(player:getId(),50000, true)
elseif item.uid == 4019 or item.uid == 4020 or item.uid == 4021 then
    doPlayerAddExp(player:getId(),350000, true)
elseif item.uid == 9169 or item.uid == 9170 then
    doPlayerAddExp(player:getId(),100000, true)
end

I give a shot on it, check the code below, but I need the "or" if don't have it, everyone can bug the experience.

Code:
local k = {
    [2213] = {experience = 250000},
    [4015] = {experience = 150000},
    [4016] = {experience = 150000},
    [4017] = {experience = 150000},
    [4018] = {experience = 150000},
    [4030] = {experience = 50000},
    [4031] = {experience = 50000},
    [4032] = {experience = 50000},
    [4032] = {experience = 50000},
    [4035] = {experience = 50000},
    [4036] = {experience = 50000},
    [4037] = {experience = 50000},
    [4040] = {experience = 50000},
    [4041] = {experience = 50000},
    [4042] = {experience = 50000},
    [4019] = {experience = 50000},
    [4020] = {experience = 50000},
    [4021] = {experience = 50000},
    [9169] = {experience = 50000},
    [9170] = {experience = 50000}
}

local t = k[item.uniqueid]
if t then
    doPlayerAddExp(player:getId(), t.experience, true)
end

Thanks for the support.
 
Last edited:
Typing this from my phone..
Hope it works.
Code:
local t = {
    [1] = {uid = 1111, exp = 1000},
    [2] = {uid = 1111, exp = 1000}
}
for i = 1, #t do
    if t[i].uid == item.uid then
        doPlayerAddExp(player:getId(), t[i].exp, true)
        break
    end
end
I don't know how your 'or' system works.. but you could check for a storage value or something.. to see if the experience has been given before.
Code:
local t = {
    [1] = {uid = 1111, exp = 1000, storage = 1111},
    [2] = {uid = 1111, exp = 1000, storage = 1111}
}
for i = 1, #t do
    if t[i].uid == item.uid then
        if -- storage < 1 then
            doPlayerAddExp(player:getId(), t[i].exp, true)
            -- Set storage to 1
            break
        end
    end
end
 
Code:
local items = {
    [{2213}] = {experience = 250000},
    [{4015, 4016, 4017, 4018}] = {experience = 150000},
    [{4019, 4020, 4021}] = {experience = 350000},
    [{4030, 4031, 4032, 4035, 4036, 4037}] = {experience = 50000},
    [{4040, 4041, 4042}] = {experience = 50000},
    [{9169, 9170}] = {experience = 100000}
 
}

function isInRangeArray(t, v)
    if type(t) == 'table' then
        for k, x in pairs(t) do
            if type(k) == 'table' then
                for n, g in pairs(k) do
                    if g == v then
                        return x
                    end
                end
            end
        end
    end
    return false
end

local reward = isInRangeArray(items, item.uid)
if reward then
    doPlayerAddExp(player:getId(), reward.experience, true)
end
 
Code:
local config = {
    [1] = {uids = {123, 234, 567}, exp = 10},
    [2] = {uids = {890}, exp = 10},
}

for i = 1, #config do
    if isInArray(config[i].uids, item.uid) then
        doPlayerAddExp(player:getId(), config[i].exp, true)
        break
    end
end
 
Last edited:
Code:
local config, cont = {
    [1] = {uids = {123, 234, 567}, exp = 10},
    [2] = {uids = {890}, exp = 10},
}, 0

for i = 1, #config do
    for j = 1, #config[i].uids do
        if item.uid == config[i].uids[j] then
            doPlayerAddExp(player:getId(), config[i].exp, true)
            cont = 1
            break
        end
    end
    if cont == 1 then break end
end
You are already breaking the loop, your 'cont' and break further in the loop will never be executed. You can just remove' cont' altogether as it's not serving any purpose. :p
 
You are already breaking the loop, your 'cont' and break further in the loop will never be executed. You can just remove' cont' altogether as it's not serving any purpose. :p

the variable count helps me finish the process faster, it is a way to save time in processing. any programmer knows that.

the "break" only breaks a cycle at a time.
 
the variable count helps me finish the process faster, it is a way to save time in processing. any programmer knows that.
Well if you want to end a loop turn your iterators into a function and just use a return statement like i did :)
 
Code:
local items = {
    [{2213}] = {experience = 250000},
    [{4015, 4016, 4017, 4018}] = {experience = 150000},
    [{4019, 4020, 4021}] = {experience = 350000},
    [{4030, 4031, 4032, 4035, 4036, 4037}] = {experience = 50000},
    [{4040, 4041, 4042}] = {experience = 50000},
    [{9169, 9170}] = {experience = 100000}

}

function isInRangeArray(t, v)
    if type(t) == 'table' then
        for k, x in pairs(t) do
            if type(k) == 'table' then
                for n, g in pairs(k) do
                    if g == v then
                        return x
                    end
                end
            end
        end
    end
    return false
end

local reward = isInRangeArray(items, item.uid)
if reward then
    doPlayerAddExp(player:getId(), reward.experience, true)
end

Code:
if isInArray(k, v) then
    return x
end

Code:
local config, cont = {
    [1] = {uids = {123, 234, 567}, exp = 10},
    [2] = {uids = {890}, exp = 10},
}, 0

for i = 1, #config do
    for j = 1, #config[i].uids do
        if item.uid == config[i].uids[j] then
            doPlayerAddExp(player:getId(), config[i].exp, true)
            cont = 1
            break
        end
    end
    if cont == 1 then break end
end

Code:
if isInArray(config[i].uids, item.uid) then
    -- do shit
    break
end
 
Code:
if isInArray(k, v) then
    return x
end



Code:
if isInArray(config[i].uids, item.uid) then
    -- do shit
    break
end
You know isInArray is a loop right?
He asked for a table, I simplified his code, no need for all those if else, or or's

Edit:
It is also important for people who want to learn to program how some of these functions work and how they can write their own.
 
You know isInArray is a loop right?
He asked for a table, I simplified his code, no need for all those if else, or or's

Edit:
It is also important for people who want to learn to program how some of these functions work and how they can write their own.
isInArray is coded in c++, faster than lua for sure :p
 
isInArray is coded in c++, faster than lua for sure :p
Actually it isn't, even tho it is defined in c++, it still needs to construct a metatable for the table passed to it, seems like extra work to me..

Code:
int LuaScriptInterface::luaIsInArray(lua_State* L)
{
    //isInArray(array, value)
    if (!isTable(L, 1)) {
        pushBoolean(L, false);
        return 1;
    }

    lua_pushnil(L);
    while (lua_next(L, 1)) {
        if (lua_equal(L, 2, -1) != 0) {
            pushBoolean(L, true);
            return 1;
        }
        lua_pop(L, 1);
    }

    pushBoolean(L, false);
    return 1;
}
 
Actually it isn't, even tho it is defined in c++, it still needs to construct a metatable for the table passed to it, seems like extra work to me..

Code:
int LuaScriptInterface::luaIsInArray(lua_State* L)
{
    //isInArray(array, value)
    if (!isTable(L, 1)) {
        pushBoolean(L, false);
        return 1;
    }

    lua_pushnil(L);
    while (lua_next(L, 1)) {
        if (lua_equal(L, 2, -1) != 0) {
            pushBoolean(L, true);
            return 1;
        }
        lua_pop(L, 1);
    }

    pushBoolean(L, false);
    return 1;
}
http://www.lua.org/source/5.3/lbaselib.c.html

Code:
static int luaB_next (lua_State *L) {
  luaL_checktype(L, 1, LUA_TTABLE);
  lua_settop(L, 2);  /* create a 2nd argument if there isn't one */
  if (lua_next(L, 1))
    return 2;
  else {
    lua_pushnil(L);
    return 1;
  }
}

static int luaB_pairs (lua_State *L) {
  return pairsmeta(L, "__pairs", 0, luaB_next);
}

The pairs works just like that my friend. The only difference is that you're using lua and its not faster than C++. With pairs it has to send the value of lua_next to lua so you can use it.
 
http://www.lua.org/source/5.3/lbaselib.c.html

Code:
static int luaB_next (lua_State *L) {
  luaL_checktype(L, 1, LUA_TTABLE);
  lua_settop(L, 2);  /* create a 2nd argument if there isn't one */
  if (lua_next(L, 1))
    return 2;
  else {
    lua_pushnil(L);
    return 1;
  }
}

static int luaB_pairs (lua_State *L) {
  return pairsmeta(L, "__pairs", 0, luaB_next);
}

The pairs works just like that my friend. The only difference is that you're using lua and its not faster than C++.
See you don't even know, you had to google that shit, which tells me you aren't trying to help people, you are just being an ass, every post I make in response to an issue you think you know a better way.. well pal you don't.

There is only 1 way to deal with people like you :)
Ba bye :)
 
See you don't even know, you had to google that shit, which tells me you aren't trying to help people, you are just being an ass, every post I make in response to an issue you think you know a better way.. well pal you don't.

There is only 1 way to deal with people like you :)
Ba bye :)
Because there is a better way? If you don't think its better just tell us why, if your arguments are correct I wouldn't say anything.

Ofcourse i had to google wtf? do you think I know how every lua function works in C? No, I don't. But I knew that pairs uses next and if you have to pass the value of something from C to lua, of course it is going to be faster if you don't have to.

I "googled" the code so i could present an argument, thats how science is done.
 
@Codex NG
Your script is not working, does not give any experience and also there is no error on console.
That is because you are not using it right, i test everything in a lua interpreter before I post it on the forums :)

Instead of saying it doesn't work.. learn to test things, it isn't rocket science to use print to check for values in the console...

If you can't even be bothered to do that much how to do expect to ever learn anything meaningful?
 
Back
Top