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

change if\else if to local table

Darekzio

New Member
Joined
Apr 26, 2024
Messages
12
Reaction score
0
can someone help to change this code to work with local table instead of elseif

LUA:
function onCreatureSay(cid, type, msg)
      msg = string.lower(msg)

      if msgcontains(msg, 'hi') and (not isFocused(cid)) and getDistanceToCreature(cid) < 4 then
        selfSay('Hello ' .. creatureGetName(cid) .. '! I buy swords, clubs, axes, wands, rods, helmets, boots, legs, shields and armors.', cid)
        addFocus(cid)

    elseif isFandD(cid, 4) and msgcontains(msg, 'test') then
          sell(cid,2446,1,600000)
    elseif isFandD(cid, 4) and msgcontains(msg, 'test2') then
          sell(cid,2447,1,100)
    else
        selfSay('you don\'t have enought! ')
    end
end
 
Solution
X
LUA:
local sellables = {
    ["test"] = {itemid = 2446, price = 600000},
    ["test2"] = {itemid = 2447, price = 100},
}

function onCreatureSay(cid, type, msg)
    msg = string.lower(msg)
    if msgcontains(msg, 'hi') and (not isFocused(cid)) and getDistanceToCreature(cid) < 4 then
        selfSay('Hello ' .. creatureGetName(cid) .. '! I buy swords, clubs, axes, wands, rods, helmets, boots, legs, shields and armors.', cid)
        addFocus(cid)
    
    elseif isFandD(cid, 4) and sellables[msg] then
        sell(cid, sellables[msg].itemid, 1, sellables[msg].price)
    else
        selfSay('you don\'t have enought! ')
    end
end
LUA:
local sellables = {
    ["test"] = {itemid = 2446, price = 600000},
    ["test2"] = {itemid = 2447, price = 100},
}

function onCreatureSay(cid, type, msg)
    msg = string.lower(msg)
    if msgcontains(msg, 'hi') and (not isFocused(cid)) and getDistanceToCreature(cid) < 4 then
        selfSay('Hello ' .. creatureGetName(cid) .. '! I buy swords, clubs, axes, wands, rods, helmets, boots, legs, shields and armors.', cid)
        addFocus(cid)
    
    elseif isFandD(cid, 4) and sellables[msg] then
        sell(cid, sellables[msg].itemid, 1, sellables[msg].price)
    else
        selfSay('you don\'t have enought! ')
    end
end
 
Solution
Back
Top