• 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 Tfs 1.2 Cant manage to send text on "hi" message

SixNine

Active Member
Joined
Dec 12, 2018
Messages
452
Reaction score
41
Hi
so im working on this code and im confused why it doesnt send my custom text from config when it should work like a charm i think :D Ignore the part that my code looks like it was made by 9 year old and its not clean :D

So when i type 'hi' he doesnt send any message just an empty text when it should send that text from config because of npcHandler:say(currentSaga.text, cid)
Lua:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

function onCreatureAppear(cid)            npcHandler:onCreatureAppear(cid)            end
function onCreatureDisappear(cid)        npcHandler:onCreatureDisappear(cid)            end
function onCreatureSay(cid, type, msg)        npcHandler:onCreatureSay(cid, type, msg)        end
function onThink()                npcHandler:onThink()                    end

local config = { 
    teleport = {{x = 81, y = 607, z = 6}, "Beetle Juice Castle"},
}

local currentSaga = { 
    [75] = {text = "Hello, i need your help can you help me defeat beetlejuice hes on crack?", setSagaValue = 76, needLevel = 300, needPremium = true},
    [76] = {text = "Are you ready to go back?.", setSagaValue = 76, needLevel = 300, needPremium = true},
    [77] = {text = "Are you ready to go back?.", setSagaValue = 77, needLevel = 300, needPremium = true},
    [78] = {text = "Are you ready to go back?.", setSagaValue = 78, needLevel = 300, needPremium = true},
    [79] = {text = "Are you ready to go back?.", setSagaValue = 79, needLevel = 300, needPremium = true},
    [80] = {text = "Are you ready to go back?.", setSagaValue = 80, needLevel = 300, needPremium = true}
}

function creatureSayCallback(cid, type, msg)
    local player = Player(cid)

    if not npcHandler:isFocused(cid) then
        if msg == "hi" or msg == "hello" then
            npcHandler:addFocus(cid)
            npcHandler:say(currentSaga.text, cid)
            npcHandler.topic[cid] = 1
        else
            return false
        end
    elseif msgcontains(msg, "yes") and npcHandler.topic[cid] == 1 then
        local sagaValue = player:getStorageValue(Storage.Saga)
        local currentSaga = currentSaga[sagaValue]
        if not currentSaga then
            npcHandler:say("Sorry, You cant do this saga.", cid)
            return true
        end

        if currentSaga.needPremium and not player:isPremium() then
            npcHandler:say("You don't have premium.", cid)
            return true
        end    

        if currentSaga.needLevel and player:getLevel() < currentSaga.needLevel then
            npcHandler:say(string.format("You have to be stronger[%d level%s].", currentSaga.needLevel, currentSaga.needLevel > 1 and "s" or ""), cid)
            return currentSaga
        end

        player:setStorageValue(Storage.Saga, currentSaga.setSagaValue)    
        npcHandler:say("Go now!", cid)
        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, string.format("You have been teleported to %s.", config.teleport[2]))
        player:teleportTo(config.teleport[1])

        npcHandler.topic[cid] = 0
        npcHandler:releaseFocus(cid)
    elseif msgcontains(msg, "bye") then
        npcHandler:say("Bye.", cid)
        npcHandler:releaseFocus(cid)
    end
    return true
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
 
Solution
I tried something like this but it gives

attempt to index a nil value
on this line
npcHandler:say(currentSaga[sagaValue].text, cid)

Oh, cause you are using the same name for the variable.

Rename
Code:
local currentSaga = {

To: currentSagas

Then change:
Code:
local currentSaga = currentSaga[sagaValue]

To:
Code:
local currentSaga = currentSagas[sagaValue]

And:
Code:
npcHandler:say(currentSagas[sagaValue].text, cid)


BTW. what is Storage.Saga? You have defined it anywhere?
That would work if you had no indexes, like this:
Code:
local currentSaga = {text = "Hello, i need your help can you help me defeat beetlejuice hes on crack?", setSagaValue = 76, needLevel = 300, needPremium = true}

Then you can use currentSaga.text

But you have indexes, so you need to use that index to access array

Like this

Code:
currentSaga[75].text
 
That would work if you had no indexes, like this:
Code:
local currentSaga = {text = "Hello, i need your help can you help me defeat beetlejuice hes on crack?", setSagaValue = 76, needLevel = 300, needPremium = true}

Then you can use currentSaga.text

But you have indexes, so you need to use that index to access array

Like this

Code:
currentSaga[75].text
So what if storage changes to 76 or 77?
 
So what if storage changes to 76 or 77?

Then you need to move that part of code:
Code:
        local sagaValue = player:getStorageValue(Storage.Saga)
        local currentSaga = currentSaga[sagaValue]

To the top, just right after:
Code:
local player = Player(cid)

And use it like this:
Code:
currentSaga[sagaValue].text
 
Then you need to move that part of code:
Code:
        local sagaValue = player:getStorageValue(Storage.Saga)
        local currentSaga = currentSaga[sagaValue]

To the top, just right after:
Code:
local player = Player(cid)

And use it like this:
Code:
currentSaga[sagaValue].text
I tried something like this but it gives

attempt to index a nil value
on this line
npcHandler:say(currentSaga[sagaValue].text, cid)
 
I tried something like this but it gives

attempt to index a nil value
on this line
npcHandler:say(currentSaga[sagaValue].text, cid)

Oh, cause you are using the same name for the variable.

Rename
Code:
local currentSaga = {

To: currentSagas

Then change:
Code:
local currentSaga = currentSaga[sagaValue]

To:
Code:
local currentSaga = currentSagas[sagaValue]

And:
Code:
npcHandler:say(currentSagas[sagaValue].text, cid)


BTW. what is Storage.Saga? You have defined it anywhere?
 
Solution
Oh, cause you are using the same name for the variable.

Rename
Code:
local currentSaga = {

To: currentSagas

Then change:
Code:
local currentSaga = currentSaga[sagaValue]

To:
Code:
local currentSaga = currentSagas[sagaValue]

And:
Code:
npcHandler:say(currentSagas[sagaValue].text, cid)


BTW. what is Storage.Saga? You have defined it anywhere?
Yea in lib storages. Btw everything works like a charm. Thanks :)

Fuck how can i make this teleport
teleport = {{x = 81, y = 607, z = 6}, "Beetle Juice Castle"},
for only 76 storage? So it wont effect those other storages in config
 
Last edited:
Back
Top