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

Random Treasure Chests

Sizaro

Advanced OT User
Joined
Aug 20, 2007
Messages
5,180
Solutions
5
Reaction score
232
Location
Sweden
GitHub
coldensjo
I want to make a system that spawns random treasure chests every (1) min on a random tile in the world (floor 7).
And if no one rightclick on the treasure chest within (1) min then it despawns and spawn on a new tile.

Code:
getClosestFreeTile(cid, targetpos[, extended = false[, ignoreHouse = true]])

I think this will be a great idea for people who love to explore and let them gain a little reward now and then :p
btw if someone wants to use the script, feel free too ^__^

I'm guessing this can be done with globalevents but I can't figure out how >_<

I have the treasure chest already:
LUA:
local tchest1 = {2148,2152,2160}
local tchest2 = {2528,2519,2534,2462,2491,2662,2476,2487,2656,2477,2488,7730,7406,7415,7454,2392,2432,7430,8902,13872,13880,8850,8856}

function onUse(cid, item, fromPosition, itemEx, toPosition)
local f = 1
	if(item.itemid == 5675) then
		local r = math.random(1,3)
		
		if r == 1 then f = 100
		doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, 'You have found 100 gold coins.')
		end
		
		if r == 2 then f = 25
		doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, 'You have found 25 platinum coins.')
		end
		
		if r == 3 then f = 1
		doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, 'You have found 1 crystal coin.')
		end
		
		doPlayerAddItem(cid, tchest1[r], f)
		
	elseif(item.itemid == 5676) then
		local c = math.random(1, #tchest2)
		doPlayerAddItem(cid, tchest2[c], f)
		doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, 'You have found a ' .. getItemNameById(tchest2[c]) .. '.')
	end
		doRemoveItem(item.uid, 1)
		doSendMagicEffect(fromPosition, CONST_ME_GIFT_WRAPS)
	return true
end

// Sizaro
 
Last edited:
Actions
randomchest.lua:
LUA:
local CHESTS = {
	[5675] = {
		[1] = {2148, 1},
		[2] = {2152, 1},
		[3] = {2160, 1}
	},
	[5676] = {
		[1] = {2528, 1},
		[2] = {2519, 1},
		[3] = {2534, 1},
		[4] = {2462, 1},
		[5] = {2491, 1},
		[6] = {2662, 1},
		[7] = {2476, 1},
		[8] = {2487, 1},
		[9] = {2656, 1},
		[10] = {2477, 1},
		[11] = {2488, 1},
		[12] = {7730, 1},
		[13] = {7406, 1},
		[14] = {7415, 1},
		[15] = {7454, 1},
		[16] = {2392, 1},
		[17] = {2432, 1},
		[18] = {7430, 1},
		[19] = {8902, 1},
		[20] = {13872, 1},
		[21] = {13880, 1},
		[22] = {8850, 1},
		[23] = {8856, 1}
	}
}
function onUse(cid, item, fromPosition, itemEx, toPosition)
	local chest = CHESTS[item.itemid]
	if(not chest) then
		return false
	end

	local addItem = chest[math.random(table.maxn(chest))]
	local itemUid = doCreateItemEx(addItem[1], addItem[2])
	if(doPlayerAddItemEx(cid, itemUid, false) ~= RETURNVALUE_NOERROR) then
		result = "You have found a reward weighing " .. getItemWeight(itemUid) .. " oz. It is too heavy or you have not enough space."
	else
		result = "You have found " .. (addItem[2] == 1 and getItemInfo(addItem[1]).article or addItem[2]) .. " " .. (addItem[2] == 1 and getItemInfo(addItem[1]).name or getItemInfo(addItem[1]).plural) .. "."
		doRemoveItem(item.uid)
	end

	doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, result)
	return true
end
In actions.xml:
XML:
	<action itemid="5675;5676" event="script" value="randomchest.lua"/>
GlobalEvents
randompositionitem.lua:
LUA:
local config = {
	item = {5675, 5676},
	delay = 1 * 60 * 1000,
	fromPos = {x = 1000, y = 1000, z = 7},
	toPos = {x = 1000, y = 1000, z = 7}
}
 
local function doCreateItemInRandomPosition()
	local ranx, rany, ranz = math.random(config.fromPos.x, config.toPos.x), math.random(config.fromPos.y, config.toPos.y), math.random(config.fromPos.z, config.toPos.z)
	local pos = {z = ranx, y = rany, z = ranz, stackpos = STACKPOS_GROUND}
	if(getTileThingByPos(pos).uid > 0) then
		local createItem = doCreateItem(config.item[math.random(table.maxn(config.item))], 1, pos)
		if(createItem) then
			addEvent(function(uid)
				if(getThing(uid)) then
					doTeleportItemToRandomPosition(uid)
				end
			end, config.delay, createItem.uid)
		end
	else
		doCreateItemInRandomPosition()
	end
end
 
local function doTeleportItemToRandomPosition(uid)
	doRemoveItem(uid)
	doCreateItemInRandomPosition()
end
 
function onThink(interval)
	doCreateItemInRandomPosition()
	return true
end
In globalevents.xml:
XML:
	<globalevent name="randompositionitem" interval="60000" event="script" value="randompositionitem.lua"/>
 
Last edited:
LUA:
		result = "You have found " .. getItemInfo(addItem[2]) .. " " .. getItemInfo(addItem[1]) .. "."
LUA:
		result = "You have found " .. (addItem[2] == 1 and getItemInfo(addItem[1]).article or addItem[2]) .. " " .. (addItem[2] == 1 and getItemInfo(addItem[1]).name or getItemInfo(addItem[1]).plural) .. "."
 
LUA:
		result = "You have found " .. (addItem[2] == 1 and getItemInfo(addItem[1]).article or addItem[2]) .. " " .. (addItem[2] == 1 and getItemInfo(addItem[1]).name or getItemInfo(addItem[1]).plural) .. "."


Nevermind, this script is impossible.
My script or what? Oh right...forgot the re-spawn after 1min part. I'll do it later.
EDIT: Done.
 
Last edited:
This will cause the server to lag and if it's unlucky it will crash the server.
I was up the whole night with the help from Cyko, but we didn't manage to fix this and it still crashed.
 
Back
Top