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

Lua Bad Argument in lua

LucasFerraz

Systems Analyst
Joined
Jun 10, 2010
Messages
2,857
Reaction score
96
Location
Brazil
How can I fix it?

LUA:
	Castle.PreEmpes = 
		{
			{x=33298, y=33048, z=6, stackpos=253}, 
			{x=33324, y=33048, z=6, stackpos=253}
		}

function Woe.checkPre()
	local Count = 0
	for _, i in ipairs(Castle.PreEmpes) do
		if (isCreature(getThingFromPos(i).uid) == false) then
			Count = Count + 1
		end
	end
	return (Count == #Castle.PreEmpes)
end

Error:

Bad argument #1 to 'ipairs' (table expected, got nil)
stack traceback:
[C]: in function 'ipairs'
SCRIPTNAME:152 in function 'checkpre'
 
Code:
Castle = {}


	Castle.PreEmpes =
		{
			{x=33298, y=33048, z=6, stackpos=253},
			{x=33324, y=33048, z=6, stackpos=253}
		}


function Woe.checkPre()
	local Count = 0
	for _, i in ipairs(Castle.PreEmpes) do
		if (isCreature(getThingFromPos(i).uid) == false) then
			Count = Count + 1
		end
	end
	return (Count == #Castle.PreEmpes)
end
 
and that too :p
you had just never initialized the castle object~ so it returned null

edit: oh yeah didnt notice that, cbrms would be the easy fix, this is how you would do it with your attempts syntax:
LUA:
Castle =     {
        PreEmpes =     {
                {x=33298, y=33048, z=6, stackpos=253}, 
                {x=33324, y=33048, z=6, stackpos=253}
            }
    }
 
Last edited:
how come :s
I_afCh-.png


and >> function Woe.checkPre()
do you have "Woe" predefined?

that's why classes are popular now
 
Last edited:
hmm
LUA:
Castle = {}


	Castle.PreEmpes =
		{
			{x=33298, y=33048, z=6, stackpos=253},
			{x=33324, y=33048, z=6, stackpos=253}
		}

Woe = {}

function Woe.checkPre()
	local Count = 0
	for _, i in ipairs(Castle.PreEmpes) do
		if (isCreature(getThingFromPos(i).uid) == false) then
			Count = Count + 1
		end
	end
	return (Count == #Castle.PreEmpes)
end
 
Still got errors

_woe.lua in TFS folder
Code:
dofile("./var.woe")

local config = woe_config

fileStore = true

infoFile = 'tmp.woe'
infoLua = {}

Woe = {}
Woe.__index = Woe

function Woe.setup()
	db.executeQuery("DROP TABLE IF EXISTS `woe`;")
	db.executeQuery("CREATE TABLE `woe` (`id` INT( 11 ) NOT NULL AUTO_INCREMENT ,`started` INT( 11 ) NOT NULL ,`guild` INT( 11 ) NOT NULL ,`breaker` INT( 11 ) NOT NULL ,`time` INT( 11 ) NOT NULL ,PRIMARY KEY ( `id` ) ,UNIQUE (`id`)) ENGINE = MYISAM")
	doBroadcastMessage("DB Added [...]", config.bcType)
	if fileStore then
		local newFile = io.open(infoFile, "w")
		for _, i in ipairs({'started', 'guild', 'breaker', 'time'}) do
			newFile:write(i .. ' = 0 ;end;\n')
		end
		newFile:close()
	else
		db.executeQuery("DROP TABLE IF EXISTS `tmpwoe`;")
		db.executeQuery("CREATE TABLE `tmpwoe` (`started` INT( 11 ) NOT NULL ,`guild` INT( 11 ) NOT NULL ,`breaker` INT( 111 ) NOT NULL ,`time` INT( 1 ) NOT NULL) ENGINE = MYISAM ;")
		db.executeQuery("ALTER TABLE `tmpwoe` ADD `indexer` INT NOT NULL ")
		db.executeQuery("INSERT INTO `tmpwoe` (`started` ,`guild` ,`breaker` ,`time`, `indexer`)VALUES ('0', '0', '0', '0', '1');")
	end
end

function Woe.getInfo()
	if fileStore then
		local open = io.open(infoFile, "r")
		if open then
			for i in io.lines(infoFile) do 
				for v, k in ipairs({'started', 'guild', 'breaker', 'time'}) do
					if (i:find(k)) then
						n = i:match(k .. '.*')
						infoLua[v] = tonumber(n:sub(n:find('=') + 2, n:find(';end;') - 2))
					end
				end
			end
			open:close()
		end
	else
		for v, k in ipairs({'started', 'guild', 'breaker', 'time'}) do
			local tmp = db.getResult("SELECT " .. k .. " FROM `tmpwoe` WHERE `indexer` = '1';")
			infoLua[v] = tmp:getDataInt(k)
			tmp:free()
		end
	end
end

function Woe.updateInfo(tab)
	if fileStore then
		local open = io.open(infoFile, "w")
		if open then
			for k, i in ipairs({'started', 'guild', 'breaker', 'time'}) do
				open:write(i .. ' = ' .. tab[k] .. ' ;end;\n')
			end
			open:close()
		end
	else
		for v, k in ipairs({'started', 'guild', 'breaker', 'time'}) do
			db.executeQuery("UPDATE `tmpwoe` SET " .. k .. " =  " .. tab[v] .. " WHERE `indexer` = 1;")
		end
	end
end

function Woe.save()
	Woe.getInfo()
	db.executeQuery("INSERT INTO `woe` (`started`, `guild`, `breaker`, `time`) VALUES (" .. infoLua[1] .. ", " .. infoLua[2] .. ", " .. infoLua[3] .. ", " .. infoLua[4] .. ");")
end

function Woe.getGuildName(id)
	local res = db.getResult("SELECT `name` FROM `guilds` WHERE `id` = " .. id .. ";")
	if (res:getID() ~= -1) then
		ret = res:getDataString('name')
		res:free()
	end
	return ret
end

function Woe.breakerName()
	Woe.getInfo()
	return infoLua[3] ~= 0 and getPlayerNameByGUID(infoLua[3]) or 'None'
end

function Woe.guildName()
	Woe.getInfo()
	return infoLua[2] ~= 0 and Woe.getGuildName(infoLua[2]) or 'Nones'
end

function Woe.startTime()
	Woe.getInfo()
	return os.date("%d %B %Y %X ", infoLua[1])
end

function Woe.breakTime()
	Woe.getInfo()
	return os.date("%d %B %Y %X ", infoLua[4])
end

function Woe.timeToEnd()
	Woe.getInfo()
	local myTable = {}
	for k, i in ipairs({"%M", "%S"}) do
		myTable[k] = os.date(i, os.difftime(os.time(), infoLua[1]))
	end
	return {mins = ((config.timeToEnd - 1) - myTable[1]), secs = (60 - myTable[2])}
end

function Woe.moveBack(cid, fromPosition, text)
	doTeleportThing(cid, fromPosition, TRUE)
	doPlayerSendCancel(cid, text)
end

function Woe.getGuildMembers(id)
	local members = {}
	for _, i in ipairs(getPlayersOnline()) do
		if id == getPlayerGuildId(i) then
			table.insert(members, i)
		end
	end
	return members
end

function Woe.deco(text)
	for _, i in ipairs(Castle.decoraciones) do
		doItemSetAttribute(i, "description", text)
	end
end

function Woe.removePortals()
	for _, i in ipairs(Castle.PrePortalsPos) do
		if (getThingFromPos(i).itemid > 0) then
			doRemoveItem(getThingFromPos(i).uid)
		end
	end
end

function Woe.removePre()
	for _, i in ipairs(Castle.PreEmpes) do
		if (isCreature(getThingFromPos(i).uid) == true) then
			doRemoveCreature(getThingFromPos(i).uid)
		end
	end
end

function Woe.checkPre()
	local Count = 0
	for _, i in ipairs(Castle.PreEmpes) do
		if (isCreature(getThingFromPos(i).uid) == false) then
			Count = Count + 1
		end
	end
	return (Count == #Castle.PreEmpes)
end

function Woe.isTime()
	return (getGlobalStorageValue(stor.WoeTime) == 1)
end

function Woe.isStarted()
	return (getGlobalStorageValue(stor.Started) == 1)
end

function Woe.isRegistered(cid)
	return (getPlayerStorageValue(cid, stor.register) == 1)
end

function Woe.isInCastle(cid)
	local myPos = getCreaturePosition(cid)
	if (myPos.x >= Castle.salas.a.fromx and myPos.x <= Castle.salas.a.tox) then
		if (myPos.y >= Castle.salas.a.fromy and myPos.y <= Castle.salas.a.toy) then
			if isInArray({Castle.salas.a.z, Castle.salas.b.z, Castle.salas.c.z}, myPos.z) then
				return true
			end
		end
	end
	return false
end

function Woe.expulsar(guild, fromx, tox, fromy, toy, z, outpos)
	for _x = fromx, tox do
		for _y = fromy, toy do
			local player = getThingFromPos({x = _x, y = _y, z = z, stackpos = 253}).uid
			if (isPlayer(player) == true) then
				if (getPlayerGuildId(player) ~= guild) then
					doTeleportThing(player, outpos, false)
				end
			end
		end
	end
end

-- extras

function doSetItemActionId(uid, action)
	doItemSetAttribute(uid, "aid", action)
end

function exhaust(cid, storevalue, exhausttime)
-- Exhaustion function by Alreth, v1.1 2006-06-24 01:31
-- Returns 1 if not exhausted and 0 if exhausted
    newExhaust = os.time()
    oldExhaust = getPlayerStorageValue(cid, storevalue)
    if (oldExhaust == nil or oldExhaust < 0) then
        oldExhaust = 0
    end
    if (exhausttime == nil or exhausttime < 0) then
        exhausttime = 1
    end
    diffTime = os.difftime(newExhaust, oldExhaust)
    if (diffTime >= exhausttime or diffTime < 0) then
        setPlayerStorageValue(cid, storevalue, newExhaust) 
        return 1
    else
        return 0
    end
end

--new
guard_pos = 
	{
		{x = 33311, y = 33042, z = 5},
		{x = 33311, y = 33041, z = 5},
		{x = 33311, y = 33040, z = 5},
		{x = 33311, y = 33039, z = 5},
		{x = 33311, y = 33038, z = 5},
		{x = 33311, y = 33037, z = 5},
		{x = 33311, y = 33036, z = 5}
	}
	
function Woe.check()
	for storage = 73901, 73908 do
		local pid = getGlobalStorageValue(storage)
		if isCreature(pid) then
			return false
		end
	end
	return true
end

function Woe.summon()
	for k, i in ipairs(guard_pos) do
		local pid = doSummonCreature("guard", i)
		setGlobalStorageValue(73900 + k, pid)
	end
end

function Woe.remove()
	for storage = 73901, 73908 do
		local pid = getGlobalStorageValue(storage)
		if isCreature(pid) then
			doRemoveCreature(pid)
		end
	end
end

var.woe in TFS folder
Code:
Castle, woe_config, stor = {}, {}, {}
	
--castle name (?)
Castle.name = 'Ruthenburg'

--aid of entry.
Castle.tiles = 16900

--aid for created portals.
Castle.portals = 16901

--aid of flags in castle.
Castle.banderas = 16903

--aid of empes bases.
Castle.bases = 16902

--uids in case you have decorations with info.
Castle.decoraciones = {}

--where you teleported from temple.
Castle.pos = {x = 33311, y = 33090, z = 7}

--where players are teleported when finished or castle owner change.
Castle._exit = {x = 32360, y = 31782, z = 7}

--where portal appears
Castle.desde = {x = 32360, y = 31784, z = 7, stackpos = 1}

--true / false
default_map = false
--if you will not use default map you should config all the pos.

--main empe pos => with this pos all castle pos will be calculated (default map only)
Castle.empePos = {x = 33311, y = 33045, z = 5, stackpos = 253}

if default_map then
	local pos = Castle.empePos
	
	--entry for guild by using flags.
	Castle.guildEntry = {x = pos.x - 9, y = pos.y + 4, z = pos.z}
	
	--each floor should be all floor.
	Castle.salas = 
		{
			a = {fromx = pos.x - 18, tox = pos.x + 18, fromy = pos.y - 10, toy =  pos.y + 28, z = pos.z + 2},
			b = {fromx = pos.x - 18, tox = pos.x + 18, fromy = pos.y - 10, toy =  pos.y + 28, z = pos.z + 1},
			c = {fromx = pos.x - 18, tox = pos.x + 18, fromy = pos.y - 10, toy =  pos.y + 28, z = pos.z}
		}

	--pre empes pos

	--when you kill pre empes new portals appears.
	Castle.PrePortalsPos = 
		{
			{x = pos.x - 4, y =  pos.y - 6, z = pos.z + 1, stackpos = 1},
			{x = pos.x + 4, y =  pos.y - 6, z = pos.z + 1, stackpos = 1}
		}

	Castle.PreToPos = 
		{
			[1] = {x = pos.x - 7, y =  pos.y + 26, z = pos.z},
			[2] = {x = pos.x + 7, y =  pos.y + 26, z = pos.z}
		}

else
--edit those if not using default map
--read above to know what is everything.
--break this comment to use [...]
--[[
	Castle.guildEntry = {x = 33302, y = 33049, z = 5}
			
	Castle.salas = 
		{
			a = {fromx=33293, tox=33329, fromy=33035, toy=33073, z=7},
			b = {fromx=33293, tox=33329, fromy=33035, toy=33073, z=6},
			c = {fromx=33293, tox=33329, fromy=33035, toy=33073, z=5}
		}

	Castle = {}
	Castle.PreEmpes =
		{
			{x=33298, y=33048, z=6, stackpos=253},
			{x=33324, y=33048, z=6, stackpos=253}
		}

	Castle.PrePortalsPos = 
		{
			{x=33307, y=33039, z=6, stackpos=1},
			{x=33315, y=33039, z=6, stackpos=1}
		}

	Castle.PreToPos = 
		{
			[1] = {x = 33304, y = 33071, z = 5},
			[2] = {x = 33318, y = 33071, z = 5}
		}
]]
end

--duration
woe_config.timeToEnd = 30

--bc type
woe_config.bcType = MESSAGE_EVENT_ADVANCE

--days
woe_config.dias = {"friday", "saturday"}

--time to start
woe_config.horaPartida = 22

--access to start
woe_config.accessToStar = 4

--recall delay
woe_config.recallTime = 3 * 60

--rewards for winners
woe_config.premios = true

--storage for register players	
stor.register = 73909

--recall delay stor
stor.recall = 73910

--global stor started
stor.Started = 73911

--global stor [...]
stor.WoeTime = 73912

Error ocurring when killing a monster (creature scripts)
LUA:
dofile("./_woe.lua")

local config = woe_config

function onDeath(cid, corpse, killer)
	if isCreature(cid) == true then
		doRemoveCreature(cid)
	end
	if Woe.isTime() then
		if Woe.checkPre() then
			doSetItemActionId(doCreateItem(1387, 1, {x=33307, y=33039, z=6, stackpos=1}), 16901)
			doSetItemActionId(doCreateItem(1387, 1, {x=33315, y=33039, z=6, stackpos=1}), 16901)
			doBroadcastMessage("Both Pre-Empes have been broken, portals opened.", config.bcType)
		else
			doBroadcastMessage("One of Pre-Empes have been broken...", config.bcType)
		end
	end
	return true
end

- - - Updated - - -

getTopCreature pls

is it better?

- - - Updated - - -

I made this:

LUA:
		local creatureone = getTopCreature({x=33298, y=33048, z=6, stackpos=253})
		local creaturetwo = getTopCreature({x=33324, y=33048, z=6, stackpos=253})
		if (creatureone == FALSE) and (creaturetwo == FALSE) then
			doSetItemActionId(doCreateItem(1387, 1, {x=33307, y=33039, z=6, stackpos=1}), 16901)
			doSetItemActionId(doCreateItem(1387, 1, {x=33315, y=33039, z=6, stackpos=1}), 16901)
			doBroadcastMessage("Both Pre-Empes have been broken, portals opened.", config.bcType)
		elseif (creatureone == FALSE) and (creaturetwo == TRUE) then
			doBroadcastMessage("One of Pre-Empes have been broken...", config.bcType)
		elseif (creatureone == TRUE) and (creaturetwo == FALSE) then
			doBroadcastMessage("One of Pre-Empes have been broken...", config.bcType)
		end
Is it the better way to do this?

- - - Updated - - -

FIXED. Thank you guys!

LUA:
		local creatureone = getTopCreature({x=33298, y=33048, z=6, stackpos=253})
		local creaturetwo = getTopCreature({x=33324, y=33048, z=6, stackpos=253})
		if (isMonster(creatureone) == FALSE) and (isMonster(creaturetwo) == FALSE) then
			doSetItemActionId(doCreateItem(1387, 1, {x=33307, y=33039, z=6, stackpos=1}), 16901)
			doSetItemActionId(doCreateItem(1387, 1, {x=33315, y=33039, z=6, stackpos=1}), 16901)
			doBroadcastMessage("Both Pre-Empes have been broken, portals opened.", config.bcType)
		elseif (isMonster(creatureone) == FALSE) and (isMonster(creaturetwo) == TRUE) then
			doBroadcastMessage("One of Pre-Empes have been broken...", config.bcType)
		elseif (isMonster(creatureone) == TRUE) and (isMonster(creaturetwo) == FALSE) then
			doBroadcastMessage("One of Pre-Empes have been broken...", config.bcType)
		end
 
Last edited:
Back
Top