Lua Addexperience in lever

Akirama

Premium User
Premium User
Joined
Aug 9, 2020
Messages
26
Reaction score
1
Hi,
I need very simple script ( simple for you not for me xD ) .
When player use lever , lever add to player experience (1945 example id lever). I don't need add storage, player can use always lever not only once :D .

TFS 1.3 , 8.6
 
Solution
Code:
Lua Script Error: [Test Interface]
data/actions/scripts/exp.lua
luaCreateAction(). Actions can only be registered in the Scripts interface.
stack traceback:
        [C]: in function 'Action'
        data/actions/scripts/exp.lua:1: in main chunk

Lua Script Error: [Test Interface]
data/actions/scripts/exp.lua
data/actions/scripts/exp.lua:3: attempt to index local 'expAction' (a nil value)
stack traceback:
        [C]: in function '__newindex'
        data/actions/scripts/exp.lua:3: in main chunk
[Warning - Event::checkScript] Can not load script: scripts/exp.lua
The new scripting style known as RevScript goes into data/scripts

That being said it's not advisable to set an entire itemid, such a lever, which is used in...

Deatthraz

Advanced OT User
Joined
May 1, 2013
Messages
439
Solutions
26
Reaction score
196
Location
Mexico
Lua:
local expAction = Action()

function expAction.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if item.aid == 1500 then
        local experience = 1000
        player:addExperience(experience, true)
    end
end

expAction:id(1945,1946)
expAction:register()
 
OP
A

Akirama

Premium User
Premium User
Joined
Aug 9, 2020
Messages
26
Reaction score
1
Code:
Lua Script Error: [Test Interface]
data/actions/scripts/exp.lua
luaCreateAction(). Actions can only be registered in the Scripts interface.
stack traceback:
        [C]: in function 'Action'
        data/actions/scripts/exp.lua:1: in main chunk

Lua Script Error: [Test Interface]
data/actions/scripts/exp.lua
data/actions/scripts/exp.lua:3: attempt to index local 'expAction' (a nil value)
stack traceback:
        [C]: in function '__newindex'
        data/actions/scripts/exp.lua:3: in main chunk
[Warning - Event::checkScript] Can not load script: scripts/exp.lua
 

Xikini

I whore myself out for likes
Support Team
Joined
Nov 17, 2010
Messages
5,403
Solutions
361
Reaction score
3,513
Code:
Lua Script Error: [Test Interface]
data/actions/scripts/exp.lua
luaCreateAction(). Actions can only be registered in the Scripts interface.
stack traceback:
        [C]: in function 'Action'
        data/actions/scripts/exp.lua:1: in main chunk

Lua Script Error: [Test Interface]
data/actions/scripts/exp.lua
data/actions/scripts/exp.lua:3: attempt to index local 'expAction' (a nil value)
stack traceback:
        [C]: in function '__newindex'
        data/actions/scripts/exp.lua:3: in main chunk
[Warning - Event::checkScript] Can not load script: scripts/exp.lua
The new scripting style known as RevScript goes into data/scripts

That being said it's not advisable to set an entire itemid, such a lever, which is used in a broad set of scripts, to a single script.

Instead, you should use an actionid.

so change script location from data/actions/scripts to data/scripts

and use this script instead. (and set ActionId on lever in remere's to 45001 - or whatever you change it to)
Lua:
local experience = 1000

local expAction = Action()

function expAction.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    player:addExperience(experience, true) -- true means to send player information about the experience gained
    item:transform(item:getId() == 1945 and 1946 or 1945) -- flops the lever
    return true
end

expAction:aid(45001)
expAction:register()
 
Solution
OP
A

Akirama

Premium User
Premium User
Joined
Aug 9, 2020
Messages
26
Reaction score
1
That's work :D.. but i have error in console when I used lever:
Lua:
Lua Script Error: [Event Interface]
data/events/scripts/player.lua:[email protected]
data/events/scripts/player.lua:206: attempt to index local 'source' (a nil value                                            )
stack traceback:
        [C]: in function '__index'
        data/events/scripts/player.lua:206: in function <data/events/scripts/pla                                            yer.lua:205>
        [C]: in function 'addExperience'
        /home/ots/data/scripts/actions/exp.lua:5: in function </home/ots/data/sc                                            ripts/actions/exp.lua:3>

Experience ofc I gained.
 

Xikini

I whore myself out for likes
Support Team
Joined
Nov 17, 2010
Messages
5,403
Solutions
361
Reaction score
3,513
That's work :D.. but i have error in console when I used lever:
Lua:
Lua Script Error: [Event Interface]
data/events/scripts/player.lua:[email protected]
data/events/scripts/player.lua:206: attempt to index local 'source' (a nil value                                            )
stack traceback:
        [C]: in function '__index'
        data/events/scripts/player.lua:206: in function <data/events/scripts/pla                                            yer.lua:205>
        [C]: in function 'addExperience'
        /home/ots/data/scripts/actions/exp.lua:5: in function </home/ots/data/sc                                            ripts/actions/exp.lua:3>

Experience ofc I gained.
I guess you modified that file in the past?

By default it should look like this...
Lua:
function Player:onGainExperience(source, exp, rawExp)
    if not source or source:isPlayer() then
        return exp
    end
    
    -- Soul regeneration
    local vocation = self:getVocation()
    if self:getSoul() < vocation:getMaxSoul() and exp >= self:getLevel() then
        soulCondition:setParameter(CONDITION_PARAM_SOULTICKS, vocation:getSoulGainTicks() * 1000)
        self:addCondition(soulCondition)
    end
    
    -- Apply experience stage multiplier
    exp = exp * Game.getExperienceStage(self:getLevel())
    
    -- Stamina modifier
    if configManager.getBoolean(configKeys.STAMINA_SYSTEM) then
        useStamina(self)
    
        local staminaMinutes = self:getStamina()
        if staminaMinutes > 2400 and self:isPremium() then
            exp = exp * 1.5
        elseif staminaMinutes <= 840 then
            exp = exp * 0.5
        end
    end
    
    return hasEventCallback(EVENT_CALLBACK_ONGAINEXPERIENCE) and EventCallback(EVENT_CALLBACK_ONGAINEXPERIENCE, self, source, exp, rawExp) or exp
end
 
OP
A

Akirama

Premium User
Premium User
Joined
Aug 9, 2020
Messages
26
Reaction score
1
I have something like this:
Lua:
function Player:onGainExperience(source, exp, rawExp)
if source:isMonster() then
        local bonusExperience = source:getMonsterLevel() * 5
        if source:getMonsterLevel() > 0 and bonusExperience > 1 then
            exp = exp * bonusExperience
        end
    end

    -- Soul regeneration
    local vocation = self:getVocation()
    if self:getSoul() < vocation:getMaxSoul() and exp >= self:getLevel() then
        soulCondition:setParameter(CONDITION_PARAM_SOULTICKS, vocation:getSoulGainTicks() * 1000)
        self:addCondition(soulCondition)
    end

    -- Apply experience stage multiplier
    exp = exp * Game.getExperienceStage(self:getLevel())

    -- Stamina modifier
    if configManager.getBoolean(configKeys.STAMINA_SYSTEM) then
        useStamina(self)

        local staminaMinutes = self:getStamina()
        if staminaMinutes > 2400 and self:isPremium() then
            exp = exp * 1.5
        elseif staminaMinutes <= 840 then
            exp = exp * 0.5
        end
    end

    return exp
end

In past I changed little script bcs I have monster lvl on server and I want to scale exp with monster lvl:

 

Xikini

I whore myself out for likes
Support Team
Joined
Nov 17, 2010
Messages
5,403
Solutions
361
Reaction score
3,513
I have something like this:
Lua:
function Player:onGainExperience(source, exp, rawExp)
if source:isMonster() then
        local bonusExperience = source:getMonsterLevel() * 5
        if source:getMonsterLevel() > 0 and bonusExperience > 1 then
            exp = exp * bonusExperience
        end
    end

    -- Soul regeneration
    local vocation = self:getVocation()
    if self:getSoul() < vocation:getMaxSoul() and exp >= self:getLevel() then
        soulCondition:setParameter(CONDITION_PARAM_SOULTICKS, vocation:getSoulGainTicks() * 1000)
        self:addCondition(soulCondition)
    end

    -- Apply experience stage multiplier
    exp = exp * Game.getExperienceStage(self:getLevel())

    -- Stamina modifier
    if configManager.getBoolean(configKeys.STAMINA_SYSTEM) then
        useStamina(self)

        local staminaMinutes = self:getStamina()
        if staminaMinutes > 2400 and self:isPremium() then
            exp = exp * 1.5
        elseif staminaMinutes <= 840 then
            exp = exp * 0.5
        end
    end

    return exp
end

In past I changed little script bcs I have monster lvl on server and I want to scale exp with monster lvl:

change this line
if source:isMonster() then
like this
if source and source:isMonster() then

That way you're confirming that source exists, in some fashion before attempting to use it to verify information with it.
 
Top