• 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 A function not work with monster

Exedion

Active Member
Joined
Jun 11, 2007
Messages
629
Reaction score
30
some days ago, diath give me a function to get the closer creature in a area, with a radius based on center position, with that function i made my own one, and work very well with players, but with monsters give me a error:

The function:

LUA:
function getCloserCreaurebyStorage(position, key, radiusA, radiusB)
	local area, creatures = getArea(position, radiusA, radiusB), {}
	for i = 1, #area do
		local creature = getTopCreature(area[i]).uid
		if creature > 0 and creature > getTopCreature(position).uid and getCreatureStorage(creature, key) ~= 1 then
			table.insert(creatures, creature)
		end
	end

	table.sort(creatures,
		function(a, b)
			return getDistanceBetween(getThingPosition(a), position) < getDistanceBetween(getThingPosition(b), position)
		end
	)

	return #creatures > 0 and creatures[1] or nil
end

a script working with players:

LUA:
function onCastSpell(cid, var)
	doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "The ".. getCreatureName(getCloserCreaurebyStorage(getThingPosition(cid), 1000, 5, 7)) .." is the most closer creature.")
	return true 
end

with a closer dragon, this return me a message like: The Dragon is the most closer creature.


a not working script with monster:
LUA:
function onStatsChange(cid, attacker, type, combat, value)
	doPlayerSendTextMessage(attacker, MESSAGE_STATUS_CONSOLE_BLUE, "The ".. getCreatureName(getCloserCreaurebyStorage(getThingPosition(cid), 1000, 5, 7)) .." is the most closer creature.")
	return true
end

is a testing script: the attacker is my player and the cid is the creature with other creature closer, this must return me the closer creature but only return me this error:
Code:
[20/11/2012 17:13:41] [Error - CreatureScript Interface] 
[20/11/2012 17:13:41] data/creaturescripts/scripts/chainreaction.lua:onStatsChange
[20/11/2012 17:13:41] Description: 
[20/11/2012 17:13:41] data/creaturescripts/scripts/chainreaction.lua:28: attempt to concatenate a boolean value
[20/11/2012 17:13:41] stack traceback:
[20/11/2012 17:13:41] 	data/creaturescripts/scripts/chainreaction.lua:28: in function <data/creaturescripts/scripts/chainreaction.lua:1>

help please!

- - - Updated - - -

bump

- - - Updated - - -

bump
 
because add the creature at the center position in the table and always shows the creature at the center position as the closest, I wanted this creature be ignored
 
can you explain me better? it's because of that error does not work in monsters?

- - - Updated - - -

i fix the function like this
LUA:
function getCloserCreaure(position, key, radiusA, radiusB)
	local area, creatures = getArea(position, radiusA, radiusB), {}
	for i = 1, #area do
		local creature = getTopCreature(area[i]).uid
		if creature > 0 and creature ~= getTopCreature(position).uid and getCreatureStorage(creature, key) ~= 1 then
			table.insert(creatures, creature)
		end
	end
	table.sort(creatures,
		function(a, b)
			return getDistanceBetween(getThingPosition(a), position) < getDistanceBetween(getThingPosition(b), position)
		end
	)
	for e = 1, #creatures do
		addEvent(table.remove(creatures, creatures[e]), 1)
	end
	return #creatures > 0 and creatures[1] or nil
end

i try to make a chain attack to others monsters but when execute the scripts some error happens, not attack the closer creature, ever attack the same creature and sometime not attack the closer creature anymore, check my script for chain attack

LUA:
function onStatsChange(cid, attacker, type, combat, value)
local chain = {9000, 9001}
local closer = getCloserCreaure(getThingPosition(cid), chain[1], 5, 7)

	function restarAttack(var)
		if(isCreature(cid) and isCreature(attacker)) then
			doCreatureSetStorage(cid, chain[1], nil)			
			doCreatureSetStorage(attacker, chain[2], nil)
		else
			return true
		end
	end

	if(type == STATSCHANGE_HEALTHLOSS) then
		if(getCreatureStorage(attacker, chain[1]) > 0 and getCreatureStorage(attacker, chain[2]) < 3) then
			registerCreatureEvent(closer, "ChainReaction")
			doTargetCombatHealth(attacker, closer, combat, -(value / 3), -(value / 2), CONST_ME_NONE)
			doCreatureSetStorage(cid, chain[1], 1)
			doCreatureSetStorage(attacker, chain[2], getCreatureStorage(attacker, chain[2]) + 1)	
			addEvent(restarAttack, 100, cid)	
		end
		return true		
	end
	return true
end

- - - Updated - - -

bump
 
Back
Top