TFS 1.X+ Problem with promotion npc

Thorn

Spriting since 2013
Premium User
Joined
Sep 24, 2012
Messages
2,188
Best answers
1
Reaction score
853
Location
Chile
Hello guys! I have a script for a npc that allows people to be promoted again.I have knights, elite knights and senior knights...and well ofc all the other vocations respective's promotions
So this npc is supose to promote you if youhave the first promotion(elite, elder, etc)
but right now, when players say the keyword "senior", he says: you already have the X(senior) promotion :/

can anyone plz help me figuring this out? i can't fully understand the 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 vocationCount = 4
local config = {
    ["first"] = { lvl = 20, cost = 1, id = 1 },
    ["senior"] = { lvl = 200, cost = 500000, id = 2 }
}

function creatureSayCallback(cid, type, msg)
    if not npcHandler:isFocused(cid) then
        return false
    end
    local player = Player(cid)
    local pname = msg:lower()
    local getPromotion = config[pname]
    if not getPromotion then
        return selfSay("The promotion you ask for does not exist.", cid)
    end
    local vocid = player:getVocation():getId()
    local count = math.floor(vocid / vocationCount)
    local canPromotion = (count - getPromotion.id)
    if canPromotion < -1 then
        return selfSay("You still can not acquire this promotion.", cid)
    elseif canPromotion == 1 then
        return selfSay("You can not degrade your promotion.", cid)
    elseif canPromotion == 0 then
        return selfSay(string.format("You already have the %s promotion.", pname), cid)
    end
    if player:getLevel() < getPromotion.lvl then
        return selfSay(string.format("You need level %u+ for the %s promotion.", getPromotion.lvl, pname), cid)
    end
    if not player:removeMoney(getPromotion.cost) then
        return selfSay("You do not have enough money.", cid)
    end
    local newVocation = Vocation(vocid + vocationCount)
    player:setVocation(newVocation)
    selfSay(string.format("You have been successfully promoted to %s.", newVocation:getName()), cid)
    player:getPosition():sendMagicEffect(CONST_ME_FIREWORK_BLUE)
    return true
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
Thanks in advance!!
 

leleco95

Member
Joined
May 6, 2009
Messages
23
Best answers
4
Reaction score
24
I assume you have the default vocations.xml ids, so knights are 4, elite knights are 8 and seniors would be 12
taking this into consideration, an elite knight would have "vocid" 8
Lua:
local count = math.floor(vocid / vocationCount)
local canPromotion = (count - getPromotion.id)
8 / 4 = 2, so when you subtract getPromotion.id (for senior, 2) it becomes 0
instead, you should do
Lua:
local count = math.floor((vocid - 1) / vocationCount)
local canPromotion = (count - getPromotion.id)
which will result in 0 for basic vocations and 1 for every first promotion, thus giving you the -1 on canPromotion
 

ZowN

Member
Joined
Mar 19, 2015
Messages
40
Best answers
1
Reaction score
20
Did you write this code? Line 27 makes no sense, your problem is in that line.
 
OP
Thorn

Thorn

Spriting since 2013
Premium User
Joined
Sep 24, 2012
Messages
2,188
Best answers
1
Reaction score
853
Location
Chile
I assume you have the default vocations.xml ids, so knights are 4, elite knights are 8 and seniors would be 12
taking this into consideration, an elite knight would have "vocid" 8
Lua:
local count = math.floor(vocid / vocationCount)
local canPromotion = (count - getPromotion.id)
8 / 4 = 2, so when you subtract getPromotion.id (for senior, 2) it becomes 0
instead, you should do
Lua:
local count = math.floor((vocid - 1) / vocationCount)
local canPromotion = (count - getPromotion.id)
which will result in 0 for basic vocations and 1 for every first promotion, thus giving you the -1 on canPromotion
Thank you man! It worked perfectly :D
 
Top