• 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 Chance system

Fire Element

Member
Joined
Sep 10, 2010
Messages
193
Reaction score
22
Location
Brazil
I wanted to put a chance this script:

Lua:
function onUse(cid, item, frompos, item2, topos)

	local n = math.random(10, 100)
	doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You received "..n.."0k.")
        doPlayerAddItem(cid, 2160, n)
	doSendMagicEffect(getPlayerPosition(cid), CONST_ME_MAGIC_GREEN)
	doRemoveItem(item.uid, 1)
	return true
end

Example:

90% chance = 10.000 ~~ 30.000gps
80% chance = 35.000 ~~ 50.000gps
etc...

Thanks!
 
Lua:
local rand = math.random(1,100)
if(rand == 10) then--1% chance
---------------------------------
or:
if(rand >= 50) then--50% chance.. etc u get the thing...
 
Best way to do this is using a 'table' and 'loops'.

Lua:
local t = {
	[1, 10] = {, },
	[11, 20] = {, },
	[21, 30] = {, },
	[31, 40] = {, },
	[41, 50] = {, },
	[51, 60] = {, },
	[61, 70] = {, },
	[71, 80] = {, },
	[81, 90] = {, },
	[91, 100] = {, }
}

function returnPercentageValue()
	local r = math.random(1,100)
	for i, k in pairs(t) do
		if r >= i[1] and r <= i[2] then
			return math.random(k[1],k[2])
		end
	end
end

Edit:
I added it to your script, just in case you had problems doing it yourself:
Lua:
local t = {
	[1, 10] = {, },
	[11, 20] = {, },
	[21, 30] = {, },
	[31, 40] = {, },
	[41, 50] = {, },
	[51, 60] = {, },
	[61, 70] = {, },
	[71, 80] = {, },
	[81, 90] = {, },
	[91, 100] = {, }
}

function returnPercentageValue()
	local r = math.random(1,100)
	for i, k in pairs(t) do
		if r >= i[1] and r <= i[2] then
			return math.random(k[1],k[2])
		end
	end
end	
function onUse(cid, item, frompos, item2, topos)
 
	local n = returnPercentageValue()
	doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You received "..n.."0k.")
    doPlayerAddItem(cid, 2160, n)
	doSendMagicEffect(getPlayerPosition(cid), CONST_ME_MAGIC_GREEN)
	doRemoveItem(item.uid, 1)
	return true
end
Be sure to add the amount range in the empty parts in the table:
Example:
Lua:
[1, 10] = {1, 5},
 
Last edited:
Error:

gp.lua:2: ´]´ expected near ´,´

Lua:
local t = {
	[1, 10] = {81, 100},
	[11, 20] = {60, 80},
	[21, 30] = {50, 54},
	[31, 40] = {40, 45},
	[41, 50] = {26, 35},
	[51, 60] = {20, 25},
	[61, 70] = {16, 18},
	[71, 80] = {11, 15},
	[81, 90] = {6, 10},
	[91, 100] = {1, 5}
}
 
function returnPercentageValue()
	local r = math.random(1,100)
	for i, k in pairs(t) do
		if r >= i[1] and r <= i[2] then
			return math.random(k[1],k[2])
		end
	end
end	
function onUse(cid, item, frompos, item2, topos)
 
	local n = returnPercentageValue()
	doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You received "..n.."0k.")
    doPlayerAddItem(cid, 2160, n)
	doSendMagicEffect(getPlayerPosition(cid), CONST_ME_MAGIC_GREEN)
	doRemoveItem(item.uid, 1)
	return true
end
 
Lua:
local t = {
	[{1, 10}] = {81, 100},
	[{11, 20}] = {60, 80},
	[{21, 30}] = {50, 54},
	[{31, 40}] = {40, 45},
	[{41, 50}] = {26, 35},
	[{51, 60}] = {20, 25},
	[{61, 70}] = {16, 18},
	[{71, 80}] = {11, 15},
	[{81, 90}] = {6, 10},
	[{91, 100}] = {1, 5}
}
 
function returnPercentageValue()
	local r = math.random(1,100)
	for i, k in pairs(t) do
		if r >= i[1] and r <= i[2] then
			return math.random(k[1],k[2])
		end
	end
end

function onUse(cid, item, frompos, item2, topos)
	local n = returnPercentageValue()
	
	doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You received " .. n .. "0k.")
	doPlayerAddItem(cid, 2160, n)
	doSendMagicEffect(getPlayerPosition(cid), CONST_ME_MAGIC_GREEN)
	doRemoveItem(item.uid, 1)
	return true
end
 
Back
Top