• 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.4] Turn on/off pvp by using item

Marko999x

999x era
Premium User
Joined
Dec 14, 2017
Messages
2,751
Solutions
80
Reaction score
1,887
Location
Germany
Hello :D
Title says everything :D
Have a great day :D
Tested on TFS 1.4 without any errors right now.
If you have any errors, let me know!

Lua:
-------------------------------------------------------------------------

-------------------------------------------------------------------------
local action = Action()

function action.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if not player:getTile():hasFlag(TILESTATE_PROTECTIONZONE) then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You can change your pvp only in protection zone!")
        return true
    end
    if player:getStorageValue(4500) == 1 then   
        player:setStorageValue(4500, -1)
        player:sendTextMessage(MESSAGE_INFO_DESCR, 'You have turned on PVP')
    else
        player:setStorageValue(4500, 1)
        player:sendTextMessage(MESSAGE_INFO_DESCR, 'You have turned off PVP')
    end
    --item:remove()
    return true
end

action:id(8920)
action:register()
-------------------------------------------------------------------------

-------------------------------------------------------------------------
local ec = EventCallback

ec.onTargetCombat = function(self, target)
    if not self or not target then
        return true
    end
    if self:isMonster() or target:isMonster() then
        return true
    end
    if self:getStorageValue(4500) == 1 or target:getStorageValue(4500) == 1 then
        return RETURNVALUE_YOUMAYNOTATTACKTHISCREATURE
    end
    return RETURNVALUE_NOERROR
end

ec:register()
-------------------------------------------------------------------------

-------------------------------------------------------------------------
local ec = EventCallback

ec.onLook = function(self, thing, position, distance, description)
    if not self or not thing then
        return description
    end
    if thing:isMonster() or thing:isItem() then
        return description
    end
    if thing:isPlayer() then
        description = description .."\n"
        if thing:getStorageValue(4500) == 1 then
            description = description .. "PVP OFF"
        else
            description = description .. "PVP ON"
        end
    end
    return description
end

ec:register()
-------------------------------------------------------------------------

-------------------------------------------------------------------------
 
Can you help me with this?

Code:
 Lua Script Error: [Event Interface]
2023-01-03 03:11:14 -  data/events/scripts/creature.lua:Creature@onTargetCombat
2023-01-03 03:11:14 -  /home/otg/data/scripts/pvp_system.lua:36: attempt to call method 'getStorageValue' (a nil value)
2023-01-03 03:11:14 -  stack traceback:
2023-01-03 03:11:14 -   [C]: in function 'getStorageValue'
2023-01-03 03:11:14 -   /home/otg/data/scripts/pvp_system.lua:36: in function '?'
2023-01-03 03:11:14 -   /home/otg/data/scripts/lib/event_callbacks.lua:131: in function </home/otg/data/scripts/lib/event_callbacks.lua:127>
2023-01-03 03:11:14 -   (...tail calls...)
2023-01-03 03:11:14 -   [C]: in function 'execute'
2023-01-03 03:11:14 -   data/spells/scripts/attack/fierce berserk.lua:21: in function <data/spells/scripts/attack/fierce berserk.lua:20>

I got messages like this one using other area spells/runes - see another example:

Code:
2023-01-03 01:08:21 -  Lua Script Error: [Event Interface]
2023-01-03 01:08:21 -  data/events/scripts/creature.lua:Creature@onTargetCombat
2023-01-03 01:08:21 -  /home/otg/data/scripts/pvp_system.lua:36: attempt to call method 'getStorageValue' (a nil value)
2023-01-03 01:08:21 -  stack traceback:
2023-01-03 01:08:21 -      [C]: in function 'getStorageValue'
2023-01-03 01:08:21 -      /home/otg/data/scripts/pvp_system.lua:36: in function '?'
2023-01-03 01:08:21 -      /home/otg/data/scripts/lib/event_callbacks.lua:131: in function </home/otg/data/scripts/lib/event_callbacks.lua:127>
2023-01-03 01:08:21 -      (...tail calls...)
2023-01-03 01:08:21 -      [C]: in function 'execute'
2023-01-03 01:08:21 -      data/spells/scripts/attack/avalanche.lua:16: in function <data/spells/scripts/attack/avalanche.lua:15>

All the rest of things are working perfectly.
People can use the Item to change between pvpON/OFF.
pvpOFF cannot attack or be attacked, normally.
I did not found whats these error messages means, cause everything is working :/

I made some translates and adjusts in the main script, so i'll post the 3 files that is in the error return
 

Attachments

Hey, now im not have chance to test it, but i have question its turning off pvp like, he can't attack any one and he can;t be attacked? Or only who use it can't attack other ppl but still can be attacked?
 
Hey, now im not have chance to test it, but i have question its turning off pvp like, he can't attack any one and he can;t be attacked? Or only who use it can't attack other ppl but still can be attacked?

You cant attack
You cant be attacked
 
You must return RETURNVALUE_NOERROR in all cases, except when protected, you return RETURNVALUE_YOUMAYNOTATTACKTHISCREATURE to protect.
if you return true, you would be sending RETURNVALUE_NOTPOSSIBLE since in lua, true is 1 and false is 0
This method no longer uses booleans
so it is an error to return true if you think it works the same as RETURNVALUE_NOERROR
1673723227102.png
 
You must return RETURNVALUE_NOERROR in all cases, except when protected, you return RETURNVALUE_YOUMAYNOTATTACKTHISCREATURE to protect.
if you return true, you would be sending RETURNVALUE_NOTPOSSIBLE since in lua, true is 1 and false is 0
This method no longer uses booleans
so it is an error to return true if you think it works the same as RETURNVALUE_NOERROR
View attachment 72818

good to know
thanks
 
Can you help me with this?

Code:
 Lua Script Error: [Event Interface]
2023-01-03 03:11:14 -  data/events/scripts/creature.lua:Creature@onTargetCombat
2023-01-03 03:11:14 -  /home/otg/data/scripts/pvp_system.lua:36: attempt to call method 'getStorageValue' (a nil value)
2023-01-03 03:11:14 -  stack traceback:
2023-01-03 03:11:14 -   [C]: in function 'getStorageValue'
2023-01-03 03:11:14 -   /home/otg/data/scripts/pvp_system.lua:36: in function '?'
2023-01-03 03:11:14 -   /home/otg/data/scripts/lib/event_callbacks.lua:131: in function </home/otg/data/scripts/lib/event_callbacks.lua:127>
2023-01-03 03:11:14 -   (...tail calls...)
2023-01-03 03:11:14 -   [C]: in function 'execute'
2023-01-03 03:11:14 -   data/spells/scripts/attack/fierce berserk.lua:21: in function <data/spells/scripts/attack/fierce berserk.lua:20>

I got messages like this one using other area spells/runes - see another example:

Code:
2023-01-03 01:08:21 -  Lua Script Error: [Event Interface]
2023-01-03 01:08:21 -  data/events/scripts/creature.lua:Creature@onTargetCombat
2023-01-03 01:08:21 -  /home/otg/data/scripts/pvp_system.lua:36: attempt to call method 'getStorageValue' (a nil value)
2023-01-03 01:08:21 -  stack traceback:
2023-01-03 01:08:21 -      [C]: in function 'getStorageValue'
2023-01-03 01:08:21 -      /home/otg/data/scripts/pvp_system.lua:36: in function '?'
2023-01-03 01:08:21 -      /home/otg/data/scripts/lib/event_callbacks.lua:131: in function </home/otg/data/scripts/lib/event_callbacks.lua:127>
2023-01-03 01:08:21 -      (...tail calls...)
2023-01-03 01:08:21 -      [C]: in function 'execute'
2023-01-03 01:08:21 -      data/spells/scripts/attack/avalanche.lua:16: in function <data/spells/scripts/attack/avalanche.lua:15>

All the rest of things are working perfectly.
People can use the Item to change between pvpON/OFF.
pvpOFF cannot attack or be attacked, normally.
I did not found whats these error messages means, cause everything is working :/

I made some translates and adjusts in the main script, so i'll post the 3 files that is in the error return
Any solution?
 
Any solution?


maybe can you try with this changes:

Lua:
local action = Action()

function action.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if not player:getTile():hasFlag(TILESTATE_PROTECTIONZONE) then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You can change your PvP status only in a protection zone!")
        return true
    end
    if player:getStorageValue(4500) == 1 then   
        player:setStorageValue(4500, 0) -- 0 means PvP is on, 1 means it's off
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You have turned on PvP.")
    else
        player:setStorageValue(4500, 1) -- 0 means PvP is on, 1 means it's off
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You have turned off PvP.")
    end
    return true
end

action:id(8920)
action:register()

local ec = EventCallback()

ec.onTargetCombat = function(self, target)
    if not self or not target then
        return RETURNVALUE_NOERROR
    end
    if self:isMonster() or target:isMonster() then
        return RETURNVALUE_NOERROR
    end
    if self:getStorageValue(4500) == 1 or target:getStorageValue(4500) == 1 then
        return RETURNVALUE_YOUMAYNOTATTACKTHISCREATURE
    end
    return RETURNVALUE_NOERROR
end

ec:register()

ec.onLook = function(self, thing, position, distance, description)
    if not self or not thing then
        return description
    end
    if thing:isMonster() or thing:isItem() then
        return description
    end
    if thing:isPlayer() then
        description = description .. "\nPvP: "
        if thing:getStorageValue(4500) == 1 then
            description = description .. "OFF"
        else
            description = description .. "ON"
        end
    end
    return description
end

ec:register()
 
maybe can you try with this changes:

Lua:
local action = Action()

function action.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if not player:getTile():hasFlag(TILESTATE_PROTECTIONZONE) then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You can change your PvP status only in a protection zone!")
        return true
    end
    if player:getStorageValue(4500) == 1 then  
        player:setStorageValue(4500, 0) -- 0 means PvP is on, 1 means it's off
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You have turned on PvP.")
    else
        player:setStorageValue(4500, 1) -- 0 means PvP is on, 1 means it's off
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You have turned off PvP.")
    end
    return true
end

action:id(8920)
action:register()

local ec = EventCallback()

ec.onTargetCombat = function(self, target)
    if not self or not target then
        return RETURNVALUE_NOERROR
    end
    if self:isMonster() or target:isMonster() then
        return RETURNVALUE_NOERROR
    end
    if self:getStorageValue(4500) == 1 or target:getStorageValue(4500) == 1 then
        return RETURNVALUE_YOUMAYNOTATTACKTHISCREATURE
    end
    return RETURNVALUE_NOERROR
end

ec:register()

ec.onLook = function(self, thing, position, distance, description)
    if not self or not thing then
        return description
    end
    if thing:isMonster() or thing:isItem() then
        return description
    end
    if thing:isPlayer() then
        description = description .. "\nPvP: "
        if thing:getStorageValue(4500) == 1 then
            description = description .. "OFF"
        else
            description = description .. "ON"
        end
    end
    return description
end

ec:register()
The problem happens when you hit a NPC with an area spell. Everything works fine, but this.
Example: (when you use exori mas that hits a NPC)

Lua Script Error: [Event Interface]
2023-04-08 00:45:46 - data/events/scripts/creature.lua:Creature@onTargetCombat
2023-04-08 00:45:46 - /home/otg/data/scripts/pvp_system.lua:50: attempt to call method 'getStorageValue' (a nil value)
2023-04-08 00:45:46 - stack traceback:
2023-04-08 00:45:46 - [C]: in function 'getStorageValue'
2023-04-08 00:45:46 - /home/otg/data/scripts/pvp_system.lua:50: in function '?'
2023-04-08 00:45:46 - /home/otg/data/scripts/lib/event_callbacks.lua:131: in function </home/otg/data/scripts/lib/event_callbacks.lua:127>
2023-04-08 00:45:46 - (...tail calls...)
2023-04-08 00:45:46 - [C]: in function 'execute'
2023-04-08 00:45:46 - data/spells/scripts/attack/groundshaker.lua:20: in function <data/spells/scripts/attack/groundshaker.lua:19>
 
The problem happens when you hit a NPC with an area spell. Everything works fine, but this.
Example: (when you use exori mas that hits a NPC)

Lua Script Error: [Event Interface]
2023-04-08 00:45:46 - data/events/scripts/creature.lua:Creature@onTargetCombat
2023-04-08 00:45:46 - /home/otg/data/scripts/pvp_system.lua:50: attempt to call method 'getStorageValue' (a nil value)
2023-04-08 00:45:46 - stack traceback:
2023-04-08 00:45:46 - [C]: in function 'getStorageValue'
2023-04-08 00:45:46 - /home/otg/data/scripts/pvp_system.lua:50: in function '?'
2023-04-08 00:45:46 - /home/otg/data/scripts/lib/event_callbacks.lua:131: in function </home/otg/data/scripts/lib/event_callbacks.lua:127>
2023-04-08 00:45:46 - (...tail calls...)
2023-04-08 00:45:46 - [C]: in function 'execute'
2023-04-08 00:45:46 - data/spells/scripts/attack/groundshaker.lua:20: in function <data/spells/scripts/attack/groundshaker.lua:19>

try changing the line

Lua:
    if self:isMonster() or target:isMonster() then

to:

Lua:
 if self:isMonster() or target:isMonster() or self:isNPC() or target:isNPC() then
 
try changing the line

Lua:
    if self:isMonster() or target:isMonster() then

to:

Lua:
 if self:isMonster() or target:isMonster() or self:isNPC() or target:isNPC() then

Niice, dude. Now it's working perfectly.
It is worth mentioning that the correct is "isNpc", not "isNPC".

Thanks a lot, dude!
 
Niice, dude. Now it's working perfectly.
It is worth mentioning that the correct is "isNpc", not "isNPC".

Thanks a lot, dude!


Perfect, It can be said that it is solved., and I apologize in my BASE is isNPC() o_O:oops:
 
How do I prevent:
Party and Guild members to Hit Each Other;
• People Hit their Own Summons;

Is in this same line? I tried some things, but nothing worked.

The line:
Lua:
 if self:isMonster() or target:isMonster() or self:isNPC() or target:isNpc() then
 
Last edited:
Back
Top