• 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 Hammer Script Won't Return Any Items

Call Me Taffy

Call Me Maybe
Joined
Aug 9, 2009
Messages
656
Reaction score
125
So I want to be able to use my hammer on a variety of different items to give the chance on a random item & amount associated. It's a quick rusty remover edit, but not returning any items at all, the hammer just disappears.

local breakChance = 5

local hammerItems = {
[5892] = {{5880, 4}, {5880, 3}, {5880, 2}, {5880, 1}, {5889, 3}, {5889, 2}, {5889, 1}, {5887, 2}, {5889, 1}, {5887, 1}},
[4850] = {{6541, 25}, {6542, 25}, {6543, 25}, {6544, 25}, {6545, 25}, {6541, 5}, {6542, 5}, {6543, 5}, {6544, 5}, {6545, 5}, {6541, 15}, {6542, 15}, {6543, 15}, {6544, 15}, {6545, 15}},
[2348] = {{2143, 25}, {2144, 25}, {2143, 15}, {2144, 15}, {2143, 5}, {2144, 5}},
[2153] = {{2150, 5}, {2150, 10}, {2150, 15}},
[2154] = {{9970, 5}, {9970, 10}, {9970, 15}},
[2155] = {{2149, 5}, {2149, 10}, {2149, 15}},
[2156] = {{2147, 5}, {2147, 10}, {2147, 15}},
[2158] = {{2146, 5}, {2146, 10}, {2146, 15}}
}


function onUse(cid, item, fromPosition, itemEx, toPosition)

if hammerItems[itemEx.itemid] ~= nil then

if math.random(100) <= breakChance then
doRemoveItem(itemEx.uid)
doPlayerSendTextMessage(cid,MESSAGE_INFO_DESCR,"You shattered it.")
doSendMagicEffect(getCreaturePosition(cid),34)
else
local newId = hammerItems[itemEx.itemid][math.random(#hammerItems[itemEx.itemid])]
doTransformItem(itemEx.uid,newId)
doPlayerSendTextMessage(cid,MESSAGE_INFO_DESCR,"SMASH! Got a "..getItemName(newId))
doSendMagicEffect(getCreaturePosition(cid),66)
end

doRemoveItem(item.uid,1)
return true
end

return false
end
 
Your math.random functions (i count 2 in this script) don't have 2 arguments.
They should be like math.random(0,100) for a random interval between 0 and 100, for example.

In your script there's math.random(100) and math.random(#hammerItems[itemEx.itemid]).
Try changing those by adding proper intervals and that should do the trick.
 
Your math.random functions (i count 2 in this script) don't have 2 arguments.
They should be like math.random(0,100) for a random interval between 0 and 100, for example.

In your script there's math.random(100) and math.random(#hammerItems[itemEx.itemid]).
Try changing those by adding proper intervals and that should do the trick.
http://lua-users.org/wiki/MathLibraryTutorial

math.random(upper) generates integer numbers between 1 and upper.
 
http://lua-users.org/wiki/MathLibraryTutorial

math.random(upper) generates integer numbers between 1 and upper.

Oh, nice.

200_s.gif
 
See if this works out
Code:
    local breakChance = 5

    local hammerItems = {
        [5892] = {{5880, 4}, {5880, 3}, {5880, 2}, {5880, 1}, {5889, 3}, {5889, 2}, {5889, 1}, {5887, 2}, {5889, 1}, {5887, 1}},
        [4850] = {{6541, 25}, {6542, 25}, {6543, 25}, {6544, 25}, {6545, 25}, {6541, 5}, {6542, 5}, {6543, 5}, {6544, 5}, {6545, 5}, {6541, 15}, {6542, 15}, {6543, 15}, {6544, 15}, {6545, 15}},
        [2348] = {{2143, 25}, {2144, 25}, {2143, 15}, {2144, 15}, {2143, 5}, {2144, 5}},
        [2153] = {{2150, 5}, {2150, 10}, {2150, 15}},
        [2154] = {{9970, 5}, {9970, 10}, {9970, 15}},
        [2155] = {{2149, 5}, {2149, 10}, {2149, 15}},
        [2156] = {{2147, 5}, {2147, 10}, {2147, 15}},
        [2158] = {{2146, 5}, {2146, 10}, {2146, 15}}
    }


    function onUse(cid, item, fromPosition, itemEx, toPosition)
        local hammer = hammerItems[itemEx.itemid]
        local chance = math.floor( math.random(1, 100) )
        local msg = "this doesn't work at all"
        local e = 2 -- POFF
        if hammer ~= nil then
            if chance <= breakChance then
                doRemoveItem(itemEx.uid)
                msg = "You shattered it."
                e = 34
            else
                local newId = hammer[ math.random(1, #hammer ) ]
                doTransformItem(itemEx.uid,    newId)
                msg = "SMASH! Got a "..getItemName(newId)
                e = 66
            end
            doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, msg)
            doSendMagicEffect(getCreaturePosition(cid), e)
            doRemoveItem(item.uid, 1)
            return true
        end
        return false
    end
 
Thanks for the help guys.
Alright so my script currently looks like this, works, but now how to make it give more than 1?

Code:
local breakChance = 10

    local hammerItems = {
        [5892] = {5880, 5889, 5887, 5880, 5880, 5880, 5880,},
        [4850] = {6541, 6542, 6543, 6544, 6545},
        [2348] = {2143, 2144},
        [2153] = {2150},
        [2154] = {9970},
        [2155] = {2149},
        [2156] = {2147},
        [2158] = {2146}
    }


    function onUse(cid, item, fromPosition, itemEx, toPosition)
        local hammer = hammerItems[itemEx.itemid]
        local chance = math.floor( math.random(1, 100) )
        local msg = "this doesn't work at all"
        local e = 2 -- POFF
        if hammer ~= nil then
            if chance <= breakChance then
                doRemoveItem(itemEx.uid)
                msg = "You shattered it."
                e = 34
            else
                local newId = hammer[ math.random(1, #hammer ) ]
                doTransformItem(itemEx.uid,    newId)
                msg = "SMASH! Got a "..getItemName(newId)
                e = 66
            end
            doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, msg)
            doSendMagicEffect(getCreaturePosition(cid), e)
            doRemoveItem(item.uid, 1)
            return true
        end
        return false
    end
 
Using your old table, sorry i was sleepy when i 'fixed' the original script :p
Code:
    local breakChance = 10

    local hammerItems = {
        [5892] = {{5880, 4}, {5880, 3}, {5880, 2}, {5880, 1}, {5889, 3}, {5889, 2}, {5889, 1}, {5887, 2}, {5889, 1}, {5887, 1}},
        [4850] = {{6541, 25}, {6542, 25}, {6543, 25}, {6544, 25}, {6545, 25}, {6541, 5}, {6542, 5}, {6543, 5}, {6544, 5}, {6545, 5}, {6541, 15}, {6542, 15}, {6543, 15}, {6544, 15}, {6545, 15}},
        [2348] = {{2143, 25}, {2144, 25}, {2143, 15}, {2144, 15}, {2143, 5}, {2144, 5}},
        [2153] = {{2150, 5}, {2150, 10}, {2150, 15}},
        [2154] = {{9970, 5}, {9970, 10}, {9970, 15}},
        [2155] = {{2149, 5}, {2149, 10}, {2149, 15}},
        [2156] = {{2147, 5}, {2147, 10}, {2147, 15}},
        [2158] = {{2146, 5}, {2146, 10}, {2146, 15}}
    }


    function onUse(cid, item, fromPosition, itemEx, toPosition)
        local hammer = hammerItems[itemEx.itemid]
        local chance = math.floor( math.random(1, 100) )
        local msg = "this doesn't work at all"
        local e = 2 -- POFF
        if hammer ~= nil then
            if chance <= breakChance then
                msg = "You shattered it."
                e = 34
            else
                local newId = hammer[ math.random(1, #hammer ) ]
                -- newId[1] is the itemid to create, newId[2] is the item amount
                doCreateItemEx(newId[1], newId[2])
                msg = "SMASH! Look at what you've found "..(newId[2] < 2 and "a" or newId[2]).." "..getItemName(newId[1])..(newId[2] < 2 and '' or 's')
                e = 66
            end
            doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, msg)
            doSendMagicEffect(getCreaturePosition(cid), e)
            doRemoveItem(itemEx.uid, 1)
            doRemoveItem(item.uid, 1)
            return true
        end
        return false
    end

Using condition and execute1 or execute2 is short hand way of using an if / else statement.
Example:
Code:
local x
if newId[2] < 2 then
    x = "a"
else
    x = newId[2]
end
-- the above code is the same thing as the code below
x = newId[2] < 2 and "a" or newId[2]

Rather than create a new post, i'll explain further about the and, or and not logical operators :)
Code:
the and logical operator
in order for and logical operator to return true both conditions must return true
true and true -- returns true
true and false -- returns false
false and false -- returns false

the or logical operator
in order for the or logical operator to return true either condition must return true
true or true -- returns true
true or false -- returns true
false or true -- returns true
false or false -- returns false

the not logical operator
the not logical operator just returns the opposite of either true or false
not true -- returns false
not false -- returns true

For the most part I like teach a bit about the language to others where and when I can.
Its important to not just learn the tfs framework but also it's supporting language in order write effects scripts even if the scripts don't work right out of the box.
But hey that is why we are here to learn :)
 
Last edited:
@Codex NG : Bro I appreciate it haven't got to test it out yet. But I did bookmark your post lol, I've been studying horticulture in my free time even though some diff coding languages are also on the TODO list
 
Back
Top