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

Param problem, everytime printing wrong

vexler222

Active Member
Joined
Apr 22, 2012
Messages
714
Solutions
15
Reaction score
46
Hi, can someone tell me why this script printing everytime wrong? When i put good answer, still i got wrong in console.
(Im in area, script what check if player is in area working good)

Lua:
local answer = TalkAction("!as")

local function isInArena(player)
    local pos = player:getPosition()
    local rooms = {
    [1] = {as = "t", rPos  = {{x = 2850, y = 2133, z = 7},{x = 2865, y = 2146, z = 7}}},
    [2] = {as = "", rPos = {{x = 2891, y = 2134, z = 7},{x = 2904, y = 2151, z = 7}}},
}
local room_x = player:getStorageValue(as.room)

    if pos.z == rooms[room_x].rPos[1].z then
        if pos.x >= rooms[room_x].rPos[1].x and pos.y >= rooms[room_x].rPos[1].y then
            if pos.x <= rooms[room_x].rPos[2].x and pos.y <= rooms[room_x].rPos[2].y then
                if param == rooms[room_x].as then
                    print("good")
                else
                    print("wrong")
                end
                return true
            end
        end
    end
    player:sendTextMessage(MESSAGE_EVENT_DEFAULT,"Poff Poff")
    return true
end

function answer.onSay(player, words, param)

    isInArena(player)

end

answer:separator(" ")
answer:register()
 
Solution
You call function isInArena without passing param variable.
Fixed version:
Lua:
local answer = TalkAction("!as")

-- move config out of function
local rooms = {
    [1] = {as = "t", rPos  = {{x = 2850, y = 2133, z = 7},{x = 2865, y = 2146, z = 7}}},
    [2] = {as = "", rPos = {{x = 2891, y = 2134, z = 7},{x = 2904, y = 2151, z = 7}}},
}

local function isInArena(player, param) -- HERE add `param`
    local pos = player:getPosition()
    local room_x = player:getStorageValue(as.room)

    if pos.z == rooms[room_x].rPos[1].z then
        if pos.x >= rooms[room_x].rPos[1].x and pos.y >= rooms[room_x].rPos[1].y then
            if pos.x <= rooms[room_x].rPos[2].x and pos.y <= rooms[room_x].rPos[2].y then
                if param ==...
you're not passing param to isInArena...

You mean add param in isInArena function? - local function isInArena(player, param)

If u meand it, not work still ;p Btw im using function isInArena, inside onSay function, so it not passing param to isInArena function?
Post automatically merged:

Ok, nvm!
When i put local function (all code) inside onsay, it work ;p
 
You call function isInArena without passing param variable.
Fixed version:
Lua:
local answer = TalkAction("!as")

-- move config out of function
local rooms = {
    [1] = {as = "t", rPos  = {{x = 2850, y = 2133, z = 7},{x = 2865, y = 2146, z = 7}}},
    [2] = {as = "", rPos = {{x = 2891, y = 2134, z = 7},{x = 2904, y = 2151, z = 7}}},
}

local function isInArena(player, param) -- HERE add `param`
    local pos = player:getPosition()
    local room_x = player:getStorageValue(as.room)

    if pos.z == rooms[room_x].rPos[1].z then
        if pos.x >= rooms[room_x].rPos[1].x and pos.y >= rooms[room_x].rPos[1].y then
            if pos.x <= rooms[room_x].rPos[2].x and pos.y <= rooms[room_x].rPos[2].y then
                if param == rooms[room_x].as then
                    print("good")
                else
                    print("wrong")
                end
                return true
            end
        end
    end
    player:sendTextMessage(MESSAGE_EVENT_DEFAULT,"Poff Poff")
    return true
end

function answer.onSay(player, words, param)
    isInArena(player, param) -- HERE add `param`

end

answer:separator(" ")
answer:register()
 
You call function isInArena without passing param variable.
Fixed version:
Lua:
local answer = TalkAction("!as")

-- move config out of function
local rooms = {
    [1] = {as = "t", rPos  = {{x = 2850, y = 2133, z = 7},{x = 2865, y = 2146, z = 7}}},
    [2] = {as = "", rPos = {{x = 2891, y = 2134, z = 7},{x = 2904, y = 2151, z = 7}}},
}

local function isInArena(player, param) -- HERE add `param`
    local pos = player:getPosition()
    local room_x = player:getStorageValue(as.room)

    if pos.z == rooms[room_x].rPos[1].z then
        if pos.x >= rooms[room_x].rPos[1].x and pos.y >= rooms[room_x].rPos[1].y then
            if pos.x <= rooms[room_x].rPos[2].x and pos.y <= rooms[room_x].rPos[2].y then
                if param == rooms[room_x].as then
                    print("good")
                else
                    print("wrong")
                end
                return true
            end
        end
    end
    player:sendTextMessage(MESSAGE_EVENT_DEFAULT,"Poff Poff")
    return true
end

function answer.onSay(player, words, param)
    isInArena(player, param) -- HERE add `param`

end

answer:separator(" ")
answer:register()
While we're at it, use the pre-built function from core to check if is in range

Lua:
local answer = TalkAction("!as")

-- move config out of function
local rooms = {
    [1] = {as = "t", rPos  = {Position(2850, 2133, 7), Position(2865, 2146, 7)}},
    [2] = {as = "", rPos = {Position(2891, 2134, 7), Position(2904, 2151, 7)}},
}

local function isInArena(player, param) -- HERE add `param`
    local room_x = player:getStorageValue(as.room) -- assuming 'as' table is loaded globally somewhere..?  
    if not rooms[room_x] then
        print("room_x doesn't exist in table. -> " .. room_x)
        return true
    end
  
    if player:getPosition():isInRange(rooms[room_x].rPos[1], rooms[room_x].rPos[2]) -- Position:isInRange(from, to)
        if param == rooms[room_x].as then
            print("good")
        else
            print("wrong")
        end
        return true
    end
    player:sendTextMessage(MESSAGE_EVENT_DEFAULT,"Poff Poff")
    return true
end

function answer.onSay(player, words, param)
    isInArena(player, param) -- HERE add `param`
  
end

answer:separator(" ")
answer:register()
 
Solution
and also while we're at it, its good to learn how to debug. There are not many options when debugging in LUA so just print a few variables that affect the desired output.

Lua:
print(pos.z)
print(rooms[room_x].rPos[1].z)
--and so on....
print(param) --would of returned nil...

if pos.z == rooms[room_x].rPos[1].z then
    if pos.x >= rooms[room_x].rPos[1].x and pos.y >= rooms[room_x].rPos[1].y then
        if pos.x <= rooms[room_x].rPos[2].x and pos.y <= rooms[room_x].rPos[2].y then
            if param == rooms[room_x].as then
                print("good")
            else
                print("wrong")
            end
            return true
        end
    end
end
 
Last edited by a moderator:
Yup my bad. Was late when I wrote it and I had been working with 5 different languages yesterday. But the principles behind the post still stand, most people just don't even bother checking/printing variables at all.
 
Back
Top