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

Solved Click random item ?

psilocibe

Member
Joined
Jul 9, 2007
Messages
479
Reaction score
8
Hello guys,
I have an action where simply click the item to buy it. Here is the code.


Code:
local config = {
        cost = 25000,
        rune_id = 8472
}

function onUse(cid, item, frompos, fromPosition, itemEx, toPosition, topos)
        if doPlayerRemoveMoney(cid, config.cost) == TRUE then
                local name = getItemName(8472)
                local bp = doPlayerAddItem(cid,  config.rune_id, 100)
                doSendMagicEffect({x=380,y=343,z=7}, CONST_ME_STUN)
                        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_RED, "You buy 100x ".. name .."s por ".. config.cost .." golds.")
for i=1,1 do
                end
                else
                        doPlayerSendCancel(cid, "Voce nao tem ".. config.cost .." golds.")
                end
        return TRUE
end

How do I buy a random item by changing this code ?
 
not tested
Code:
local Cc = 25000
local prizes = {
    { item = 2160, count = 1 },
    { item = 2160, count = 100 },
    { item = 2160, count = 100 },
    { item = 2160, count = 100 },
    { item = 2160, count = 50 },
    { item = 2160, count = 100 },
    { item = 2160, count = 100 },
    { item = 2160, count = 1 },
    { item = 2160, count = 1 },
    { item = 2160, count = 1 },
    { item = 2160, count = 1 },
    { item = 2160, count = 1 },
    { item = 2257, count = 1 }

}

function onUse(cid, item, fromPosition, itemEx, toPosition)
if doPlayerRemoveMoney(cid, Cc) == TRUE then
local random = math.random(1, #prizes)
                doPlayerAddItem(cid, prizes[random].item, prizes[random].count)
                 doCreatureSay(cid, "You got " .. getItemNameById(prizes[random].item) .. " from the random lever!" ,19)
end
else
doPlayerSendTextMessage(cid,MESSAGE_INFO_DESCR, "You need 25 cc to use the lever!")
doSendMagicEffect(frompos,2)
end
return 1
end
 
delete the end above the else

Works, but every time I click gives this error.

Code:
Lua Script Error: [Action Interface]
data/actions/scripts/mainquests/randombp.lua:onUse
data/actions/scripts/mainquests/randombp.lua:23: attempt to call global 'getItemNameById' (a nil value)
stack traceback:
        [C]: in function 'getItemNameById'
        data/actions/scripts/mainquests/randombp.lua:23: in function <data/actions/scripts/mainquests/randombp.lua:19>
 
Add this function to your library :

Code:
function getItemNameById(itemid)
    return getItemDescriptionsById(itemid).name
end

Or you can also try to replace in your script:
this
Code:
doCreatureSay(cid, "You got " .. getItemNameById(prizes[random].item) .. " from the random lever!" ,19)
with
Code:
doCreatureSay(cid, "You got " .. getItemDescriptionsById(prizes[random].item).name .. " from the random lever!" ,19)
 
Add this function to your library :

Code:
function getItemNameById(itemid)
    return getItemDescriptionsById(itemid).name
end

Or you can also try to replace in your script:
this
Code:
doCreatureSay(cid, "You got " .. getItemNameById(prizes[random].item) .. " from the random lever!" ,19)
with
Code:
doCreatureSay(cid, "You got " .. getItemDescriptionsById(prizes[random].item).name .. " from the random lever!" ,19)


The error continues

Code:
Lua Script Error: [Action Interface]
data/actions/scripts/mainquests/randombp.lua:onUse
data/actions/scripts/mainquests/randombp.lua:23: attempt to call global 'getItemDescriptionsById' (a nil value)
stack traceback:
        [C]: in function 'getItemDescriptionsById'
        data/actions/scripts/mainquests/randombp.lua:23: in function <data/actions/scripts/mainquests/randombp.lua:19>

Where should I add the function? data / actions / lib / actions.lua?
I did this but the error continues
 
Code:
local Cc = 250000
local prizes = {
    { item = 2160, count = 1 },
    { item = 2157, count = 100},

}

function onUse(cid, item, fromPosition, itemEx, toPosition)
local player = Player(cid)

if player:removeMoney(Cc) then
local prize = math.random(1, #prizes)
                player:addItem(prizes[prize].item, prizes[prize].count)
                player:say("You won "..prizes[prize].count.." " .. ItemType(prizes[prize].item):getName() .. " from the random lever!" ,TALKTYPE_MONSTER_SAY)
else
player:sendTextMessage(MESSAGE_INFO_DESCR, "You need 25 cc to use the lever!")
fromPosition:sendMagicEffect(CONST_ME_POFF)
end
return true
end
 
Code:
function onUse(cid, item, frompos, item2, topos)
     if isPlayer(item2.uid) then
         local head, helmet = 'None', getPlayerSlotItem(item2.uid, CONST_SLOT_HEAD).itemid
         if helmet > 0 then
             head = getItemNameById(helmet)
         end
         doPlayerPopupFYI(cid, 'Head: '..head..' ')
     else
         doPlayerSendCancel(cid,"Error")
     end
     return true
end

He buys, not of error but the client falls crash

crash.jpg
 
Try with this, I just replaced TALKTYPE_MONSTER_SAY with TALKTYPE_ORANGE_1, you can change to whatever you know is the normal text you use for the orange words on your server.
Code:
local Cc = 250000
local prizes = {
    { item = 2160, count = 1 },
    { item = 2157, count = 100},

}

function onUse(cid, item, fromPosition, itemEx, toPosition)
local player = Player(cid)

if player:removeMoney(Cc) then
local prize = math.random(1, #prizes)
                player:addItem(prizes[prize].item, prizes[prize].count)
                player:say("You won "..prizes[prize].count.." " .. ItemType(prizes[prize].item):getName() .. " from the random lever!" ,TALKTYPE_ORANGE_1)
else
player:sendTextMessage(MESSAGE_INFO_DESCR, "You need 25 cc to use the lever!")
fromPosition:sendMagicEffect(CONST_ME_POFF)
end
return true
end
 

Similar threads

Replies
8
Views
1K
Evil Puncker
E
Back
Top