• 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 SOLVED [TFS 1.4] attempt to call method 'titleCase' (a nil value)

CorpseDude

Member
Joined
Jan 22, 2021
Messages
20
Solutions
1
Reaction score
5
I am working on converting over a RL map data pack to work with 1.4 for 10.98. I have searched but didnt find any releases. This is an error that a lot of my NPCS are getting. I know barely anything about coding but I can use context clues to normally make it work. The following error though I have no clue on because I want to keep the proper questing functions to allow for different destinations.
Code:
Lua Script Error: [Npc interface]
data/npc/scripts/Brodrosch.lua
data/npc/scripts/Brodrosch.lua:48: attempt to call method 'titleCase' (a nil value)
stack traceback:
        [C]: in function 'titleCase'
        data/npc/scripts/Brodrosch.lua:48: in function 'addTravelKeyword'
        data/npc/scripts/Brodrosch.lua:53: in main chunk
[Warning - NpcScript::NpcScript] Can not load script: Brodrosch.lua

Below is the affected part of the script

Lua:
local function addTravelKeyword(keyword, cost, discount, destination, action)
    local travelKeyword = keywordHandler:addKeyword({keyword}, StdModule.say, {npcHandler = npcHandler, text = 'Do you seek a ride to ' .. keyword:titleCase() .. ' for |TRAVELCOST|?', cost = cost, discount = discount})
        travelKeyword:addChildKeyword({'yes'}, StdModule.travel, {npcHandler = npcHandler, premium = true, text = 'Full steam ahead!', cost = cost, discount = discount, destination = destination}, nil, action)
        travelKeyword:addChildKeyword({'no'}, StdModule.say, {npcHandler = npcHandler, text = 'We would like to serve you some time.', reset = true})
end

addTravelKeyword('farmine', 210, {'postman', 'new frontier'},
    function(player)
        local destination = Position(33025, 31553, 14)
        --if player:getStorageValue(Storage.TheNewFrontier.Mission05) == 7 then --if The New Frontier Quest 'Mission 05: Getting Things Busy' complete then Stage 3
            if player:getStorageValue(Storage.TheNewFrontier.Mission05) <= 700 then
            destination.z = 10
          
            --elseif player:getStorageValue(Storage.TheNewFrontier.Mission03) <= 300 then
        --elseif player:getStorageValue(Storage.TheNewFrontier.Mission03) == 3 then --if The New Frontier Quest 'Mission 03: Strangers in the Night' complete then Stage 2
            --destination.z = 10
        end

        return destination
    end
)
addTravelKeyword('cormaya', 160, 'postman', Position(33311, 31989, 15),
    function(player)
        if player:getStorageValue(Storage.postman.Mission01) == 4 then
            player:setStorageValue(Storage.postman.Mission01, 5)
        end
    end
)

Below is full script
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 voices = { {text = 'Passage to Cormaya! Unforgettable steamboat ride!'} }
npcHandler:addModule(VoiceModule:new(voices))

local function creatureSayCallback(cid, type, msg)

    if not npcHandler:isFocused(cid) then
        return false
    end

    if msgcontains(msg, 'ticket') then
        if Player(cid):getStorageValue(Storage.wagonTicket) >= os.time() then
            npcHandler:say('Your weekly ticket is still valid. Would be a waste of money to purchase a second one', cid)
            return true
        end

        npcHandler:say('Do you want to purchase a weekly ticket for the ore wagons? With it you can travel freely and swiftly through Kazordoon for one week. 250 gold only. Deal?', cid)
        npcHandler.topic[cid] = 1
    elseif msgcontains(msg, 'yes') and npcHandler.topic[cid] > 0 then
        local player = Player(cid)
        if npcHandler.topic[cid] == 1 then
            if not player:removeMoney(250) then
                npcHandler:say('You don\'t have enough money.', cid)
                npcHandler.topic[cid] = 0
                return true
            end

            player:setStorageValue(Storage.wagonTicket, os.time() + 7 * 24 * 60 * 60)
            npcHandler:say('Here is your stamp. It can\'t be transferred to another person and will last one week from now. You\'ll get notified upon using an ore wagon when it isn\'t valid anymore.', cid)
        end
        npcHandler.topic[cid] = 0
    elseif msgcontains(msg, 'no') and npcHandler.topic[cid] > 0 then
        npcHandler:say('No then.', cid)
        npcHandler.topic[cid] = 0
    end
    return true
end

local function addTravelKeyword(keyword, cost, discount, destination, action)
    local travelKeyword = keywordHandler:addKeyword({keyword}, StdModule.say, {npcHandler = npcHandler, text = 'Do you seek a ride to ' .. keyword:titleCase() .. ' for |TRAVELCOST|?', cost = cost, discount = discount})
        travelKeyword:addChildKeyword({'yes'}, StdModule.travel, {npcHandler = npcHandler, premium = true, text = 'Full steam ahead!', cost = cost, discount = discount, destination = destination}, nil, action)
        travelKeyword:addChildKeyword({'no'}, StdModule.say, {npcHandler = npcHandler, text = 'We would like to serve you some time.', reset = true})
end

addTravelKeyword('farmine', 210, {'postman', 'new frontier'},
    function(player)
        local destination = Position(33025, 31553, 14)
        --if player:getStorageValue(Storage.TheNewFrontier.Mission05) == 7 then --if The New Frontier Quest 'Mission 05: Getting Things Busy' complete then Stage 3
            if player:getStorageValue(Storage.TheNewFrontier.Mission05) <= 700 then
            destination.z = 10
          
            --elseif player:getStorageValue(Storage.TheNewFrontier.Mission03) <= 300 then
        --elseif player:getStorageValue(Storage.TheNewFrontier.Mission03) == 3 then --if The New Frontier Quest 'Mission 03: Strangers in the Night' complete then Stage 2
            --destination.z = 10
        end

        return destination
    end
)
addTravelKeyword('cormaya', 160, 'postman', Position(33311, 31989, 15),
    function(player)
        if player:getStorageValue(Storage.postman.Mission01) == 4 then
            player:setStorageValue(Storage.postman.Mission01, 5)
        end
    end
)

keywordHandler:addKeyword({'passage'}, StdModule.say, {npcHandler = npcHandler, text = 'Do you want me take you to {Cormaya} or {Farmine}?'})

npcHandler:setMessage(MESSAGE_GREET, 'Welcome, |PLAYERNAME|! May earth protect you on the rocky grounds. If you need a {passage}, I can help you.')
npcHandler:setMessage(MESSAGE_FAREWELL, 'Good bye.')
npcHandler:setMessage(MESSAGE_WALKAWAY, 'Good bye then.')

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
Solution
SOLVED
On lined 24-30 of data/npc/lib/keywordhandler.lua I added the following.

Lua:
function titleCase(str)
    local result = string.gsub(str, '(%a)([%w_\']*)', function(first, rest)
        return first:upper() .. rest:lower()
    end)

    return result
end
The error message says that on line 48 you are calling the method (something:method() <--- this is a method, google it for mor info) titleCase(), but it is returning a nil value. This is either because titleCase() does not exist, or because the method does not return anything in this case. Try finding the method titleCase() wherever it is defined. I have not seen it being used in TFS1.4. I suppose it should capitalize the string, to make the first letter capitalized and the rest lower case. You could probably just remove the function and use nothing but keyword, to make sure that nothing else is broken. Then you can make your own function, either locally or globablly, like something in this thread on stack overflow. If you make the function local, then just add that function in your code, maybe right before the local function addTravelKeyword(), i.e. on line 46, and remember to write "local function titleCase()"
 
The error message says that on line 48 you are calling the method (something:method() <--- this is a method, google it for mor info) titleCase(), but it is returning a nil value. This is either because titleCase() does not exist, or because the method does not return anything in this case. Try finding the method titleCase() wherever it is defined. I have not seen it being used in TFS1.4. I suppose it should capitalize the string, to make the first letter capitalized and the rest lower case. You could probably just remove the function and use nothing but keyword, to make sure that nothing else is broken. Then you can make your own function, either locally or globablly, like something in this thread on stack overflow. If you make the function local, then just add that function in your code, maybe right before the local function addTravelKeyword(), i.e. on line 46, and remember to write "local function titleCase()"
First off thank you for taking the time to respond!

Im kind of understanding but not 100%
Your saying its calling a function but but returning nothing?
I was able to track down where title case was defined. Its in string.lua

Here is the Function?
Lua:
string.titleCase = function(str)
    return str:gsub("(%a)([%w_']*)", function(first, rest) return first:upper() .. rest:lower() end)
end


I would assume I need to load something into the beginning of the script to call upon string.lua?
Lua:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local string = titleCase:new()

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

When I add that to line 4 it gives me this error code.
Code:
data/npc/scripts/Brodrosch.lua:4: attempt to index global 'titleCase' (a nil value)
stack traceback:
So maybe I need to move title case from string.lua to the global.lua?
 
SOLVED
On lined 24-30 of data/npc/lib/keywordhandler.lua I added the following.

Lua:
function titleCase(str)
    local result = string.gsub(str, '(%a)([%w_\']*)', function(first, rest)
        return first:upper() .. rest:lower()
    end)

    return result
end
 
Last edited:
Solution
Back
Top