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

A FUNCTION AND ARRAY IN LUA

waqmaz

Member
Joined
Jun 17, 2015
Messages
203
Reaction score
11
Lets say I have a function with an array:

Code:
local function tp(from, to)

local pos = {
        ['pos_start'] = {
                {['x'] = 179, ['y'] = 367, ['z'] = 7},
                {['x'] = 666, ['y'] = 222, ['z'] = 7}
        },
        ['pos_end'] = {
                {['x'] = 1555, ['y'] = 12, ['z'] = 7},
                {['x'] = 1127, ['y'] = 124, ['z'] = 7}
        }
    }

-- lets say we havea for loop here so we can add "i"
        doSendMagicEffect(from[i], CONST_ME_POFF)
    doSendMagicEffect(to[i], CONST_ME_TELEPORT)

end

Why won't it work? I mean "from" and to

I want to call it like this way:
Code:
tp(pos['pos_start'], pos['pos_end'])
 
Make sure that the "pos" array is declared in the scope where you call tp().
Otherwise you could post the whole script.
 
Code:
function lever(leverID, from, to)

    if item.uid == leverID and item.itemid == 1945 then
           
        local count_players = #to
        local store = {}
       
        for i = 1, count_players do
            local pid = getTopCreature(from[i]).uid
            if (pid == 0 or not isPlayer(pid)) then
                    return doPlayerSendCancel(cid, 'You need ' .. count_players .. ' players to use this lever.')
            end
            store[i] = pid
        end
       
        for i = 1, count_players do
            doSendMagicEffect(from[i], CONST_ME_POFF)
            doTeleportThing(store[i], to[i], false)
            doSendMagicEffect(to[i], CONST_ME_TELEPORT)
        end
       
        doTransformItem(item.uid, item.itemid + 1)
           
    elseif item.uid == leverID and item.itemid == 1946 then
        doTransformItem(item.uid, item.itemid -1)
    end

end

function onUse(cid, item, fromPosition, itemEx, toPosition)
   
    local pos = {
        ['pos_start'] = {
                {['x'] = 1059, ['y'] = 1034, ['z'] = 7},
                {['x'] = 1060, ['y'] = 1034, ['z'] = 7}
        },
        ['pos_end'] = {
                {['x'] = 1059, ['y'] = 1032, ['z'] = 7},
                {['x'] = 1060, ['y'] = 1032, ['z'] = 7}
        }
    }
   
    lever(10150, pos['pos_start'], pos['pos_end'])
   
    return true
   
end
 
Code:
function lever(leverID, from, to)

    if item.uid == leverID and item.itemid == 1945 then
         
        local count_players = #to
        local store = {}
     
        for i = 1, count_players do
            local pid = getTopCreature(from[i]).uid
            if (pid == 0 or not isPlayer(pid)) then
                    return doPlayerSendCancel(cid, 'You need ' .. count_players .. ' players to use this lever.')
            end
            store[i] = pid
        end
     
        for i = 1, count_players do
            doSendMagicEffect(from[i], CONST_ME_POFF)
            doTeleportThing(store[i], to[i], false)
            doSendMagicEffect(to[i], CONST_ME_TELEPORT)
        end
     
        doTransformItem(item.uid, item.itemid + 1)
         
    elseif item.uid == leverID and item.itemid == 1946 then
        doTransformItem(item.uid, item.itemid -1)
    end

end

function onUse(cid, item, fromPosition, itemEx, toPosition)
 
    local pos = {
        ['pos_start'] = {
                {['x'] = 1059, ['y'] = 1034, ['z'] = 7},
                {['x'] = 1060, ['y'] = 1034, ['z'] = 7}
        },
        ['pos_end'] = {
                {['x'] = 1059, ['y'] = 1032, ['z'] = 7},
                {['x'] = 1060, ['y'] = 1032, ['z'] = 7}
        }
    }
 
    lever(10150, pos['pos_start'], pos['pos_end'])
 
    return true
 
end
Your loop is not correct and "item" is not defined in the function scope you have to pass it as a parameter.

Look:
Code:
function test()
    print(id)
end

function use()
    local id = 1234
    test()
end

use()

This will print nil.
 
no matter where the "pos" var is defined. it doesn't work even outside functions

i get error:
[28/05/2016 04:07:26] [Error - Action Interface]
[28/05/2016 04:07:26] data/actions/scripts/lever1.lua:eek:nUse
[28/05/2016 04:07:26] Description:
[28/05/2016 04:07:26] data/actions/scripts/lever1.lua:102: attempt to index global 'pos' (a nil value)
[28/05/2016 04:07:26] stack traceback:
[28/05/2016 04:07:26] data/actions/scripts/lever1.lua:102: in function <data/actions/scripts/lever1.lua:99>

or

[Error - Action Interface]
[28/05/2016 04:09:27] data/actions/scripts/lever1.lua:onUse
[28/05/2016 04:09:27] Description:
[28/05/2016 04:09:28] data/actions/scripts/lever1.lua:70: attempt to index global 'item' (a nil value)
[28/05/2016 04:09:28] stack traceback:
[28/05/2016 04:09:28] data/actions/scripts/lever1.lua:70: in function 'lever'
[28/05/2016 04:09:28] data/actions/scripts/lever1.lua:100: in function <data/actions/scripts/lever1.lua:97>
 
you get an error on line 102, most likely because that function uses pos but cant see it. and what you posted here isnt that long, how do you expect us to help you?
 
ok, guys, you cannot help me then
i do not know how to do this, i am php programmer, not lua.
in php it would work
 
you edited and added an error thats on line 70/100, which still isnt in what you posted here
to fix item error, add
Code:
local leverID = 10150
to the top, then change lever(10150 to lever(item

ofc we cant help you if you wont post the full script, you have errors on lines we dont have
 
no, men. it is full script. really. thanks for an answer. will try my best. it doesnt work. including an integer to the varaible does nothing usefull. i can just make an integer value instead of this. it isnt useful

and you got all code you need. what more do you need?
 
Last edited:
idk.. I made this global event quickly to show you how I'd normally check an area for players.
Checking this many tiles for information will most likely cause your server to freeze for a second or two though.
Code:
local top_left_corner  = {x = 900, y = 900, z = 6}
local bottom_right_corner = {x = 1100, y = 1100, z = 8}

function onThink(cid, interval, lastExecution)
     local players = 0
     local player = {}
     local txt = ""
     for t = top_left_corner.x, bottom_right_corner.x do
         for f = top_left_corner.y, bottom_right_corner.y do
             for h = top_left_corner.z, bottom_right_corner.z do
                 pos = {x = t, y = f, z = h}
                 pid = getTopCreature(pos).uid
                 if isPlayer(pid) then
                     players = players + 1
                     table.insert(player, getCreatureName(pid))
                 end
             end
         end
     end
     print("There are " .. players .. " players online in this area.")
     for i = 1, #player do
         if txt ~= "" then
             txt = txt .. ", "
         end
         txt = txt .. player[i]
     end
     print("The player names are:\n" .. txt .. ".")
     return true
end
 
Back
Top