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

OTX 3 Event Script

athenso

Average Coder
Joined
May 31, 2011
Messages
155
Solutions
3
Reaction score
23
I have an event script for Events -> creature.lua The script is supposed to be working, so a player can not attack another player that is 30% less than the attacker. In this case its for my reborn storages.

Code:
function Creature:onTargetCombat(target)
local percent = 0.3   --- devide the percent by 100 , so 0.3 = 30%
local player = Player(cid)
local reb = 85987
        local level, tlevel = getStorageValue(cid, reb), getStorageValue(target, reb)
        if tlevel < level + level * - percent / 100 or tlevel > level + level * percent / 100 then
             player:sendTextMessage(MESSAGE_INFO_DESCR, "You cannot attack players with more than ".. percent .."% level difference.")
                return false
        else
        return true
    end
end


The script is not working, and it is not throwing any errors. Some outside eyes on it would be nice. Thanks
 
Lua:
    function Creature:onTargetCombat(target)
    local percent = 130 -- 100 is default, 130 is 100 + 30%
    local reb = 85987
    if self:isPlayer() and target:isPlayer() then
    local level = player:getStorageValue(reb)
    local tlevel = target:getStorageValue(reb)
    local diff = ((level / tlevel) * 100)
    if diff >= percent then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You cannot attack players with more than ".. (percent - 100) .."% level difference.")
        return false
    end end
        return true
    end
 
Is onTargetCombat the same as onStatsChange in 0.3.7?
If it's not.. this only stops the player from selecting the target.
Area damage is still gonna murder each other.
 
Lua:
    function Creature:onTargetCombat(target)
    local percent = 130 -- 100 is default, 130 is 100 + 30%
    local reb = 85987
    if self:isPlayer() and target:isPlayer() then
    local level = player:getStorageValue(reb)
    local tlevel = target:getStorageValue(reb)
    local diff = ((level / tlevel) * 100)
    if diff >= percent then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You cannot attack players with more than ".. (percent - 100) .."% level difference.")
        return false
    end end
        return true
    end

Tested this and it has the same outcome as my script had.

Is onTargetCombat the same as onStatsChange in 0.3.7?
If it's not.. this only stops the player from selecting the target.
Area damage is still gonna murder each other.

I knew i would overlook something, but 1 step at a time lol. Im just trying to keep a reborn 30 from destroying a level 400 non reborn.
 
Because of OtLand outage, I had to save this in a notepad for like 5 hours. xD
-----------
I knew i would overlook something, but 1 step at a time lol. Im just trying to keep a reborn 30 from destroying a level 400 non reborn.

Well let's look at your math for a second.
Let's say we have a level 100 rebirth and a level 200 rebirth.
Lua:
if tlevel < level + level * - percent / 100 or tlevel > level + level * percent / 100 then
-- if 100 < 200 + 200 * - 0.3 / 100 or 200 > 100 + 100 * 0.3 / 100 then
   -- if 100 < 200 + -60 / 100 or 200 > 100 + 30 / 100 then
   -- if 100 < 200 + -0.6 or 200 > 100 + 0.3 then
   -- if 100 < 199.4 or 200 > 100.3 then

If you actually write out your equation, you can see it makes no sense.

So let's try to think about what we want to do.

Alright, we want to check if these people can attack each other.
Since we KNOW that cid is the person initiating the attack..
We only need to know if person X can attack person Y.
Since we don't know if x is lower or higher then y to start, we need to check both ways.

So, let's start here.
Lua:
function()
    local x = cid_rebirth
    local y = target_rebirth

    if x > y then
        -- do this
    elseif x < y then
        -- do this
    end
    return true
end

Alright, now we want to find out what 30% of number x .
For clarity, let's assign x to 100 and y to 200 like before.
Lua:
function()
    local x = 100
    local y = 200
    local x_range = x * 0.3 -- 30
  
    if x > y then
       -- do this
    elseif x < y then
        -- do this
    end
    return true
end

Let's start with x being a lower value then y.
We know x's range is 30% , we just need to see if y is within that range.
We also know we want this statement to return false, we will ask the question backwards.
Lua:
function()
   local x = 100
   local y = 200
   local x_range = x * 0.3 -- 30
  
   if x > y then
       if x + x_range < y then
       --if 100 + 30 < 200 then
           return false
       end
   elseif x < y then
       -- do this
   end
   return true
end

Alright, so we got some working math.
At this point we need to start working on if x is higher then y.
In this case we'll want to swap it around, and see what the target's range is, instead of our range, since they are the smaller level.
So let's add y_range into the equation.
And since we don't need to check x_range inside the second check, let's move these locals too.
Lua:
function()
   local x = 200
   local y = 100
  
   if x > y then
       local x_range = x * 0.3
       if x + x_range < y then
           return false
       end
   elseif x < y then
       local y_range = y * 0.3 -- 30
       if y + y_range < x then
       --if 100 + 30 < 200 then
           return false
       end
   end
   return true
end

Alright still working.
Now let's try some edge cases in both directions.
Let's just check the equations, no need to post entire code.
Lua:
if x + x_range < y then
--if 100 + 30 < 130 then
   return false
end

if y + y_range < x then
--if 100 + 30 < 130 then
   return false
end
Still working.
Alright, let's turn this into a real function you can use..
and plug it into the script.

----------------------------
I'm not very good with OTX or 1.0+ scripting..
but isn't
Code:
local player = Player(cid)
this incorrect?
Shouldn't it be like this?
Code:
local self = Player(Creature)
I'm gonna write the script as if my version is correct.
Someone is gonna have to fix up this part if it's wrong, but my math and reasoning is gonna be correct inside the function.
----------------------------
Lua:
local function isInRebirthLevelRange(cid, target)
   local reb = 85987
   local x = getStorageValue(cid, reb)
   local y = getStorageValue(target, reb)
   local range = 0.3
  
   if x > y then
       local x_range = x * range
       if x + x_range < y then
           return false
       end
   elseif x < y then
       local y_range = y * range
       if y + y_range < x then
           return false
       end
   end
   return true
end

function Creature:onTargetCombat(target)
   -- this extra check is so that monsters and npc's don't get their storages checked
   if Creature:isPlayer() and target:isPlayer() then
       local player = Player(Creature)
       if not isInRebirthLevelRange(player, target) then
           player:sendTextMessage(MESSAGE_INFO_DESCR, "You cannot attack players with more than 30% level difference.")
           return false
       end
   end
   return true
end

Well anyways, hope that works.
If not, at least you know the math is correct. xP
 
Because of OtLand outage, I had to save this in a notepad for like 5 hours. xD
-----------


Well let's look at your math for a second.
Let's say we have a level 100 rebirth and a level 200 rebirth.
Lua:
if tlevel < level + level * - percent / 100 or tlevel > level + level * percent / 100 then
-- if 100 < 200 + 200 * - 0.3 / 100 or 200 > 100 + 100 * 0.3 / 100 then
   -- if 100 < 200 + -60 / 100 or 200 > 100 + 30 / 100 then
   -- if 100 < 200 + -0.6 or 200 > 100 + 0.3 then
   -- if 100 < 199.4 or 200 > 100.3 then

If you actually write out your equation, you can see it makes no sense.

So let's try to think about what we want to do.

Alright, we want to check if these people can attack each other.
Since we KNOW that cid is the person initiating the attack..
We only need to know if person X can attack person Y.
Since we don't know if x is lower or higher then y to start, we need to check both ways.

So, let's start here.
Lua:
function()
    local x = cid_rebirth
    local y = target_rebirth

    if x > y then
        -- do this
    elseif x < y then
        -- do this
    end
    return true
end

Alright, now we want to find out what 30% of number x .
For clarity, let's assign x to 100 and y to 200 like before.
Lua:
function()
    local x = 100
    local y = 200
    local x_range = x * 0.3 -- 30
 
    if x > y then
       -- do this
    elseif x < y then
        -- do this
    end
    return true
end

Let's start with x being a lower value then y.
We know x's range is 30% , we just need to see if y is within that range.
We also know we want this statement to return false, we will ask the question backwards.
Lua:
function()
   local x = 100
   local y = 200
   local x_range = x * 0.3 -- 30
 
   if x > y then
       if x + x_range < y then
       --if 100 + 30 < 200 then
           return false
       end
   elseif x < y then
       -- do this
   end
   return true
end

Alright, so we got some working math.
At this point we need to start working on if x is higher then y.
In this case we'll want to swap it around, and see what the target's range is, instead of our range, since they are the smaller level.
So let's add y_range into the equation.
And since we don't need to check x_range inside the second check, let's move these locals too.
Lua:
function()
   local x = 200
   local y = 100
 
   if x > y then
       local x_range = x * 0.3
       if x + x_range < y then
           return false
       end
   elseif x < y then
       local y_range = y * 0.3 -- 30
       if y + y_range < x then
       --if 100 + 30 < 200 then
           return false
       end
   end
   return true
end

Alright still working.
Now let's try some edge cases in both directions.
Let's just check the equations, no need to post entire code.
Lua:
if x + x_range < y then
--if 100 + 30 < 130 then
   return false
end

if y + y_range < x then
--if 100 + 30 < 130 then
   return false
end
Still working.
Alright, let's turn this into a real function you can use..
and plug it into the script.

----------------------------
I'm not very good with OTX or 1.0+ scripting..
but isn't
Code:
local player = Player(cid)
this incorrect?
Shouldn't it be like this?
Code:
local self = Player(Creature)
I'm gonna write the script as if my version is correct.
Someone is gonna have to fix up this part if it's wrong, but my math and reasoning is gonna be correct inside the function.
----------------------------
Lua:
local function isInRebirthLevelRange(cid, target)
   local reb = 85987
   local x = getStorageValue(cid, reb)
   local y = getStorageValue(target, reb)
   local range = 0.3
 
   if x > y then
       local x_range = x * range
       if x + x_range < y then
           return false
       end
   elseif x < y then
       local y_range = y * range
       if y + y_range < x then
           return false
       end
   end
   return true
end

function Creature:onTargetCombat(target)
   -- this extra check is so that monsters and npc's don't get their storages checked
   if Creature:isPlayer() and target:isPlayer() then
       local player = Player(Creature)
       if not isInRebirthLevelRange(player, target) then
           player:sendTextMessage(MESSAGE_INFO_DESCR, "You cannot attack players with more than 30% level difference.")
           return false
       end
   end
   return true
end

Well anyways, hope that works.
If not, at least you know the math is correct. xP

This script looks amazing. I tried it and nothing happened. I was looking at it last night, and it looks as though the script is never getting called being put where it is. So i will have to find a different method to perform the check. I thought for sure Events would be the place to try this, as in OTX there is no "onCombat" for creature scripts. Thanks anyways
 
Tested this and it has the same outcome as my script had.



I knew i would overlook something, but 1 step at a time lol. Im just trying to keep a reborn 30 from destroying a level 400 non reborn.

Lua:
    function Creature:onTargetCombat(target)
   
    if not self then
    return true
    end

    local percent = 130 -- 100 is default, 130 is 100 + 30%
    local reb = 85987
    if self:isPlayer() and target:isPlayer() then
    local level = player:getLevel()
    local tlevel = target:getLevel()
    local diff = ((level / tlevel) * 100)
    if diff >= percent then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You cannot attack players with more than ".. (percent - 100) .."% level difference.")
        return false
    end end

    return true
    end
 
Lua:
    function Creature:onTargetCombat(target)
  
    if not self then
    return true
    end

    local percent = 130 -- 100 is default, 130 is 100 + 30%
    local reb = 85987
    if self:isPlayer() and target:isPlayer() then
    local level = player:getLevel()
    local tlevel = target:getLevel()
    local diff = ((level / tlevel) * 100)
    if diff >= percent then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You cannot attack players with more than ".. (percent - 100) .."% level difference.")
        return false
    end end

    return true
    end
Code:
level = 100
tlevel = 200
diff = (100 / 200) * 100
-- 2 * 100
if 200 >= 130 then
The math doesn't add up.
 
Code:
level = 100
tlevel = 200
diff = (100 / 200) * 100
-- 2 * 100
if 200 >= 130 then
The math doesn't add up.

Lua:
1.3 == 130 ( 1.3 * 100 )
2.0 == 200 ( 2.0 * 100 )

What are you talking about?
it's the same comparison
just that it's better not to use decimals
 
Last edited:
Lua:
1.3 == 130 ( 1.3 * 100 )
2.0 == 200 ( 2.0 * 200 )

What are you talking about?
it's the same comparison
just that it's better not to use decimals
if you if you actually read your script instead of quoting me and telling me bullshit you would know you're wrong
 
Make sure you enabled it in events.xml
Lua:
<event class="Creature" method="onTargetCombat" enabled="1" />

Lua:
-- local variables
local rebirthStorage = 85987
local percent = 0.3 -- 30%

function Player:canAttack(target)
    local pLvl, tLvl = self:getStorageValue(rebirthStorage), target:getStorageValue(rebirthStorage)
    -- if the target is equal/higher level then he can attack automatically
    if tLvl >= pLvl then
        return true
    end
 
    -- if the diff between the lower target and player is less than or equal to 30% of player level then he can attack
    local diff = pLvl - tLvl
    if diff <= (pLvl * percent) then
        return true
    end
return false
end

function Creature:onTargetCombat(target)
    if not self then
        return true
    end
 
    if self:isPlayer() and target:isPlayer() then
        if not self:canAttack(target) then
            self:sendTextMessage(MESSAGE_INFO_DESCR, "You may not attack players with more than " .. percent * 100 .. "% rebirth level difference.")
            return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER
        end
    end
return true
end
 
Last edited:
Back
Top