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

Remove backpack

MiPo91

Member
Joined
Mar 30, 2010
Messages
299
Reaction score
18
Location
Finland
Hello, Im wondering how to do talkaction that would remove all empty backpacks from player if he says !bp or something like that.
 
data/talkactions/talkactions.xml
Code:
	<talkaction words="!bp" event="script" value="bp.lua"/>
data/talkactions/scripts/bp.lua
Code:
local removable = {1987, 1988}

function removeEmptyContainers(uid, ids)
	local ret, i = 0, 0
	while i < getContainerSize(uid) do
		local v = getContainerItem(uid, i)
		if isContainer(v.uid) then
			if getContainerSize(v.uid) ~= 0 then
				ret = ret + removeEmptyContainers(v.uid, removable)
				if isInArray(removable, v.itemid) and getContainerSize(v.uid) == 0 then
					i = i - 1
				end
			elseif isInArray(removable, v.itemid) then
				doRemoveItem(v.uid)
				i = i - 1
				ret = ret + 1
			end
		end
		i = i + 1
	end
	return ret
end

function onSay(cid, words, param, channel)
	local ret = 0
	for i = 1, 10 do
		local v = getPlayerSlotItem(cid, i).uid
		if v > 0 and isContainer(v) then
			ret = ret + removeEmptyContainers(v, removable)
		end
	end
	ret = tostring(ret)
	return doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, ret .. " empty container" .. (ret:sub(-1) == "1" and ret:sub(-2) ~= "11" and "" or "s") .. " removed.")
end
 
Thanks, im going to test it when i get at home!

EDIT:
Its working, Rep++ but do you know how to add exhaustion time to the command? like it could be executed once in 1 minute or something like that.

EDIT:2
I got it working with exhaustion.
Code:
local removable = {1987, 1988, 5926, 7342, 5949, 2004, 2001, 2003, 2000, 2002, 1998, 10521, 10518, 10519}

function removeEmptyContainers(uid, ids)
	local ret, i = 0, 0
	while i < getContainerSize(uid) do
		local v = getContainerItem(uid, i)
		if isContainer(v.uid) then
			if getContainerSize(v.uid) ~= 0 then
				ret = ret + removeEmptyContainers(v.uid, removable)
				if isInArray(removable, v.itemid) and getContainerSize(v.uid) == 0 then
					i = i - 1
				end
			elseif isInArray(removable, v.itemid) then
				doRemoveItem(v.uid)
				i = i - 1
				ret = ret + 1
			end
		end
		i = i + 1
	end
	return ret
end

local config = {
        exhaustionInSeconds = 30,
        storage = 73209
}

function onSay(cid, words, param, channel)
	if(exhaustion.check(cid, config.storage) == TRUE) then
			doPlayerSendCancel(cid, "You can use this command once every  " .. config.exhaustionInSeconds .. " seconds.")
			return TRUE
	end
	local ret = 0
	for i = 1, 10 do
		local v = getPlayerSlotItem(cid, i).uid
		if v > 0 and isContainer(v) then
			ret = ret + removeEmptyContainers(v, removable)
		end
	end
	ret = tostring(ret)
	exhaustion.set(cid, config.storage, config.exhaustionInSeconds)
	return doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, ret .. " empty container" .. (ret:sub(-1) == "1" and ret:sub(-2) ~= "11" and "" or "s") .. " removed.")
end
 
Last edited:
@Cykotitan!

I've edited your script and now if the containers have some action id it won't be removed (Some quest are using backpack with action id)
Lua:
local removable = {1987, 1988}

function removeEmptyContainers(uid, ids)
	local ret, i = 0, 0
	while i < getContainerSize(uid) do
		local v = getContainerItem(uid, i)
		if isContainer(v.uid) then
			if getContainerSize(v.uid) ~= 0 then
				ret = ret + removeEmptyContainers(v.uid, removable)
				if isInArray(removable, v.itemid) and getContainerSize(v.uid) == 0 then
					i = i - 1
				end
			elseif isInArray(removable, v.itemid) then
				if v.actionid < 1 then
					doRemoveItem(v.uid)
					i = i - 1
					ret = ret + 1
				end
			end
		end
		i = i + 1
	end
	return ret
end

function onSay(cid, words, param, channel)
	local ret = 0
	for i = 1, 10 do
		local v = getPlayerSlotItem(cid, i).uid
		if v > 0 and isContainer(v) then
			ret = ret + removeEmptyContainers(v, removable)
		end
	end
	ret = tostring(ret)
	return doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, ret .. " empty container" .. (ret:sub(-1) == "1" and ret:sub(-2) ~= "11" and "" or "s") .. " removed.")
end
 
Back
Top