• 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!

RevScripts Protection by % based on a storage value.

nefinoo

Carnage.flv
Joined
Sep 11, 2010
Messages
549
Solutions
1
Reaction score
58
Location
Lo Mochis, Sinaloa
Could you help me with a revscript to protect those who have a 30% "score" lower than other players? I am using storage 757557, for example if the attacking player has a score of 300, and the attacked player has a score less than 200, he does not suffer damage or activate pk on the attacker.
 
Solution
more or less:
data/scripts/script.lua
Lua:
local storage = 757557
local difference = 30 -- 30%

local ec = EventCallback

function ec.onTargetCombat(creature, target)
    if creature and target and creature:isPlayer() and target:isPlayer() then
        local creatureScore = creature:getStorageValue(storage)
        if math.abs((creatureScore - target:getStorageValue(storage)) / creatureScore * 100) <= difference then
            return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER
        end
    end
    return RETURNVALUE_NOERROR
end

ec:register(-777)
The players that differ more than defined in the difference = 30% variable cannot attack each other.
more or less:
data/scripts/script.lua
Lua:
local storage = 757557
local difference = 30 -- 30%

local ec = EventCallback

function ec.onTargetCombat(creature, target)
    if creature and target and creature:isPlayer() and target:isPlayer() then
        local creatureScore = creature:getStorageValue(storage)
        if math.abs((creatureScore - target:getStorageValue(storage)) / creatureScore * 100) <= difference then
            return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER
        end
    end
    return RETURNVALUE_NOERROR
end

ec:register(-777)
The players that differ more than defined in the difference = 30% variable cannot attack each other.
 
Solution
more or less:
data/scripts/script.lua
Lua:
local storage = 757557
local difference = 30 -- 30%

local ec = EventCallback

function ec.onTargetCombat(creature, target)
    if creature and target and creature:isPlayer() and target:isPlayer() then
        local creatureScore = creature:getStorageValue(storage)
        if math.abs((creatureScore - target:getStorageValue(storage)) / creatureScore * 100) <= difference then
            return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER
        end
    end
    return RETURNVALUE_NOERROR
end

ec:register(-777)
The players that differ more than defined in the difference = 30% variable cannot attack each other.
Not working :S
 
And if you try this one?... (this is a modified version of the script that @Sarah Wesker have created), thanks Sarah, also hope this works.

Lua:
local storage = 757557
local difference = 30 -- 30%

local ec = EventCallback

function ec.onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
    if attacker and creature and attacker:isPlayer() and creature:isPlayer() then
        local creatureScore = attacker:getStorageValue(storage)
        if math.abs((creatureScore - creature:getStorageValue(storage)) / creatureScore * 100) <= difference then
            return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER
        end
    end
    return primaryDamage, primaryType, -secondaryDamage, secondaryType, origin
end

ec:register(-777)
 
And if you try this one?... (this is a modified version of the script that @Sarah Wesker have created), thanks Sarah, also hope this works.

Lua:
local storage = 757557
local difference = 30 -- 30%

local ec = EventCallback

function ec.onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
    if attacker and creature and attacker:isPlayer() and creature:isPlayer() then
        local creatureScore = attacker:getStorageValue(storage)
        if math.abs((creatureScore - creature:getStorageValue(storage)) / creatureScore * 100) <= difference then
            return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER
        end
    end
    return primaryDamage, primaryType, -secondaryDamage, secondaryType, origin
end

ec:register(-777)
Got this:

Lua:
Lua Script Error: [Scripts Interface]
C:\Users\Nefi\OneDrive\Escritorio\Minecraft files\data\scripts\CUSTOMS\PROTECTION.lua
LuaScriptInterface::luaDebugPrint(). [Warning - EventCallback::onHealthChange] is not a valid callback.
stack traceback:
        ...rio\Minecraft files\data\scripts/lib\event_callbacks.lua:99: in function '__newindex'
        ...orio\Minecraft files\data\scripts\CUSTOMS\PROTECTION.lua:6: in main chunk
        [C]: in function 'reload'
        data/talkactions/scripts/reload.lua:81: in function <data/talkactions/scripts/reload.lua:59>

Lua Script Error: [Scripts Interface]
C:\Users\Nefi\OneDrive\Escritorio\Minecraft files\data\scripts\CUSTOMS\PROTECTION.lua
LuaScriptInterface::luaDebugPrint(). [Warning - EventCallback::register] need to setup a callback before you can register.
stack traceback:
        ...rio\Minecraft files\data\scripts/lib\event_callbacks.lua:67: in function 'register'
        ...orio\Minecraft files\data\scripts\CUSTOMS\PROTECTION.lua:16: in main chunk
        [C]: in function 'reload'
        data/talkactions/scripts/reload.lua:81: in function <data/talkactions/scripts/reload.lua:59>
 
and if try to remove the callback?... just this line... "local ec = EventCallback"

and change this ...
function ec.onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)

for this...

function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)

:'(

if not work, you may try this "creatureevent"

Lua:
local creatureEvent = CreatureEvent("HolyProtection")
function creatureEvent.onPrepareDeath(creature, killer)
    if creature and killer and creature:isPlayer() and killer:isPlayer() then
        local creatureScore = creature:getStorageValue(storage)
        if math.abs((creatureScore - killer:getStorageValue(storage)) / creatureScore * 100) <= difference then
            return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER
        end
    end
    return RETURNVALUE_NOERROR
end
creatureEvent:register()

local creatureEvent = CreatureEvent("HolyProtectionLogin")
function creatureEvent.onLogin(player)
    player:registerEvent("HolyProtection")
    return true
end
creatureEvent:register()
 
Last edited:
I'm a JavaScript dev but shouldn't it be ec = EventCallback() so you call the constructor and actually instantiate the object before registering the callback? I'd assume without the parens you're just referring to the constructor itself and not your new EventCallback instance

Like this:
Lua:
local storage = 757557
local difference = 30 -- 30%

local ec = EventCallback()

function ec.onTargetCombat(creature, target)
    if creature and target and creature:isPlayer() and target:isPlayer() then
        local creatureScore = creature:getStorageValue(storage)
        if math.abs((creatureScore - target:getStorageValue(storage)) / creatureScore * 100) <= difference then
            return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER
        end
    end
    return RETURNVALUE_NOERROR
end

ec:register(-777)
 
more or less:
data/scripts/script.lua
Lua:
local storage = 757557
local difference = 30 -- 30%

local ec = EventCallback

function ec.onTargetCombat(creature, target)
    if creature and target and creature:isPlayer() and target:isPlayer() then
        local creatureScore = creature:getStorageValue(storage)
        if math.abs((creatureScore - target:getStorageValue(storage)) / creatureScore * 100) <= difference then
            return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER
        end
    end
    return RETURNVALUE_NOERROR
end

ec:register(-777)
The players that differ more than defined in the difference = 30% variable cannot attack each other.
does this onTargetCombat work with area damages, that you can use without a specific target?
lets say you use an ultimate explosion without a target, would the player still be protected?
 
does this onTargetCombat work with area damages, that you can use without a specific target?
lets say you use an ultimate explosion without a target, would the player still be protected?
Yes, it will trigger once for each creature that is hit by the AOE.
 
Sarah's script is correct, except on line 9 <= should be >=.
Its not working for me :/ and dont send any error
Solved, i have onTargetCombat disable on events.xml

Lua:
local storage = 757557
local difference = 25 -- 30%

local ec = EventCallback

function ec.onTargetCombat(creature, target)
    if creature and target and creature:isPlayer() and target:isPlayer() then
        local creatureScore = creature:getStorageValue(storage)
        local comparation = math.abs(creatureScore * (difference / 100))
        if target:getStorageValue(storage) <= comparation then
            return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER
        end
    end
    return RETURNVALUE_NOERROR
end

ec:register(-777)
This is what i a want, but i cant attack anyone with a high score or low score

instead of returning a value RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER with a msg "You may not attack this player" says "You may not attack this creature"
 
Last edited:
Back
Top