I've been working on some lua to add and remove a newly created trait and have encountered some strange behavior. The objective is that a character with a specified skill (a skill adding in the mod in question "extra_govern_basic") who moves into a provincial capital would receive the new trait and if they leave the captial they would lose the trait. I first implemented the "manager_skill" lua section and everything seemed to work properly (by which I mean testing in game a character without the skill had no impact, with the skill if they move into a capital they got the trait, leave the capital and they lost the trait. Yay, all is good. Then I moved onto the second phase, the idea being that if the character had a second skill (the referenced "extra_govern_tax") then they would get a second point towards the trait if they entered a capital. So the idea would be if you had one skill you get one point, if you had both skills (in this case the second skill requires the first so no way you'd only have one of them) then you'd get two points (which was setup to mean two levels) of the trait.

However, after adding the new levels and second step of the trait I discovered a strange doubling behavior. To be specific a lord without the skills still behaved as expected (no impact), but if a lord had one skill and entered a capital he would get two points in the trait and a lord with both skills would get four points in the trait (eventual plan was to have the trait have four levels so they were added in already even though at the moment there was not intended to be any way of actually earning four trait points). Has anyone encountered a similar problem and/or understand why the lua below would produce two trait points instead of one upon entering an appropriate garrison? Interestingly when a lord enters a garrison he receives two or four separate pop-ups with the "Trait Gained" message corresponding to having one or two of the skills respectively.

In the pack being tested the lua below is appended to the end of the script/export_triggers__core__.lua file. All of the standard vanilla lua exists above it (not sure at the moment I want to mess around with trying to get the lua imported properly by creating new lua. The corresponding character skills and traits referenced here appear correctly and aside from this trait all of their effects appear as expected, the trait also appears as expected and has the expected effects.

Thanks
Code:
--[[ manager_skill ]]--

function manager_skill_impl (context)
        return context:character():has_skill("extra_govern_basic") and context:character():region():is_province_capital()
end 




events.CharacterEntersGarrison[#events.CharacterEntersGarrison+1] =
function (context)
    if manager_skill_impl(context) then
        effect.trait("extra_trait_administrator", "agent", 1, 100, context)
        return true
    end
    return false
end


events.CharacterLeavesGarrison[#events.CharacterLeavesGarrison+1] =
function (context)
    if manager_skill_impl(context) then
        cm:force_remove_trait("character_cqi:"..context:character():command_queue_index(), "extra_trait_administrator")
        return true
    end
    return false
end




--[[ governor_skill ]]--
function governor_skill_impl (context)
        return context:character():has_skill("extra_govern_tax") and context:character():region():is_province_capital()
end 


events.CharacterEntersGarrison[#events.CharacterEntersGarrison+1] =
function (context)
    if governor_skill_impl(context) then
        effect.trait("extra_trait_administrator", "agent", 1, 100, context)
        return true
    end
    return false
end


events.CharacterLeavesGarrison[#events.CharacterLeavesGarrison+1] =
function (context)
    if governor_skill_impl(context) then
        cm:force_remove_trait("character_cqi:"..context:character():command_queue_index(), "extra_trait_administrator")
        return true
    end
    return false
end