• 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 onKill() function ain't working?

Evan

A splendid one to behold
Senator
Premium User
Joined
May 6, 2009
Messages
7,019
Solutions
1
Reaction score
1,029
Location
United States
Lua:
function onKill(cid, target, lastHit)
monsterPos = getCreaturePosition(target)
newMonsterPos = {x= 748, y= 1139, z= 7}
wall_pos = {x= 748, y= 1139, z= 7, stackpos= 1}  
wall = getThingfromPos(wall_pos)  

        if isPlayer(cid) and (getCreatureName(target):lower() == "demon oak1") and wall.itemid == 8289 then
        doSendAnimatedText(monsterPos, "KILL!", TEXTCOLOR_ORANGE)
		doRemoveItem(wall.uid) 
		doSummonCreature("demon oak2", newMonsterPos)
        end
    return TRUE
end

Anyone know why this isn't working?
Is it because I have 2 different onKill() functions existing?
 
ruda might be right, it may not be possible to have 2 different onKill() scripts registered at the same time.

Anyone know how to put these 2 together?

Lua:
function onKill(cid, target, lastHit)
monsterPos = getCreaturePosition(target)
newMonsterPos = {x= 748, y= 1139, z= 7}
wall_pos = {x= 748, y= 1139, z= 7, stackpos= 1}  
wall = getThingfromPos(wall_pos)  
 
        if isPlayer(cid) and (getCreatureName(target):lower() == "demon oak1") and wall.itemid == 8289 then
        doSendAnimatedText(monsterPos, "KILL!", TEXTCOLOR_ORANGE)
		doRemoveItem(wall.uid) 
		doSummonCreature("demon oak2", newMonsterPos)
        end
    return TRUE
end

and

Lua:
function onKill(cid, target, lastHit)
monsterPos = getCreaturePosition(target)
newMonsterPos = {x= 750, y= 1137, z= 7}
wall_pos = {x= 750, y= 1137, z= 7, stackpos= 1}  
wall = getThingfromPos(wall_pos)

        if isPlayer(cid) and (getCreatureName(target):lower() == "demon oak") and wall.itemid == 8288 then
        doSendAnimatedText(monsterPos, "KILL!", TEXTCOLOR_ORANGE)
		doRemoveItem(wall.uid) 
		doSummonCreature("demon oak1", newMonsterPos)
        end
    return TRUE
end



Here is my noob attempt:

Lua:
function onKill(cid, target, lastHit)
monsterPos = getCreaturePosition(target)
monsterPos2 = getCreaturePosition(target)
newMonsterPos = {x= 750, y= 1137, z= 7}
wall_pos = {x= 750, y= 1137, z= 7, stackpos= 1}  
wall = getThingfromPos(wall_pos)
newMonsterPos2 = {x= 748, y= 1139, z= 7}
wall_pos2 = {x= 748, y= 1139, z= 7, stackpos= 1}  
wall2 = getThingfromPos(wall_pos2)  

        if isPlayer(cid) and (getCreatureName(target):lower() == "demon oak") and wall.itemid == 8288 then
        doSendAnimatedText(monsterPos, "KILL!", TEXTCOLOR_ORANGE)
		doRemoveItem(wall.uid) 
		doSummonCreature("demon oak1", newMonsterPos)
        else if isPlayer(cid) and (getCreatureName(target):lower() == "demon oak1") and wall2.itemid == 8289 then
        doSendAnimatedText(monsterPos2, "KILL!", TEXTCOLOR_ORANGE)
		doRemoveItem(wall2.uid) 
		doSummonCreature("demon oak2", newMonsterPos2)
        end
        end
    return TRUE
end

And does not work (no errors too)
 
Try:
Lua:
local t = {
['demon oak'] = {"demon oak1", {x= 750, y= 1137, z= 7}, {x= 750, y= 1137, z= 7, stackpos= 1}, 8288},
['demon oak1'] = {"demon oak2", {x= 748, y= 1139, z= 7}, {x= 748, y= 1139, z= 7, stackpos= 1}, 8289}
}
function onKill(cid, target, lastHit)
monsterPos = getCreaturePosition(target)
if isPlayer(cid) then
   for k, v in pairs(t) do
       if (getCreatureName(target):lower() == k) then
          if getThingfromPos(v[3]).itemid == v[4] then
             doSendAnimatedText(monsterPos, "KILL!", TEXTCOLOR_ORANGE)
             doRemoveItem(getThingfromPos(v[3]).uid)
             doSummonCreature(v[1], v[2])
          end
      end
   end
end
return true
end
 
@Santi,

Okay, when you kill the first "demon oak", it summons "demon oak1" and "demon oak2", I just need it to summon "demon oak1". Though, killing "demon oak1" does summon "demon oak2", but killing "demon oak2" does summon "demon oak1" and THAT is not supposed to happen (will make demon oak3 later).
 
Lua:
local config = {
	["MONSTER"] = {
		"SECOND MONSTER", ITEMID, {x = 100, y = 100, z = 7}, {x = 100, y = 100, z = 7}
	}
}

function onKill(cid, t)
	for v, x in pairs(config) do
		if getCreatureName(t):lower() == v then
			doCreatureSay(t, "OWNED!", TALKTYPE_MONSTER)
			doCreateMonster(x[1], x[3], false, false, nil)
			doRemoveItem(getTileItemById(x[4], x[2]).uid)
		end
	end
	
	return true
end
 
@Santi,

Okay, when you kill the first "demon oak", it summons "demon oak1" and "demon oak2", I just need it to summon "demon oak1". Though, killing "demon oak1" does summon "demon oak2", but killing "demon oak2" does summon "demon oak1" and THAT is not supposed to happen (will make demon oak3 later).

I forgot about something and I edited that some minutes ago.
That error should not be any longer now.
 
I'm not trying to bug you here, but I tried the script again.
It doesn't summon demon oak2 for some reason.

I tried to look and see if I could make any changes myself before coming back here like a beggar, unfortunately, I've never seen any of this v[#] stuff in my life, so... I'm lost.

No errors in console either.
 
Yeah, thanks for the bump though.

@Santi, if you could give a tutorial on how you used v[#] stuff in the script, I could see if I can fix it myself.
 
it's simple
look at the var t. it's an array, right?

Code:
local t = {
['demon oak'] = {"demon oak1", {x= 750, y= 1137, z= 7}, {x= 750, y= 1137, z= 7, stackpos= 1}, 8288},
['demon oak1'] = {"demon oak2", {x= 748, y= 1139, z= 7}, {x= 748, y= 1139, z= 7, stackpos= 1}, 8289}
}

Code:
local t = {
['indexName'] = {value1, value2, value3, value4, etc...}
or
[indexNumber] = {value1, value2, value3, value4, etc...}
}

so...(an example)
to access this array "manually" and get the fourth value of the first index you should use t['demon oak'][4] or just t[1][4] (will return 8288)

the for does access it dynamically, going through the whole array, where (in this case)...
k = only the index
v = the current index as a variable

understand?
 
Okay so..

To summon demon oak2, how would the k work?
Because when I used the script, for some reason, k was determined to be demon oak, now how do I make k be demon oak1? If that's how it's supposed to be meant?

I understand the array stuff, but I'm very very confused with the for/k/v stuff.
 
@JDB,

I tried looking into your script but I got really confused because I had to figure out how to add more than one monsters, considering I will be using 4.
Also, being the coordinates were the same, I didn't know which is which. Then ruda came to give me a better knowledge on arrays and tables and now I can understand what all those x[#]'s were, but I'm still a teeny bit confused, it'll take me a bit until I eventually figure it out. I appreciate you helping me though!
 
To use the stuff in local cfg. Match the colors.

Code:
local cfg = {
	[COLOR="red"]["monster"][/COLOR] = [COLOR="blue"]{"monster2"}[/COLOR]
}

function onKill(cid, t)
	for [B][COLOR="red"]k[/COLOR][/B], [COLOR="blue"][B]v[/B][/COLOR] in pairs(cfg) do
		if getCreatureName(t):lower() == [B][COLOR="red"]k[/COLOR][/B] then
			-- do something awesome with [B][COLOR="blue"]v[/COLOR][/B] --
		end
		
		break
	end
	
	return true
end

Continued (v)...

Code:
{[COLOR="blue"]"monster2"[/COLOR], [COLOR="blue"]{x = 100, y = 100, z = 7}[/COLOR]} = [COLOR="blue"]v[1][/COLOR], [COLOR="blue"]v[2][/COLOR]

If you add, it's the same result. You don't change anything EXCEPT THE CONFIG.

Code:
local cfg = {
	[COLOR="red"]["monster"][/COLOR] = [COLOR="blue"]{"monster2"},[/COLOR]
        [COLOR="red"]["monster2"][/COLOR] = [COLOR="blue"]{"monster3"}[/COLOR]
}

You kill the red, it makes the blue...and so on. This is why coders use this way, it's simpler. You don't edit ANYTHING in the body.
 
Last edited:
@up

I see, okay.

So, if I were to make another one for a different monster:
Code:
local v = {
	[COLOR="red"]["monster"][/COLOR] = [COLOR="blue"]{"monster2"}[/COLOR],
        [COLOR="green"]["monster2"][/COLOR] = [COLOR="orange"]{"monster3"}[/COLOR]
}

function onKill(cid, t)
	for[COLOR="red"] k[/COLOR],[COLOR="blue"] v[/COLOR] in pairs(v) do
		if getCreatureName(t):lower() == k then
			-- do something awesome --
		end
		
		break
	end
	
	return true
end

How would I do that? That's what I got confused with in Santi's code.
 
the for is used in this case to find the match.
the match in this case is if (getCreatureName(target):lower() == k) then


as I told you, k = only the index
so, k will be the value in the current index ('demon oak', 'demon oak1', and whatever indexes you have...)
the first if in the for checks if the name of the monster killed matches to the actual index of the array
if matches, then the second if in the for checks if the item id of the thing in the position v[3] (v[3] is the third value of the current index, which is a position in your case) is equal to v[4] (v[4] is fourth value in the current index, which is an item id in your case)
 
OHHHH! Okay... I see it now.
So, k finds out what you kill and uses that "row" to determine what to use in the script.

So, if I kill demon oak, v[1] = demon oak1, v[4] = 8288
If I kill demon oak1, v[1] = demon oak2, v[4] = 8289

Code:
local t = {
[COLOR="red"]['demon oak'][/COLOR] = {[COLOR="darkorange"]"demon oak1"[/COLOR], {x= 750, y= 1137, z= 7}, {x= 750, y= 1137, z= 7, stackpos= 1}, [COLOR="magenta"]8288[/COLOR]},
[COLOR="green"]['demon oak1'][/COLOR] = {[COLOR="blue"]"demon oak2"[/COLOR], {x= 748, y= 1139, z= 7}, {x= 748, y= 1139, z= 7, stackpos= 1}, [COLOR="indigo"]8289[/COLOR]}
}

That makes everything a whole hell lot simpler, holy shit... why haven't I learned this before?
Thanks A LOT guys!
 
Back
Top Bottom