• 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 script change tfs 1.3+++

Lbtg

Intermediate OT User
Joined
Nov 22, 2008
Messages
2,312
Reaction score
135
hey hey! healthy day :)

want to ask help for reworking abit with this script below

i want that on change of item, it picks 1 random item from list, lets say something like this:
[46926] = { transformTo = 41144, or transformTo = 41145, or transformTo = 41146, or transformTo = 41146, or },

so random gift given, + if for every item can set a chance% rate how easy or hard its getaible,


Lua:
local config = {
   [10503] = { transformTo = 45154 },
}
local action = Action()
function action.onUse(player, item, fromPos, target, toPos, isHotkey)
   if target then
      if target:isItem() then
         local it = config[target:getId()]
         if it then
            --player:sendSuccess(string.format("You transformed the item %s to %s.", getItemName(it), getItemName(it.transformTo)))
            target:remove(1)
            item:remove(1)
            player:getPosition():sendMagicEffect(932)
            player:addItem(it.transformTo, 1)
         else
            player:sendFail("You can only use this key on stat gem present box.")
            return false
         end
      end
   end
   return true
end

action:id(31133)
action:register()
 
Solution
I improved the script, cleaner syntax!!! and moved the ID to the config table... easier to work with.

Lua:
local config = {
    itemId = 31133, -- Item ID you will use
    
    [10503] = {
        transformations = { -- chances are 'cumulative', they add together..
            { itemId = 45154, minammount = 1, maxammount = 10, chance = 50 }, -- 50% chance to transform into item 45154 (1 to 10 items)
            { itemId = 45155, minammount = 1, maxammount = 20, chance = 30 }, -- 50+30% -> 80% chance to transform into item 45155 (1 to 20 items)
            { itemId = 45156, minammount = 1, maxammount = 1, chance = 20 }   -- 50+30+20% -> 100% chance to transform into item 45156 (1 item)
        }
    },
}

local action = Action()

function...
Lua:
local config = {
   [10503] = {
      transformations = {
         { itemId = 45154, chance = 50 },
         { itemId = 45155, chance = 30 },
         { itemId = 45156, chance = 20 },
      }
}

local action = Action()

function action.onUse(player, item, fromPos, target, toPos, isHotkey)
   if target and target:isItem() then
      local itemId = target:getId()
      local transformations = config[itemId]
      
      if transformations then
         local totalChance = 0
         for _, transformation in ipairs(transformations.transformations) do
            totalChance = totalChance + transformation.chance
         end

         local randomNumber = math.random(1, totalChance)
         local cumulativeChance = 0

         for _, transformation in ipairs(transformations.transformations) do
            cumulativeChance = cumulativeChance + transformation.chance
            if randomNumber <= cumulativeChance then
               item:remove(1)
               target:remove(1)
               player:getPosition():sendMagicEffect(932)
               player:addItem(transformation.itemId, 1)
               return true
            end
         end
      end

      player:sendFail("Transformation failed.")
      return false
   end

   player:sendCancelMessage("Sorry not possibruu.")
   return false
end

action:id(31133)
action:register()
 
Lua:
local config = {
   [10503] = {
      transformations = {
         { itemId = 45154, chance = 50 },
         { itemId = 45155, chance = 30 },
         { itemId = 45156, chance = 20 },
      }
}

local action = Action()

function action.onUse(player, item, fromPos, target, toPos, isHotkey)
   if target and target:isItem() then
      local itemId = target:getId()
      local transformations = config[itemId]
    
      if transformations then
         local totalChance = 0
         for _, transformation in ipairs(transformations.transformations) do
            totalChance = totalChance + transformation.chance
         end

         local randomNumber = math.random(1, totalChance)
         local cumulativeChance = 0

         for _, transformation in ipairs(transformations.transformations) do
            cumulativeChance = cumulativeChance + transformation.chance
            if randomNumber <= cumulativeChance then
               item:remove(1)
               target:remove(1)
               player:getPosition():sendMagicEffect(932)
               player:addItem(transformation.itemId, 1)
               return true
            end
         end
      end

      player:sendFail("Transformation failed.")
      return false
   end

   player:sendCancelMessage("Sorry not possibruu.")
   return false
end

action:id(31133)
action:register()
error appearing, doesnt work
Post automatically merged:

Lua:
03-19 12:29:57 -  > key open stat gem.lua [error]
2024-03-19 12:29:57 -  ^ /data/scripts/actions/source/key open stat gem.lua:10: '}' expected (to close '{' at line 1) near 'local'
 
I improved the script, cleaner syntax!!! and moved the ID to the config table... easier to work with.

Lua:
local config = {
    itemId = 31133, -- Item ID you will use
    
    [10503] = {
        transformations = { -- chances are 'cumulative', they add together..
            { itemId = 45154, minammount = 1, maxammount = 10, chance = 50 }, -- 50% chance to transform into item 45154 (1 to 10 items)
            { itemId = 45155, minammount = 1, maxammount = 20, chance = 30 }, -- 50+30% -> 80% chance to transform into item 45155 (1 to 20 items)
            { itemId = 45156, minammount = 1, maxammount = 1, chance = 20 }   -- 50+30+20% -> 100% chance to transform into item 45156 (1 item)
        }
    },
}

local action = Action()

function action.onUse(player, item, fromPos, target, toPos, isHotkey)
    if not target or not target:isItem() then
        player:sendCancelMessage("Sorry, not possible.")
        return true
    end
    
    local targetId = target:getId()
    local transformationConfig = config[targetId]
    
    if not transformationConfig then
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You can only use this on a stat gem present box.")
        return true
    end
    
    local randomChance = math.random(100)
    local cumulativeChance = 0
    
    for _, transformation in ipairs(transformationConfig.transformations) do
        cumulativeChance = cumulativeChance + transformation.chance
    
        if randomChance <= cumulativeChance then
            item:remove(1) 
            target:remove(1)
            player:getPosition():sendMagicEffect(932)
    
            local itemCount = math.random(transformation.minammount, transformation.maxammount)
            player:addItem(transformation.itemId, itemCount)
    
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Transformation successful.")
            return true
        end
    end
    
    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Transformation failed.")
    return true
end

action:id(config.itemId)
action:register()
 
Last edited:
Solution
I improved the script, cleaner syntax!!! and moved the ID to the config table... easier to work with.

Lua:
local config = {
    itemId = 31133, -- Item ID you will use

    [10503] = {
        transformations = {
            { itemId = 45154, chance = 50 }, -- 50% chance to transform into item 2400
            { itemId = 45155, chance = 30 }, -- 30% chance to transform into item 2401
            { itemId = 45156, chance = 20 }, -- 20% chance to transform into item 2402
        },
    },
}

local action = Action()

function action.onUse(player, item, fromPos, target, toPos, isHotkey)
    if target and target:isItem() then
        local targetId = target:getId()
        local transformationConfig = config[targetId]

        if transformationConfig then
            local randomChance = math.random(100)
            local cumulativeChance = 0

            for _, transformation in ipairs(transformationConfig.transformations) do
                cumulativeChance = cumulativeChance + transformation.chance
                if randomChance <= cumulativeChance then
                    item:remove(1)
                    target:remove(1)
                    player:getPosition():sendMagicEffect(932)
                    player:addItem(transformation.itemId, 1)
                    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Transformation successful.")
                    return true
                end
            end
        end

        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Transformation failed.")
        return false
    else
        player:sendCancelMessage("Sorry, not possible.")
        return false
    end
end

action:id(config.itemId)
action:register()
lol i just thinked about you hahaha
let me test
Post automatically merged:

I improved the script, cleaner syntax!!! and moved the ID to the config table... easier to work with.

Lua:
local config = {
    itemId = 31133, -- Item ID you will use

    [10503] = {
        transformations = {
            { itemId = 45154, chance = 50 }, -- 50% chance to transform into item 45154
            { itemId = 45155, chance = 30 }, -- 30% chance to transform into item 45155
            { itemId = 45156, chance = 20 }, -- 20% chance to transform into item 45156
        },
    },
}

local action = Action()

function action.onUse(player, item, fromPos, target, toPos, isHotkey)
    if target and target:isItem() then
        local targetId = target:getId()
        local transformationConfig = config[targetId]

        if transformationConfig then
            local randomChance = math.random(100)
            local cumulativeChance = 0

            for _, transformation in ipairs(transformationConfig.transformations) do
                cumulativeChance = cumulativeChance + transformation.chance
                if randomChance <= cumulativeChance then
                    item:remove(1)
                    target:remove(1)
                    player:getPosition():sendMagicEffect(932)
                    player:addItem(transformation.itemId, 1)
                    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Transformation successful.")
                    return true
                end
            end
        end

        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Transformation failed.")
        return false
    else
        player:sendCancelMessage("Sorry, not possible.")
        return false
    end
end

action:id(config.itemId)
action:register()
seems working perfect, but what about if i want set 2160-50x ?
 
I still don't understand. Could you explain a bit more to help me understand better? What do you mean by wanting to add 2160 - 50 count? Could you clarify?
example


Lua:
        transformations = {
            { itemId = 45154, minammount = 1, maxammount = 10, chance = 50 }, -- 50% chance to transform into item 45154
            { itemId = 45155, minammount = 1, maxammount = 20, chance = 30 }, -- 30% chance to transform into item 45155
            { itemId = 45156, minammount = 1, maxammount = 1, chance = 20 }, -- 20% chance to transform into item 45156
        },
    },
}
 
I improved the script, cleaner syntax!!! and moved the ID to the config table... easier to work with.

Lua:
local config = {
    itemId = 31133, -- Item ID you will use

    [10503] = {
        transformations = {
            { itemId = 45154, minammount = 1, maxammount = 10, chance = 50 }, -- 50% chance to transform into item 45154 (1 to 10 items)
            { itemId = 45155, minammount = 1, maxammount = 20, chance = 30 }, -- 30% chance to transform into item 45155 (1 to 20 items)
            { itemId = 45156, minammount = 1, maxammount = 1, chance = 20 }, -- 20% chance to transform into item 45156 (1 item)
        },
    },
}

local action = Action()

function action.onUse(player, item, fromPos, target, toPos, isHotkey)
    if target and target:isItem() then
        local targetId = target:getId()

        local transformationConfig = config[targetId]
        if transformationConfig then

            local randomChance = math.random(100)

            local cumulativeChance = 0

            for _, transformation in ipairs(transformationConfig.transformations) do
                cumulativeChance = cumulativeChance + transformation.chance

                if randomChance <= cumulativeChance then
                    item:remove(1) 
                    target:remove(1)
                    player:getPosition():sendMagicEffect(932)

                    local itemCount = math.random(transformation.minammount, transformation.maxammount)
                    player:addItem(transformation.itemId, itemCount)

                    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Transformation successful.")
                    return true
                end
            end
        else
        end

        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Transformation failed.")
        return false
    else
        player:sendCancelMessage("Sorry, not possible.")
        return false
    end
end

action:id(config.itemId)
action:register()

Didn't change anything about the script, but further improved the 'clean syntax', as an example.
Figured you might appreciate it.

@Lbtg Just so you are aware, at some point a 'cumulative' chance was added into the script..
So the chances of each item are being added together.
I updated the green text to try to highlight this for you.

Lua:
local config = {
	itemId = 31133, -- Item ID you will use
	
	[10503] = {
		transformations = { -- chances are 'cumulative', they add together..
			{ itemId = 45154, minammount = 1, maxammount = 10, chance = 50 }, -- 50% chance to transform into item 45154 (1 to 10 items)
			{ itemId = 45155, minammount = 1, maxammount = 20, chance = 30 }, -- 50+30% -> 80% chance to transform into item 45155 (1 to 20 items)
			{ itemId = 45156, minammount = 1, maxammount = 1, chance = 20 }   -- 50+30+20% -> 100% chance to transform into item 45156 (1 item)
		}
	},
}

local action = Action()

function action.onUse(player, item, fromPos, target, toPos, isHotkey)
	if not target or not target:isItem() then
		player:sendCancelMessage("Sorry, not possible.")
		return true
	end
	
	local targetId = target:getId()
	local transformationConfig = config[targetId]
	
	if not transformationConfig then
		player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You can only use this on a stat gem present box.")
		return true
	end
	
	local randomChance = math.random(100)
	local cumulativeChance = 0
	
	for _, transformation in ipairs(transformationConfig.transformations) do
		cumulativeChance = cumulativeChance + transformation.chance
	
		if randomChance <= cumulativeChance then
			item:remove(1) 
			target:remove(1)
			player:getPosition():sendMagicEffect(932)
	
			local itemCount = math.random(transformation.minammount, transformation.maxammount)
			player:addItem(transformation.itemId, itemCount)
	
			player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Transformation successful.")
			return true
		end
	end
	
	player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Transformation failed.")
	return true
end

action:id(config.itemId)
action:register()
 
Didn't change anything about the script, but further improved the 'clean syntax', as an example.
Figured you might appreciate it.

@Lbtg Just so you are aware, at some point a 'cumulative' chance was added into the script..
So the chances of each item are being added together.
I updated the green text to try to highlight this for you.

Lua:
local config = {
    itemId = 31133, -- Item ID you will use
   
    [10503] = {
        transformations = { -- chances are 'cumulative', they add together..
            { itemId = 45154, minammount = 1, maxammount = 10, chance = 50 }, -- 50% chance to transform into item 45154 (1 to 10 items)
            { itemId = 45155, minammount = 1, maxammount = 20, chance = 30 }, -- 50+30% -> 80% chance to transform into item 45155 (1 to 20 items)
            { itemId = 45156, minammount = 1, maxammount = 1, chance = 20 }   -- 50+30+20% -> 100% chance to transform into item 45156 (1 item)
        }
    },
}

local action = Action()

function action.onUse(player, item, fromPos, target, toPos, isHotkey)
    if not target or not target:isItem() then
        player:sendCancelMessage("Sorry, not possible.")
        return true
    end
   
    local targetId = target:getId()
    local transformationConfig = config[targetId]
   
    if not transformationConfig then
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You can only use this on a stat gem present box.")
        return true
    end
   
    local randomChance = math.random(100)
    local cumulativeChance = 0
   
    for _, transformation in ipairs(transformationConfig.transformations) do
        cumulativeChance = cumulativeChance + transformation.chance
   
        if randomChance <= cumulativeChance then
            item:remove(1)
            target:remove(1)
            player:getPosition():sendMagicEffect(932)
   
            local itemCount = math.random(transformation.minammount, transformation.maxammount)
            player:addItem(transformation.itemId, itemCount)
   
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Transformation successful.")
            return true
        end
    end
   
    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Transformation failed.")
    return true
end

action:id(config.itemId)
action:register()
hmm, bad syntax randomly can cause a crash ? or they do just ram leech ?

Also why 50+30+20%, i would like for every item just to be different chance% for each item, cuz if i have 100+ items here, or even 10+ it would be a realy confusing to config all.

i just want that some items are easier to get, and some harder, so just for every time setingthe chance% for it
 
Back
Top