• 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+ creaturescript onadvance exp issue

Tbol

Well-Known Member
Joined
Apr 7, 2019
Messages
592
Reaction score
64
Cant get it working i though i fixed it but noticed it does give particular reward after reaching certain level point, strange part wrote two code version and both works on 50% becase i have two rewards and in one code it gives one reward but it doesnt give another reward, than in another code version its opposite :D

So in this code this part addExtraHealth, addExtraMana doesnt work for some reason it doesnt add up,but addToAvailablePoints, addToTotalPoints works just fine

LUA:
local config = {
    vocations = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 101, 86, 81, 77, 75, 73, 71, 111, 108, 113, 115},
    restoreHealth = true,

    stages = {
        [15] = {
            addToAvailablePoints = 1,
            addToTotalPoints = 1
        },
        [30] = {
            addExtraHealth = 250,
            addExtraMana = 250
        }
    }
}

function onAdvance(player, skill, oldLevel, newLevel)
    if skill ~= SKILL_LEVEL or newLevel <= oldLevel then
        return true
    end

    for level = oldLevel + 1, newLevel do
        local reward = config.stages[level]
        if reward and isInArray(config.vocations, player:getVocation():getId()) and player:getStorageValue(Storage.LevelReward) < level then
            if reward.addExtraHealth then
                player:addHealth(reward.addExtraHealth)
            end
            if reward.addExtraMana then
                player:addMana(reward.addExtraMana)
            end

            if reward.addToAvailablePoints then
                Rewardpoint.addToAvailablePoints(player, reward.addToAvailablePoints)
                player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, string.format("Hello! Youve gained %u points.", reward.addToAvailablePoints))
            end
            if reward.addToTotalPoints then
                Rewardpoint.addToTotalPoints(player, reward.addToTotalPoints)
            end

            player:setStorageValue(Storage.LevelReward, level)
        end
    end

    if config.restoreHealth then
        player:addHealth(player:getMaxHealth())
        player:addMana(player:getMaxMana())
    end

    return true
end

Now in this code addExtraHealth, addExtraMana works just fine, but it has a issue if i use action script that gives for example 5000000exp it ignore the reward and doesnt give for example addToAvailablePoints, addToTotalPoints, but in first code it works fine, so im like stuck in a curse if i fix something another shit breaks
LUA:
local config = {
    vocations = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 101, 86, 81, 77, 75, 73, 71, 111, 108, 113, 115},
    restoreHealth = true,
    stages = {
        [15] = {
            addToAvailablePoints = 1,
            addToTotalPoints = 1
        },
        [30] = {
            addExtraHealth = 250,
            addExtraMana = 250
        }
    }
}

function onAdvance(player, skill, oldLevel, newLevel)
    if skill ~= SKILL_LEVEL or newLevel <= oldLevel then
        return true
    end

    local reward = config.stages[newLevel]
    if reward and isInArray(config.vocations, player:getVocation():getId()) and player:getStorageValue(Storage.LevelReward) < newLevel then
        player:takeReward(reward)
        player:setStorageValue(Storage.LevelReward, newLevel)
        player:save()

        -- Add technique points if defined in the reward configuration
        if reward.addToAvailablePoints then
            Techniques.addToAvailablePoints(player, reward.addToAvailablePoints)
            player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, string.format("Hello! You've gained %u Points.", reward.addToAvailablePoints))
        end
        if reward.addToTotalPoints then
            Techniques.addToTotalPoints(player, reward.addToTotalPoints)
        end
    end

    -- Restore health and mana if configured
    if config.restoreHealth then
        player:addHealth(player:getMaxHealth())
        player:addMana(player:getMaxMana())
    end

    return true
end
 
Cant get it working i though i fixed it but noticed it does give particular reward after reaching certain level point, strange part wrote two code version and both works on 50% becase i have two rewards and in one code it gives one reward but it doesnt give another reward, than in another code version its opposite :D

So in this code this part addExtraHealth, addExtraMana doesnt work for some reason it doesnt add up,but addToAvailablePoints, addToTotalPoints works just fine

LUA:
local config = {
    vocations = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 101, 86, 81, 77, 75, 73, 71, 111, 108, 113, 115},
    restoreHealth = true,

    stages = {
        [15] = {
            addToAvailablePoints = 1,
            addToTotalPoints = 1
        },
        [30] = {
            addExtraHealth = 250,
            addExtraMana = 250
        }
    }
}

function onAdvance(player, skill, oldLevel, newLevel)
    if skill ~= SKILL_LEVEL or newLevel <= oldLevel then
        return true
    end

    for level = oldLevel + 1, newLevel do
        local reward = config.stages[level]
        if reward and isInArray(config.vocations, player:getVocation():getId()) and player:getStorageValue(Storage.LevelReward) < level then
            if reward.addExtraHealth then
                player:addHealth(reward.addExtraHealth)
            end
            if reward.addExtraMana then
                player:addMana(reward.addExtraMana)
            end

            if reward.addToAvailablePoints then
                Rewardpoint.addToAvailablePoints(player, reward.addToAvailablePoints)
                player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, string.format("Hello! Youve gained %u points.", reward.addToAvailablePoints))
            end
            if reward.addToTotalPoints then
                Rewardpoint.addToTotalPoints(player, reward.addToTotalPoints)
            end

            player:setStorageValue(Storage.LevelReward, level)
        end
    end

    if config.restoreHealth then
        player:addHealth(player:getMaxHealth())
        player:addMana(player:getMaxMana())
    end

    return true
end

Now in this code addExtraHealth, addExtraMana works just fine, but it has a issue if i use action script that gives for example 5000000exp it ignore the reward and doesnt give for example addToAvailablePoints, addToTotalPoints, but in first code it works fine, so im like stuck in a curse if i fix something another shit breaks
LUA:
local config = {
    vocations = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 101, 86, 81, 77, 75, 73, 71, 111, 108, 113, 115},
    restoreHealth = true,
    stages = {
        [15] = {
            addToAvailablePoints = 1,
            addToTotalPoints = 1
        },
        [30] = {
            addExtraHealth = 250,
            addExtraMana = 250
        }
    }
}

function onAdvance(player, skill, oldLevel, newLevel)
    if skill ~= SKILL_LEVEL or newLevel <= oldLevel then
        return true
    end

    local reward = config.stages[newLevel]
    if reward and isInArray(config.vocations, player:getVocation():getId()) and player:getStorageValue(Storage.LevelReward) < newLevel then
        player:takeReward(reward)
        player:setStorageValue(Storage.LevelReward, newLevel)
        player:save()

        -- Add technique points if defined in the reward configuration
        if reward.addToAvailablePoints then
            Techniques.addToAvailablePoints(player, reward.addToAvailablePoints)
            player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, string.format("Hello! You've gained %u Points.", reward.addToAvailablePoints))
        end
        if reward.addToTotalPoints then
            Techniques.addToTotalPoints(player, reward.addToTotalPoints)
        end
    end

    -- Restore health and mana if configured
    if config.restoreHealth then
        player:addHealth(player:getMaxHealth())
        player:addMana(player:getMaxMana())
    end

    return true
end
Because when you gain a lot of exp oldLevel is for eg. 200 and newLevel is 205.
And youre just getting config for a new level ONLY
Code:
local reward = config.stages[newLevel]

But you should do it for every new level

Do a for loop

Code:
 -- Loop through each level gained to ensure no rewards are skipped
 for level = oldLevel + 1, newLevel do
    local reward = config.stages[level]
   
    if reward and isInArray(config.vocations, player:getVocation():getId()) and player:getStorageValue(Storage.LevelReward) < level then
        player:takeReward(reward)
        player:setStorageValue(Storage.LevelReward, level)
        player:save()

        -- Add technique points if defined in the reward configuration
        if reward.addToAvailablePoints then
            Techniques.addToAvailablePoints(player, reward.addToAvailablePoints)
            player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, string.format("Hello! You've gained %u Points.", reward.addToAvailablePoints))
        end
        if reward.addToTotalPoints then
            Techniques.addToTotalPoints(player, reward.addToTotalPoints)
        end
       
        -- Extra health and mana rewards if configured
        if reward.addExtraHealth then
            player:addHealth(reward.addExtraHealth)
        end
        if reward.addExtraMana then
            player:addMana(reward.addExtraMana)
        end
    end
end

Should work with this change, let me know.
 
Because when you gain a lot of exp oldLevel is for eg. 200 and newLevel is 205.
And youre just getting config for a new level ONLY
Code:
local reward = config.stages[newLevel]

But you should do it for every new level

Do a for loop

Code:
 -- Loop through each level gained to ensure no rewards are skipped
 for level = oldLevel + 1, newLevel do
    local reward = config.stages[level]
  
    if reward and isInArray(config.vocations, player:getVocation():getId()) and player:getStorageValue(Storage.LevelReward) < level then
        player:takeReward(reward)
        player:setStorageValue(Storage.LevelReward, level)
        player:save()

        -- Add technique points if defined in the reward configuration
        if reward.addToAvailablePoints then
            Techniques.addToAvailablePoints(player, reward.addToAvailablePoints)
            player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, string.format("Hello! You've gained %u Points.", reward.addToAvailablePoints))
        end
        if reward.addToTotalPoints then
            Techniques.addToTotalPoints(player, reward.addToTotalPoints)
        end
      
        -- Extra health and mana rewards if configured
        if reward.addExtraHealth then
            player:addHealth(reward.addExtraHealth)
        end
        if reward.addExtraMana then
            player:addMana(reward.addExtraMana)
        end
    end
end

Should work with this change, let me know.
Wow worked first try, damn took me few days and didnt figured anything out
 
Wow worked first try, damn took me few days and didnt figured anything out
Glad it worked. You’ll learn and get better ;). Always think then code, and good approach is to write comments inside code editor that contain steps that needs to be done to achieve the goal. So you dont forget loops :D. For Loops OP
 
Back
Top