• 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!
  • 2026 staff recruitment is open! Check it out and consider applying!

[Lua] Use X item and get one of XXX random items | REP

Tastertur

New Member
Joined
Feb 26, 2009
Messages
149
Reaction score
4
Hello,

*I used the search function but didn't found such script*
Player use X item and get one of XXX random items, it should have a % function. Would be great if someone could do that.

Thanks in advance
 
Code:
local items = {{1111,20},{2222,50},{3333,30}}
-- {itemid, chance}
local total = 0
for i,v in pairs(items) do
items[i][3] = total
total = total + v[2]
end
function onUse(cid, item, fromPosition, itemEx, toPosition)
local item = math.random(0,total)
for _,v in pairs(items) do
if (item >= v[3] and item < (v[2]+v[3])) then
doPlayerAddItem(cid,v[1],1)
break
end
end
doRemoveItem(item.uid)
return true
end
 
its working great but is it possible to remove the item that has been used?

PHP:
function onUse(cid, item, fromPosition, itemEx, toPosition)
		doSendMagicEffect(getCreaturePosition(cid), 12)
                doRemoveItem(item.uid, 1)
return true
end

smt like that?




edit: ur script

[17:57:16.013] [Error - Action Interface]
[17:57:16.013] data/actions/scripts/other/randomitem.lua:onUse
[17:57:16.013] Description:
[17:57:16.013] data/actions/scripts/other/randomitem.lua:16: attempt to index local 'item' (a number value)
[17:57:16.014] stack traceback:
[17:57:16.014] data/actions/scripts/other/randomitem.lua:16: in function <data/actions/scripts/other/randomitem.lua:8>
 
Last edited:
I overwrote the item variabele by mistake so removing it broke down. Fixed version below:

Code:
local items = {{1111,20},{2222,50},{3333,30}}
-- {itemid, chance}
local total = 0
for i,v in pairs(items) do
items[i][3] = total
total = total + v[2]
end
function onUse(cid, oitem, fromPosition, itemEx, toPosition)
local item = math.random(0,total)
for _,v in pairs(items) do
if (item >= v[3] and item < (v[2]+v[3])) then
doPlayerAddItem(cid,v[1],1)
break
end
end
doRemoveItem(oitem.uid)
return true
end
 
is it possible to add "local count" for stack items ...?


(example chicken feather) and a normal item (soul stone)


for countable items {2060,10,50}
-- itemid, count, chance (%)

*count: It should be random from 1-10
*chance: from 1-100%

normal items {2060,50}
-- itemid, chance (%)

*chance: from 1-100%


can someone help me please?
thanks in advance
 
Last edited:
Code:
local items = {{1111,5,20},{2222,1,50},{3333,100,30}}
-- {itemid, count, chance}
local total = 0
for i,v in pairs(items) do
items[i][4] = total
total = total + v[3]
end
function onUse(cid, oitem, fromPosition, itemEx, toPosition)
local item = math.random(0,total)
for _,v in pairs(items) do
if (item >= v[4] and item < (v[3]+v[4])) then
doPlayerAddItem(cid,v[1],v[2])
break
end
end
doRemoveItem(oitem.uid)
return true
end
 
Code:
local items = {{1111,5,20},{2222,1,50},{3333,100,30}}
-- {itemid, count, chance}
local total = 0
for i,v in pairs(items) do
items[i][4] = total
total = total + v[3]
end
function onUse(cid, oitem, fromPosition, itemEx, toPosition)
local item = math.random(0,total)
for _,v in pairs(items) do
if (item >= v[4] and item < (v[3]+v[4])) then
doPlayerAddItem(cid,v[1],v[2])
break
end
end
doRemoveItem(oitem.uid)
return true
end

You should try tabbing :p
 
I do, for actual server scripting.
But why should I open notepad if I can do without it? To please you?

Uhm, I was just suggesting you to tab it, there's no need to be "rude".
It looks way cleaner if you actually use tabs; correct me if I am wrong but, the script you made is for a server, hence it's "server scripting".
Now, sorry for the off-topic posts, this is my last post in this thread, if you really feel the need to discuss more about this, use the private message function or post on my wall.
 
Back
Top