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

onKill, modify userdata value target.uid

guiismiti

Well-Known Member
Joined
May 19, 2014
Messages
315
Solutions
3
Reaction score
68
Hello,

I noticed that the onKill creature script for the killing in the name of ... quest may run multiple times when more than one player has killed the monster. This will add a kill for every player who runs the script, and I want to change that.

The parameters of this function are player and target.
target is a user data and I am able to print target.uid

I would like the script to run once only, so I tried to set target.uid to 0 in the end of the script.
In the beginning of the script I'm testing if target.uid is 0, and if it is, it means the kill has been added to another player already, and the function ends (I know that it may have added the kill to the lowest damage dealing player, but I'm gonna check the target's damage map and fix that later).

The problem is, when I try to set target.uid to 0, I get this error:
Code:
attempt to index local 'target' (a userdata value)

If I can change this value, how do I do it? I need a persistent flag so the script knows the kill for this monster has already been counted.


Thanks in advance.
 
so, you want the kill only count for the person who did the most dmgs task, e.g if the most dmg person doesnt have task, noone gets credit, or the person with that task who did the most dmg gets credit?
you cant modify target for onkill, onKill is called once per player that participated in the kill afaik, and target will always be different userdata
to check if a monster already has been counted you can store the killed monsters id in a variable / table temporarily, and then use addevent to reset it after a second or so, and onKill check if that id is in the table, if not, credit, if it is, dont credit
or you can use damage map
 
so, you want the kill only count for the person who did the most dmgs task, e.g if the most dmg person doesnt have task, noone gets credit, or the person with that task who did the most dmg gets credit?
you cant modify target for onkill, onKill is called once per player that participated in the kill afaik, and target will always be different userdata
to check if a monster already has been counted you can store the killed monsters id in a variable / table temporarily, and then use addevent to reset it after a second or so, and onKill check if that id is in the table, if not, credit, if it is, dont credit
or you can use damage map

But how can I use a persistent table? Can I store a table in the game storages?

If I wanted to give the kill to the highest damaging player, I would scan the damage map and add the kill if it is the right player. Else, return.

But instead, I want the players to get the same chance to get the kill as the damage they did. E. g., player dealt 35% of the damage, he has 35% chance to get the kill. So, anyway, I still need to link one script run to the other through a persistent variable (preferably a table, where I can add and the monster's uid and remove it one second later, like you suggested).
 
have you tried onPrepareDeath? i think that might only be executed once, for the creature dying, and you can get damagemap and do stuff with it, instead of 1 onKill event per player doing different things
then you wouldnt have to do anything else either, just do a math.random vs damagemap numbers and award kill to the player who gets it

you can just put
Code:
t = {}
outside a function, e.g in global.lua for example, and all files can access it

if onpreparedeath is executed once per player, it will be the same as onkill though
then you could have something like
Code:
function resettable(id)
t[id] = nil
end


if not t[killed:getId()] then
t[killed:getId()] = true
local map = killed:damagemap()
do stuff with map
addEvent(resettable, 500, killed:getId())
end
 
Back
Top