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

TFS 1.X+ Get a value by showTextDialog

aqubjukr

Well-Known Member
Joined
Mar 13, 2013
Messages
200
Solutions
17
Reaction score
79
Location
Paraná, Brazil
Does anyone know how to tell me the correct way to get the text value typed by the player inside the showTextDialog? I know that the function is structured in the following way: showTextDialog(id, text, canWrite, length), but I couldn't do it at all.

Lua:
local function processValue(cid)
    local player = Player(cid)
    local text = "Insert a value:\n"
    if player:showTextDialog(3505, text, true, 35) then
        addEvent(function() player:sendTextMessage(MESSAGE_EVENT_ADVANCE, text) end, 3000)
    end
end
 
Lua:
local function processValue(cid)
    local player = Player(cid)
    local text = "Insert a value:\n"
    local result = player:showTextDialog(3505, text, true, 35)
    if result and result ~= "" then
        addEvent(function()
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You entered: " .. result)
        end, 3000)
    end
end
 
Lua:
local creatureEvent = CreatureEvent("TextEdit")
function creatureEvent.onTextEdit(player, item, text)
    player:unregisterEvent("TextEdit")

    if #text == 0 then
        return true
    end

    if item:getId() == 1948 then
        player:sendTextMessage(MESSAGE_INFO_DESCR, text:match("\n(.+)"))
    end

    return true
end
creatureEvent:register()

local talkAction = TalkAction("!test")
function talkAction.onSay(player, words, param, type)

    player:registerEvent("TextEdit")
    player:showTextDialog(1948, "Insert a value:\n", true)

    return false
end

talkAction:register()

I wouldnt recommend it because there is no trigger when the edition is canceled. So a player may close/cancel the window and the event will not be unregistered (TextEdit) and then later, the player may find the same item ID in-game and if he writes on it, it will trigger the event

There is no proper solution unless you use an unique item ID (unobtainable in-game) or by using a physical item with some sort of action id or custom attribute.

Also consider that this code will nicely crash your server randomly (you must verify the userdata/object first)
Lua:
addEvent(function() player:sendTextMessage(MESSAGE_EVENT_ADVANCE, text) end, 3000)

Solution:
Lua:
addEvent(function(playerId)
    local player = Player(playerId)
    if not player then
        return
    end
 
    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, text)
end, 3000, player:getId())
 
Thanks @Roddet. Não tinha pensado dessa forma, usando register event.
I will also reflect and try to get a better solution to the problem you mentioned.
Btw, thx man. U've helped me a lot. <3
 
Back
Top