• 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 Easy script with random gives allways same return

Fermantor

Active Member
Joined
Dec 16, 2009
Messages
209
Solutions
4
Reaction score
34
Location
Germany
Here is my script:
LUA:
local rewardItems = {
{chances = 1435, item = 2238, amount = 1}, -- Worn Leather Boots
{chances = 2864, item = 2237, amount = 1}, -- Dirty Cape 
{chances = 4275, item = 2115, amount = 1}, -- Broken Piggy Bank 
{chances = 4789, item = 2695, amount = 1}, -- Egg
{chances = 5297, item = 2148, amount = 6}, -- 6 Gold Coins
{chances = 5764, item = 5890, amount = 1}, -- Chicken Feather
{chances = 6189, item = 5894, amount = 1}, -- Bat Wing
{chances = 6590, item = 2787, amount = 1}, -- White Mushroom
{chances = 6961, item = 10606, amount = 1}, -- Bunch of Troll Hair
{chances = 7320, item = 3976, amount = 4}, -- 4 Worms
{chances = 7673, item = 5899, amount = 1}, -- Turtle Shell
{chances = 8008, item = 5902, amount = 1}, -- Honeycomb 
{chances = 8325, item = 2403, amount = 1}, -- Knife
{chances = 8648, item = 8859, amount = 1}, -- Spider Fangs
{chances = 8792, item = 2661, amount = 1}, -- Scarf
{chances = 8900, item = 2199, amount = 1}, -- Garlic Necklace
{chances = 8978, item = 13926, amount = 1}, -- Plague Bell 
{chances = 9050, item = 2143, amount = 1}, -- White Pearl
{chances = 9122, item = 5879, amount = 1}, -- Spider Silk
{chances = 9188, item = 2114, amount = 1}, -- Piggy Bank
{chances = 9224, item = 2110, amount = 1}, -- Doll
{chances = 9260, item = 10563, amount = 1}, -- Book of Prayers
{chances = 9272, item = 5880, amount = 1}, -- Iron Ore
{chances = 9326, item = 13925, amount = 1}, -- Plague Mask
{chances = 9350, item = 5895, amount = 1}, -- Fish Fin
{chances = 9392, item = 13508, amount = 1}, -- Slug Drug
{chances = 9398, item = 2195, amount = 1} -- Boots of Haste
}

function onUse(cid, item, fromPos, item2, toPos)
	doSendMagicEffect(fromPos, CONST_ME_POFF)
	local Random = math.random(1,10000)
	--doCreatureSay(cid, Random, TALKTYPE_ORANGE_1)
	for k = 1, #rewardItems do
		if rewardItems[k].chances <= Random then
			doTransformItem(item.uid, rewardItems[k].item, rewardItems[k].amount)
			return TRUE
		end
	end
	doRemoveItem(item.uid)
	return TRUE
end
The problem is, this script transformes the item always in the first item, of the list.
I don't think I am a beginner in scripting, but I tried everything and it won't work -.-
 
well rather do the chance in % and throw the items with the same percentage chance into one table
example:
LUA:
local reward =
{
    {chance = 15, {{222,1},{3333,2},{4444,3}}},
    {chance = 30, {{5555,1},{6666,7}}}
}
    if reward[k].chance <= math.random(1,100) then
        local rnd = math.random(1, #reward[k][2])
        doTransformItem(item.uid, reward[k][2][rnd][1], reward[k][2][rnd][2])
just a rough example on how you could merge it with yours.
 
Evil Hero's script isn't good. Very bad optimization. For e.g. he have 2 items, 15% chance and 30%, the problem is - practicly both of them have exactly the same chance.
First item 15% chance, and second one 30% - 15%. So, both 15%.

Here is my script, optimized. Works perfectly.
You have to edit it a bit.

LUA:
local config = {
	[1] = {item = 8982, level = 70, chance = 3},
	[2] = {item = 8985, level = 70, chance = 1},
	[3] = {item = 9006, level = 30, chance = 450},
	[4] = {item = 9019, level = 70, chance = 3},
	[5] = {item = 9662, level = 65, chance = 8},
	[6] = {item = 1681, level = 1, chance = 10000},
	[7] = {item = 2110, level = 20, chance = 1200},
}

function onUse(cid, item, fromPosition, itemEx, toPosition)
	local chanceMax = 0
	local now = {}
	for r = 1, #config do
		if chanceMax < config[r].chance then
			chanceMax = config[r].chance
		end
	end
	table.sort(config, function(x, y) return x.chance < y.chance end)
	for i = 1, #config do
		if getPlayerLevel(cid) >= config[i].level then
			if (config[i].chance >= math.random(1, chanceMax)) then
				now = config[i]
				break
			end
		end
	end

	doPlayerAddItem(cid, now.item, 1)
	return true
end
 
Evil Hero's script isn't good. Very bad optimization. For e.g. he have 2 items, 15% chance and 30%, the problem is - practicly both of them have exactly the same chance.
First item 15% chance, and second one 30% - 15%. So, both 15%.

Here is my script, optimized. Works perfectly.
You have to edit it a bit.

LUA:
local config = {
	[1] = {item = 8982, level = 70, chance = 3},
	[2] = {item = 8985, level = 70, chance = 1},
	[3] = {item = 9006, level = 30, chance = 450},
	[4] = {item = 9019, level = 70, chance = 3},
	[5] = {item = 9662, level = 65, chance = 8},
	[6] = {item = 1681, level = 1, chance = 10000},
	[7] = {item = 2110, level = 20, chance = 1200},
}

function onUse(cid, item, fromPosition, itemEx, toPosition)
	local chanceMax = 0
	local now = {}
	for r = 1, #config do
		if chanceMax < config[r].chance then
			chanceMax = config[r].chance
		end
	end
	table.sort(config, function(x, y) return x.chance < y.chance end)
	for i = 1, #config do
		if getPlayerLevel(cid) >= config[i].level then
			if (config[i].chance >= math.random(1, chanceMax)) then
				now = config[i]
				break
			end
		end
	end

	doPlayerAddItem(cid, now.item, 1)
	return true
end
If you can't even properly read a code of others then I'd suggest to do not claim things which are not true at all, I clearly stated that he could merge my parts into his, which makes perfect sense.
Looking at your code just tells me what "optimized" means for you, using tables which you could replace with a single statement.
 
Back
Top