• 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 Player target Item, detect which one and give reward or experience

alejandro762

Well-Known Member
Joined
Sep 6, 2021
Messages
225
Reaction score
63
Hello

I would like to know in lua, how can we tell in an 'if' if a player uses (right click) an object (without consuming it) such as, for example, right click open a table > modal windows, so that it detects what is the table it opens and depending on the table, it executes a function.

Let me explain,
I have 1 blue table, and a red table, I would like that if the player clicks on the blue table, after create an object ( craft ) he has for example 100 coins, and if he clicks on the red table he has 200 coins.
I saw an isInArray function (also many others, if target.getId(), etc ).


I don't really know how we could write that, and of course if it exists.
 
Example withs itemIds:
Lua:
local blueTableId = 0000
local redTableId = 0000

local action = Action()

function action.onUse(player, item, fromPos, target, toPos, isHotkey)
    local itemId = item:getId()
    if itemId == blueTableId then -- BLUE TABLE
        local modalWindow = ModalWindow{
            title = "Test",
            message = "Test",
        }

        modalWindow:addButton("Accept", function (player, button, choice)
            player:say("Hi, this is a blue table.")
        end)
        modalWindow:addButton("Decline")

        modalWindow:sendToPlayer(player)
    elseif itemId == redTableId then -- RED TABLE
        local modalWindow = ModalWindow{
            title = "Test",
            message = "Test",
        }

        modalWindow:addButton("Accept", function (player, button, choice)
            player:say("Hi, this is a red table.")
        end)
        modalWindow:addButton("Decline")

        modalWindow:sendToPlayer(player)
    end
    return true
end

action:id(blueTableId, redTableId)
action:register()

Example with actionId:
Lua:
local actionId = 00000
local selectedTable = {}

local action = Action()

function action.onUse(player, item, fromPos, target, toPos, isHotkey)
    selectedTable[player:getId()] = {
        itemId = item:getId(),
        pos = item:getPosition()
    }

    local modalWindow = ModalWindow{
        title = "Test",
        message = "Test",
    }

    modalWindow:addButton("Accept", function (player, button, choice)
        local selected = selectedTable[player:getId()]
        if selected then
            local tble = Tile(selected.pos):getItemById(selected.itemId)
            if tble then
                player:say(string.format("Hi, this is a %s table.", tble:getName()))
            end
        end
    end)

    modalWindow:addButton("Decline")

    modalWindow:sendToPlayer(player)
    return true
end

action:aid(actionId)
action:register()

Another example:
Lua:
local actionId = 00000
local tables = {
    [1212] = "Red Table",
    [1224] = "Blue Table"
}

local action = Action()

function action.onUse(player, item, fromPos, target, toPos, isHotkey)
    local selected = tables[item:getId()]
    if not selected then
        player:sendCancelMessage(string.format("Not found table with itemId: %d", item:getId()))
        return true
    end

    local modalWindow = ModalWindow{
        title = "Test",
        message = "Test",
    }

    modalWindow:addButton("Accept", function (player, button, choice)
        player:say(string.format("Hi, this is a %s table.", selected))
    end)

    modalWindow:addButton("Decline")

    modalWindow:sendToPlayer(player)
    return true
end

action:aid(actionId)
action:register()

Idk if I understood what you're saying, I think this was what you asked, right?
How to know what was the item you clicked inside a modal window?

I forgot to mention that I made these examples with THIS <
 
Last edited:
Example withs itemIds:
Lua:
local blueTableId = 0000
local redTableId = 0000

local action = Action()

function action.onUse(player, item, fromPos, target, toPos, isHotkey)
    local itemId = item:getId()
    if itemId == blueTableId then -- BLUE TABLE
        local modalWindow = ModalWindow{
            title = "Test",
            message = "Test",
        }

        modalWindow:addButton("Accept", function (player, button, choice)
            player:say("Hi, this is a blue table.")
        end)
        modalWindow:addButton("Decline")

        modalWindow:sendToPlayer(player)
    elseif itemId == redTableId then -- RED TABLE
        local modalWindow = ModalWindow{
            title = "Test",
            message = "Test",
        }

        modalWindow:addButton("Accept", function (player, button, choice)
            player:say("Hi, this is a red table.")
        end)
        modalWindow:addButton("Decline")

        modalWindow:sendToPlayer(player)
    end
    return true
end

action:id(blueTableId, redTableId)
action:register()

Example with actionId:
Lua:
local actionId = 00000
local selectedTable = {}

local action = Action()

function action.onUse(player, item, fromPos, target, toPos, isHotkey)
    selectedTable[player:getId()] = {
        itemId = item:getId(),
        pos = item:getPosition()
    }

    local modalWindow = ModalWindow{
        title = "Test",
        message = "Test",
    }

    modalWindow:addButton("Accept", function (player, button, choice)
        local selected = selectedTable[player:getId()]
        if selected then
            local tble = Tile(selected.pos):getItemById(selected.itemId)
            if tble then
                player:say(string.format("Hi, this is a %s table.", tble:getName()))
            end
        end
    end)

    modalWindow:addButton("Decline")

    modalWindow:sendToPlayer(player)
    return true
end

action:aid(actionId)
action:register()

Another example:
Lua:
local actionId = 00000
local tables = {
    [1212] = "Red Table",
    [1224] = "Blue Table"
}

local action = Action()

function action.onUse(player, item, fromPos, target, toPos, isHotkey)
    local selected = tables[item:getId()]
    if not selected then
        player:sendCancelMessage(string.format("Not found table with itemId: %d", item:getId()))
        return true
    end

    local modalWindow = ModalWindow{
        title = "Test",
        message = "Test",
    }

    modalWindow:addButton("Accept", function (player, button, choice)
        player:say(string.format("Hi, this is a %s table.", selected))
    end)

    modalWindow:addButton("Decline")

    modalWindow:sendToPlayer(player)
    return true
end

action:aid(actionId)
action:register()

Idk if I understood what you're saying, I think this was what you asked, right?
How to know what was the item you clicked inside a modal window?
Yeah, well, i got a modal Windows, with a dofile, there i have added levels ans experience right.

So i got many tables and i wish to get a level for each target table.itemId, since i cannot add an action on It, but i wish to know on if function what add to "detect" which table is using (clicking) a playera, then i will add exp/item for this table, then i will repeat code, for each different craft table i get.

If needed i share the dofile, craft system is from here, tfs 1.1, 1 dofile, 1 script where are added each recipe(action, onUse player:sendModalWindows.
 
Back
Top