• 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+ blessing script error - C++ exception

Marko999x

999x era
Premium User
Joined
Dec 14, 2017
Messages
2,815
Solutions
82
Reaction score
1,938
Location
Germany
Lua Script Error: [TalkAction Interface]
data/talkactions/scripts/bless.lua:eek:nSay
C++ exception

Lua:
function onSay(cid)
    local player = Player(cid)
    local totalBlessPrice = 100000

   for i = 1, 5 do
       if player:hasBlessing(i) then
           player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You are alredy blessed!")
       end
    end
    if player:removeMoney(totalBlessPrice) then
        player:addBlessing(i, 1)     
        player:say('You have been blessed', TALKTYPE_MONSTER_SAY)
    else
        if player:withdrawMoney(totalBlessPrice) then
            if player:removeMoney(totalBlessPrice) then
                player:addBlessing(i, 1)     
                player:say('You have been blessed', TALKTYPE_MONSTER_SAY)
            else
                player:sendCancelMessage("You don't have enough money. You need " .. totalBlessPrice .. " to buy bless.", cid)
            end
        else
            player:sendCancelMessage("You don't have enough money. Not even in your bank account! You need " .. totalBlessPrice .. " to buy bless.", cid)
        end
    end
    return false
end
 
It seems that the error is in the main function.
Change
Lua:
function onSay(cid)
to this:
Lua:
function onSay(player, words, param)

No need to do a player check, because only players can execute commands
 
Last edited:
Have not tested:

Lua:
function onSay(cid, words, param)
   local player = Player(cid)
 
    local n = 0
    for i = 1, 5 do
        if not player:hasBlessing(i) then
            n = n + 1
        end
    end
    
    if n == 0 then
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You are alredy blessed!")
        return false
    end
    
    local success = false
    local price = n * 10000
    
    if player:removeMoney(price) then
        success = true
    else
        if player:withdrawMoney(price) then
            if player:removeMoney(price) then
                success = true
            end
                player:sendCancelMessage("You don't have enough money. You need " .. price .. " gold to buy ".. n .." blessings.", cid)
            end   
        else
            player:sendCancelMessage("You don't have enough money. Not even in your bank account! You need " .. price .. " gold to buy " .. n .. " blessings.", cid)
        end
    end
    
    if success then
        for i = 1, n do
            player:addBlessing(i, 1) 
        end
        player:say('You have recieved ' .. n .. ' blessings.', TALKTYPE_MONSTER_SAY)
    end
    
    return false
end
 
It seems that the error is in the main function.
Change
Lua:
function onSay(cid)
to this:
Lua:
function onSay(player, words, param)

No need to do a player check, because only players can execute commands

Its not my script but im confused why it wouldnt work
change the main function did not work
 
Its not my script but im confused why it wouldnt work
change the main function did not work
In this part, it is necessary to return (either false or true), to end the execution of the script, because it is verifying if the player has a blessing, and if the player has a blessing, it is not necessary for the script to continue.

Screenshot from 2021-10-06 16-16-39.png

It is not necessary to do this verification twice, because if the first one was not fulfilled, the second will not be fulfilled. Also, someone will have money in the bank, will lose it, and will not grant blessings.

Screenshot from 2021-10-06 16-20-02.png

I would do this:
Lua:
function onSay(player, words, param)
    local totalBlessPrice = 100000
    for i = 1, 5 do
       if player:hasBlessing(i) then
           player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You are alredy blessed!")
           return false
       end
    end

    if player:removeMoney(totalBlessPrice) or player:withdrawMoney(totalBlessPrice) then
        for i = 1, 5 do
            player:addBlessing(i)
        end
        player:say('You have been blessed', TALKTYPE_MONSTER_SAY)
        player:getPosition():sendMagicEffect(CONST_ME_MAGIC_BLUE)
        return false
    end

    player:sendCancelMessage("You don't have enough money. You need " .. totalBlessPrice .. " gps or in your bank account! to buy bless.", cid)
    return false
end

if either of the two is true, bless is granted
Screenshot from 2021-10-06 16-32-49.png
 
Hm, then you'll have to put some breakpoints and debug it properly :C

Keep in mind it's a C++ exception, so you should rather look into your C++ code, not Lua
@edit
My bad, the issue is actually in your Lua
This function should take one argument, while you are passing two, for example here.
Lua:
if player:removeMoney(totalBlessPrice) then
        player:addBlessing(i, 1) 
        player:say('You have been blessed', TALKTYPE_MONSTER_SAY)
    else
it should be
Lua:
if player:removeMoney(totalBlessPrice) then
        player:addBlessing(i) 
        player:say('You have been blessed', TALKTYPE_MONSTER_SAY)
    else

Below you can find the whole script corrected
Lua:
function onSay(cid)
    local player = Player(cid)
    local totalBlessPrice = 100000

   for i = 1, 5 do
       if player:hasBlessing(i) then
           player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You are alredy blessed!")
       end
    end
    if player:removeMoney(totalBlessPrice) then
        player:addBlessing(i)    
        player:say('You have been blessed', TALKTYPE_MONSTER_SAY)
    else
        if player:withdrawMoney(totalBlessPrice) then
            if player:removeMoney(totalBlessPrice) then
                player:addBlessing(i)    
                player:say('You have been blessed', TALKTYPE_MONSTER_SAY)
            else
                player:sendCancelMessage("You don't have enough money. You need " .. totalBlessPrice .. " to buy bless.", cid)
            end
        else
            player:sendCancelMessage("You don't have enough money. Not even in your bank account! You need " .. totalBlessPrice .. " to buy bless.", cid)
        end
    end
    return false
end
 
Last edited:
Back
Top