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

Lua [TFS 1.X] Random Reward Action

demon088

#088 in the Horde
Joined
Jun 17, 2009
Messages
249
Solutions
3
Reaction score
30
Location
Hell
Hello OtLand!
I wanted to share this action script that I have written with other scripts posted in this site. Review it and if someone wants to use, it would be cool! I'm using TFS 1.2 and it is working fine!
It is useful to make this a random reward box. In my own OTS I use 6 different items with rewards from different levels, this one is in the middle and gives equipment from all vocations. If you have any issue, suggestion or improvement, please leave your comments, It would be fun to work on it and share with you all!
To add it to your own server just follow the next:
Add to data/actions/actions.xml
Code:
<action itemid="4863" script="randomreward.lua"/>

Then create actions/scripts/randomreward.lua
Script:
Lua:
local REWARD = {2471, 2496, 2343, 7902, 18398, 8868, 8867, 15489, 12643, 8887, 8886, 8885, 8877, 8878, 15407, 8884, 8881, 12607, 8889, 8865, 8890, 6132, 11117, 11118, 11240, 9933, 2358, 9777, 18405, 2469}
local JEWEL = {2171, 2661, 2168, 2214, 2165, 2213, 2207, 2169, 2122, 2127, 2124, 2121, 7697, 13825, 6300}
function onUse(cid, item, fromPosition, itemEx, toPosition)
      local randomChance = math.random(2, #REWARD)
      doPlayerAddItem(cid, REWARD[randomChance], 1)

local randomLoot = math.random(1,20)
    if randomLoot == 1 then
    doPlayerSendTextMessage(cid, 19, "You found an extra item!")
             local randomChance = math.random(2, #REWARD)
              doPlayerAddItem(cid, REWARD[randomChance], 1)
    end

local randomJewel = math.random(1,10)
    if randomJewel == 1 then
    doPlayerSendTextMessage(cid, 19, "You found a jewel!")
      local randomChance = math.random(2, #JEWEL)
      doPlayerAddItem(cid, JEWEL[randomChance], 1)
    end
  
doSendMagicEffect(getPlayerPosition(cid), 73)
   doRemoveItem(item.uid, 1)
   return true
end
Q: How this works?
A: It works giving one random item from 'REWARD', but also you have 5% to receive an additional item from 'REWARD', then you have also 10% chance to get an amulet, a ring or an accesory from 'JEWEL'.

I have also included in my own OTS [TFS 1.1] Item's upgrading by jewels and i added also a small chance to get the upgrading item. This is a good item for quests and provides that spicy fact on getting better items with luck. I hope you could enjoy it and expect good feedback from you! If someone else have worked on similar systems, please comment!
NOTE: This has not been tested in older TFS versions. If it works let me know!
 
Why is it impossible to get the first item in both lists?

local randomChance = math.random(2, #REWARD)
local randomChance = math.random(2, #REWARD)
local randomChance = math.random(2, #JEWEL)
 
Why is it impossible to get the first item in both lists?

local randomChance = math.random(2, #REWARD)
local randomChance = math.random(2, #REWARD)
local randomChance = math.random(2, #JEWEL)
I didn't notice this before. I will test and fix the script. Thanks for your feedback!
EDIT: The correct script should look like this.
Lua:
local REWARD = {2471, 2496, 2343, 7902, 18398, 8868, 8867, 15489, 12643, 8887, 8886, 8885, 8877, 8878, 15407, 8884, 8881, 12607, 8889, 8865, 8890, 6132, 11117, 11118, 11240, 9933, 2358, 9777, 18405, 2469}
local JEWEL = {2171, 2661, 2168, 2214, 2165, 2213, 2207, 2169, 2122, 2127, 2124, 2121, 7697, 13825, 6300}
function onUse(cid, item, fromPosition, itemEx, toPosition)
      local randomChance = math.random(1, #REWARD)
      doPlayerAddItem(cid, REWARD[randomChance], 1)

local randomLoot = math.random(1,20)
    if randomLoot == 1 then
    doPlayerSendTextMessage(cid, 19, "You found an extra item!")
             local randomChance = math.random(1, #REWARD)
              doPlayerAddItem(cid, REWARD[randomChance], 1)
    end

local randomJewel = math.random(1,10)
    if randomJewel == 1 then
    doPlayerSendTextMessage(cid, 19, "You found a jewel!")
      local randomChance = math.random(1, #JEWEL)
      doPlayerAddItem(cid, JEWEL[randomChance], 1)
    end
 
doSendMagicEffect(getPlayerPosition(cid), 73)
   doRemoveItem(item.uid, 1)
   return true
end
 
Last edited:
I've recently noticed this, when you receive an item with charges (Ex: Might Ring which normally has 20 charges), this item has only 1 charge. Same with amulets or other kind of items including charges.
I was trying to include this within the script to add charges to the item, only if it was supposed to have charges like this:
Lua:
local randomGem = math.random(1,10)
    if randomGem == 1 then
    doPlayerSendTextMessage(cid, 19, "You found a jewel!")
    local randomChance = math.random(1, #PRESENT_REDD)
    local id = doCreateItemEx(PRESENT_REDD[randomChance], 50)
    if(doPlayerAddItemEx(cid, id, true) ~= RETURNVALUE_NOERROR) then
        return false
    end
    end
It is working like this, only adding 50 charges to the item:
21:47 You see a might ring (protection physical +20%, energy +20%, earth +20%, fire +20%, ice +20%, holy +20%, death +20%) that has 50 charges left. It weighs 1.00 oz. Item ID: 2164
I tried adding random charges like this :
Code:
local randomGem = math.random(1,10)
    if randomGem == 1 then
    doPlayerSendTextMessage(cid, 19, "You found a jewel!")
    local charges = math.random(20, 100)
    local randomChance = math.random(1, #PRESENT_REDD)
    local id = doCreateItemEx(PRESENT_REDD[randomChance], charges)
    if(doPlayerAddItemEx(cid, id, true) ~= RETURNVALUE_NOERROR) then
        return false
    end
    end
This is the result:
21:47 You see a might ring (protection physical +20%, energy +20%, earth +20%, fire +20%, ice +20%, holy +20%, death +20%) that has 77 charges left. It weighs 1.00 oz. Item ID: 2164
However I wanted to know if is there any way to include charges as registered at items.xml. I mean, if you get the might ring, it will be granted with 20 charges instead of a fixed amount or a random amount, which is good but some items might get op. Just think about a stone skin amulet with 100 charges...
 
I've recently noticed this, when you receive an item with charges (Ex: Might Ring which normally has 20 charges), this item has only 1 charge. Same with amulets or other kind of items including charges.
I was trying to include this within the script to add charges to the item, only if it was supposed to have charges like this:
Lua:
local randomGem = math.random(1,10)
    if randomGem == 1 then
    doPlayerSendTextMessage(cid, 19, "You found a jewel!")
    local randomChance = math.random(1, #PRESENT_REDD)
    local id = doCreateItemEx(PRESENT_REDD[randomChance], 50)
    if(doPlayerAddItemEx(cid, id, true) ~= RETURNVALUE_NOERROR) then
        return false
    end
    end
It is working like this, only adding 50 charges to the item:
21:47 You see a might ring (protection physical +20%, energy +20%, earth +20%, fire +20%, ice +20%, holy +20%, death +20%) that has 50 charges left. It weighs 1.00 oz. Item ID: 2164
I tried adding random charges like this :
Code:
local randomGem = math.random(1,10)
    if randomGem == 1 then
    doPlayerSendTextMessage(cid, 19, "You found a jewel!")
    local charges = math.random(20, 100)
    local randomChance = math.random(1, #PRESENT_REDD)
    local id = doCreateItemEx(PRESENT_REDD[randomChance], charges)
    if(doPlayerAddItemEx(cid, id, true) ~= RETURNVALUE_NOERROR) then
        return false
    end
    end
This is the result:
21:47 You see a might ring (protection physical +20%, energy +20%, earth +20%, fire +20%, ice +20%, holy +20%, death +20%) that has 77 charges left. It weighs 1.00 oz. Item ID: 2164
However I wanted to know if is there any way to include charges as registered at items.xml. I mean, if you get the might ring, it will be granted with 20 charges instead of a fixed amount or a random amount, which is good but some items might get op. Just think about a stone skin amulet with 100 charges...
0.4 -- get's base charges from items.xml
getItemInfo(itemid).charges

1.3 -- get's the remaining charges on the item.
item:getCharges()

1.3 -- get's the base charges? idk never tested.
itemType:getCharges()
 
Lua:
    local randomGem = math.random(1,10)
    if randomGem == 1 then
    doPlayerSendTextMessage(cid, 19, "You found a jewel!")
    local randomChance = math.random(1, #PRESENT_REDD)
    local chargedItem = player:addItem(PRESENT_REDD[randomChance], 1)
    if chargedItem then
        chargedItem:setAttribute(ITEM_ATTRIBUTE_CHARGES, math.random(20, 100))
    end
    end

to edit "any" item attribute with Lua you can use the setAttribute command;

Code:
ITEM_ATTRIBUTE_NONE
ITEM_ATTRIBUTE_ACTIONID
ITEM_ATTRIBUTE_UNIQUEID
ITEM_ATTRIBUTE_DESCRIPTION
ITEM_ATTRIBUTE_TEXT
ITEM_ATTRIBUTE_DATE
ITEM_ATTRIBUTE_WRITER
ITEM_ATTRIBUTE_NAME
ITEM_ATTRIBUTE_ARTICLE
ITEM_ATTRIBUTE_PLURALNAME
ITEM_ATTRIBUTE_WEIGHT
ITEM_ATTRIBUTE_ATTACK
ITEM_ATTRIBUTE_DEFENSE
ITEM_ATTRIBUTE_EXTRADEFENSE
ITEM_ATTRIBUTE_ARMOR
ITEM_ATTRIBUTE_HITCHANCE
ITEM_ATTRIBUTE_SHOOTRANGE
ITEM_ATTRIBUTE_OWNER
ITEM_ATTRIBUTE_DURATION
ITEM_ATTRIBUTE_DECAYSTATE
ITEM_ATTRIBUTE_CORPSEOWNER
ITEM_ATTRIBUTE_CHARGES
ITEM_ATTRIBUTE_FLUIDTYPE
ITEM_ATTRIBUTE_DOORID
 
Back
Top