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

Random item when use item

kilirt

New Member
Joined
May 11, 2009
Messages
223
Reaction score
2
Hello,
i need a action script for when i use an item this give me a random item and do a broadcast message who said: The player "name" win a "name of item"

thx i :thumbup: rep
 
u meann like a
random = math.random(100,1000)
if random == 100 then
doPlayerAddItem(cid,itemOne,1)
elseif random == 200 then
doPlayerAddItem(cid,itemTwo,1)
...

something like that?
 
Why dont you use a lottery system?
Put it in your mods folder, it works fine for me tfs 0.3.6
XML:
<mod name="Lottery System" version="1.5" author="vDk" contact="[email protected]" enabled="yes">
	<config name="lottery_config"><![CDATA[
		config = {
			lottery_hour = "1 Hour", -- Time to next lottery (only for broadcast message, real time you can set on globalevents.xml)
			rewards_id = {8922, 8910, 2472, 2514, 2160}, -- Rewards ID
			crystal_counts = 10, -- Used only if on rewards_id is crystal coin (ID: 2160).
			website = "no" -- Only if you have php scripts and table `lottery` in your database!
		}
	]]></config>
	<globalevent name="lottery" interval="5000" event="script"><![CDATA[
		domodlib('lottery_config')
	function onThink(interval, lastExecution)
	if(getWorldCreatures(0) == 0)then
		return true
	end
		local list = {}
		for i, tid in ipairs(getPlayersOnline()) do
		list[i] = tid
	end
	
		local winner = list[math.random(1, #list)]
		local random_item = config.rewards_id[math.random(1, #config.rewards_id)]
	
	if(random_item == 2160) then
		doPlayerAddItem(winner, random_item, config.crystal_counts)
		doBroadcastMessage("[LOTTERY SYSTEM] Winner: " .. getCreatureName(winner) .. ", Reward: " .. config.crystal_counts .. " " .. getItemNameById(random_item) .. "s! Congratulations! (Next Lottery in " .. config.lottery_hour .. ")")
	else
		doBroadcastMessage("[LOTTERY SYSTEM] Winner: " .. getCreatureName(winner) .. ", Reward: " .. getItemNameById(random_item) .. "! Congratulations! (Next Lottery in " .. config.lottery_hour .. ")")
		doPlayerAddItem(winner, random_item, 1)
	end
	
	if(config.website == "yes") then
		db.executeQuery("INSERT INTO `lottery` (`name`, `item`) VALUES ('".. getCreatureName(winner) .."', '".. getItemNameById(random_item) .."');")
	end
	return true
	end 
	]]></globalevent>
</mod>

All credits for the script go to the original author(which u can see in the mod)
 
randomitem.lua:
LUA:
local ITEMS =
{
	[1] = {3971}, -- charmer tiara
	[2] = {3963}, -- templar scytheblade
	[3] = {2195} -- boots of haste
}

function onUse(cid, item, fromPosition, itemEx, toPosition)
	for k, v in pairs(ITEMS) do
		if(isInArray(k, math.random(100))) then
			local item = doCreateItemEx(cid, v, 1)
			if(doPlayerAddItemEx(cid, item) ~= RETURNVALUE_NOERROR) then
				doPlayerSendDefaultCancel(cid, RETURNVALUE_CONTAINERNOTENOUGHROOM)
			else
				doBroadcastMessage("The player " .. getCreatureName(cid) .. " won a " .. getItemInfo(k).name .. ".", MESSAGE_EVENT_ADVANCE)
			end
		end
	end

	return true
end

In actions.xml:
XML:
	<action [action, unique, item]id="XXXX" event="script" value="other/randomitem.lua"/>
 
Last edited:
Ohhhh, I didnt get it at first, now I get it.
Btw Ratser, you missed a string :l
LUA:
local ITEMS =
{
	[1] = {3971}, -- charmer tiara
	[2] = {3963}, -- templar scytheblade
	[3] = {2195} -- boots of haste
}
 
function onUse(cid, item, fromPosition, itemEx, toPosition)
	for k, v in pairs(ITEMS) do
		if(math.random(100) == k) then
			local backpack = doCreateItemEx(cid, v, 1)
			if(doPlayerAddItemEx(cid, backpack) ~= RETURNVALUE_NOERROR) then
				doPlayerSendDefaultCancel(cid, RETURNVALUE_CONTAINERNOTENOUGHROOM)
			else
				doPlayerBroadcastMessage(cid, "The player " .. getCreatureName(cid) .. " won a " .. getItemInfo(k).name .. " ")
			end
		end
	end
 
	return true
end

Im not sure it will work thouhg, I've tried using math.random like you and I got an error.
 
Why not, then how I'm supposed to check the value of math.random? :p

Thats exactly why I hate this, for example, you cant put formulas at locals either.
When I tried to make my casino like that, it gave me that freakin error, I was so mad:mad:
But he can try anyway, and we'll see later :p
 
If it doesn't work, let's try something different...

randomitem.lua:
LUA:
local ITEMS = {3971, 3963, 2195}

function onUse(cid, item, fromPosition, itemEx, toPosition)
	local random = ITEMS[math.random(1, #ITEMS)]
	if(doPlayerAddItem(cid, random, 1) ~= RETURNVALUE_NOERROR) then
		doPlayerSendDefaultCancel(cid, RETURNVALUE_CONTAINERNOTENOUGHROOM)
	else
		doBroadcastMessage("The player " .. getCreatureName(cid) .. " won a " .. getItemInfo(random).name .. ".", MESSAGE_EVENT_ADVANCE)
		doRemoveItem(item.uid, 1)
	end

	return true
end
 
Last edited:
i've tested all and no work :/ and no sorry no loterry system i want just when we click on a item the item give you a random item and send a broadcast message
 
err no is good work ^.^ is just an error of me but is ilimited how can i do when i use the item deleted too?


ps: sorry double post
 
Add
LUA:
doRemoveItem(item.uid, 1)
after
LUA:
doBroadcastMessage("The player " .. getCreatureName(cid) .. " won a " .. getItemInfo(random).name .. ".", MESSAGE_EVENT_ADVANCE)
 
Edit:

[19/12/2010 01:52:28] [Error - Action Interface]
[19/12/2010 01:52:28] data/actions/scripts/randomitem.lua:onUse
[19/12/2010 01:52:28] Description:
[19/12/2010 01:52:28] data/actions/scripts/randomitem.lua:6: attempt to index local 'item' (a number value)
[19/12/2010 01:52:28] stack traceback:
[19/12/2010 01:52:28] data/actions/scripts/randomitem.lua:6: in function <data/actions/scripts/randomitem.lua:3>

:S
 
test this out n tell me if it fails lol.
if it does fail, send the error.

Code:
--Daniel Couillard: Blood BlvD Ot.
function onUse(cid, item, frompos, item2, topos)
---Config---
--ADD ITEM IDS HERE--
local itemOneID =
local itemTwoID =
local itemThreeID =
local itemFourID =
local itemFiveID =
local rand = math.random(1,5)
--/Config---

	if item.itemid == item.itemid then
		if rand == 1 then 
			doPlayerAddItem(cid,itemOneID,1)
			doBroadcastMessage("The player " .. getCreatureName(cid) .. " won a " .. getItemInfo(itemOneID).name .. ".", MESSAGE_EVENT_ADVANCE)
			doRemoveItem(item.uid, 1)
		elseif rand == 2 then 
			doPlayerAddItem(cid,itemTwoID,1)
			doBroadcastMessage("The player " .. getCreatureName(cid) .. " won a " .. getItemInfo(itemTwoID).name .. ".", MESSAGE_EVENT_ADVANCE)
			doRemoveItem(item.uid, 1)
		elseif rand == 3 then 
			doPlayerAddItem(cid,itemThreeID,1)
			doBroadcastMessage("The player " .. getCreatureName(cid) .. " won a " .. getItemInfo(itemThreeID).name .. ".", MESSAGE_EVENT_ADVANCE)
			doRemoveItem(item.uid, 1)
		elseif rand == 4 then 
			doPlayerAddItem(cid,itemFourID,1)
			doBroadcastMessage("The player " .. getCreatureName(cid) .. " won a " .. getItemInfo(itemFourID).name .. ".", MESSAGE_EVENT_ADVANCE)
			doRemoveItem(item.uid, 1)
		elseif rand == 5 then 
			doPlayerAddItem(cid,itemFiveID,1)
			doBroadcastMessage("The player " .. getCreatureName(cid) .. " won a " .. getItemInfo(itemFiveID).name .. ".", MESSAGE_EVENT_ADVANCE)
			doRemoveItem(item.uid, 1)
		else
		end
	end
return true
end

FIXED
 
Last edited:
[19/12/2010 02:13:42] [Error - LuaScriptInterface::loadFile] data/actions/scripts/randomitem.lua:13: 'then' expected near '='
[19/12/2010 02:13:42] [Warning - Event::loadScript] Cannot load script (data/actions/scripts/randomitem.lua)
[19/12/2010 02:13:42] data/actions/scripts/randomitem.lua:13: 'then' expected near '='
[19/12/2010 02:13:42] Reloaded actions.
 
Back
Top