• 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 onDeath or onKill?

Il Knight

Veteran OT User
Joined
Dec 1, 2014
Messages
678
Solutions
7
Reaction score
352
Location
Spain
hi, i am triying to make this script work

Lua:
_____________________________
local chance = math.random(100)
local send = "asd"

function onDeath(creature, target, mostDamageKiller)

if getCreatureName(target) == "rat" then
player:setBankBalance(player:getBankBalance() + chance)
player:sendTextMessage(MESSAGE_STATUS_DEFAULT, send)

end
return true
end
______________________________

i need when you kill a "rat" in this case you get 0-100 gold coins in your bank balance but nothing happend :/
its possible to add more monsters in one script? like local monsters "rat", "rotworm" etc?
 
Maybe something like this?

Code:
local config = {
    monstersNames = {"Rotworm", "Rat"},
    maxGold = 100
}

function onKill(player, target)
    local monster = target:getMonster()
    if not monster then
        return true
    end
   
    if not isInArray({config.monstersNames}, monster:getName()) then
        return true
    end
   
    player:setBankBalance(player:getBankBalance() + math.random(1, maxGold)
    player:sendTextMessage(MESSAGE_STATUS_DEFAULT, string.format('You recived %d gold coin%s.', config.maxGold, config.maxGold ~= 1 and "s" or ""))
    return true
end
 
thanks for your help its give me an error on line 16 expect to close because need other ")"
and give other error in getmonster

i resolve that with

local value = math.random(0, 10)

function onKill(player, target, mostDamageKiller)

if getCreatureName(target) == "Rat" then
player:setBankBalance(player:getBankBalance() + value)
player:sendTextMessage(MESSAGE_STATUS_DEFAULT, value)

end
return true
end

but i need add something like
local config = {
monstersNames = {"Rotworm", "Rat"},

and have other problem if you kill a rat and give 5 gp, all the other rats give 5gp-- how i can resolve this?
 
done, still giving the same amount to all the monsters but if i reload creaturescripts change to other value
need to add something more after end? to change the amount of the math to the next monster
 
Put the table inside the function.
When the table is outside of the function, math.random only executes once, at the time of the script loading into the server.

Put like this.
Code:
function onKill(player, target)

local config = {
    monstersNames = {"Rotworm", "Rat"},
    maxGold = 100
}
.
.
rest of script
 
idk.
instead of using 'monster' use a different name? :p
Code:
local targetMonster = target:getMonster()
if not targetMonster then
     return true
end

(For future posts, can you post your current script, and any errors you get?
Kind of hard to troubleshoot based on what we think the script looks like atm.)
 
Okay this is the current script (working but still need some things)

Code:
local value = math.random(0, 10)

function onKill(player, target, mostDamageKiller)

    if getCreatureName(target) == "Rat" then
    player:setBankBalance(player:getBankBalance() + value)
    player:sendTextMessage(MESSAGE_STATUS_DEFAULT, value)

    end
    return true
    end

when you kill a "RAT" this script add to your balance a math number 0-10 and give a default message saying the number but if you kill more rats all give the same amount of the first__ if i close and open the server or use /reload creaturescript the number change and stuck again
 
Try this.
Code:
local monsters = {"rat", "cave rat"}

function onKill(creature, target)
    local targetMonster = target:getMonster()
    if not targetMonster then
        return true
    end

    if not isInArray(monsters, targetMonster:getName():lower()) then
        return true
    end

    local value = math.random(0, 10)

    player:setBankBalance(player:getBankBalance() + value)
    player:sendTextMessage(MESSAGE_STATUS_DEFAULT, value)

    return true
end
 
attempt to call method "getmonster" (a nil value)

Code:
function onKill(player, target, mostDamageKiller)

local value = math.random(0, 10)

    if getCreatureName(target) == "Rat" then
    player:setBankBalance(player:getBankBalance() + value)
    player:sendTextMessage(MESSAGE_STATUS_DEFAULT, value)

    end
    return true
    end

this work! only need to use local value before function like you say thanks, now i need to edit the player:sendtext.. (to say correct message XXX gold coins gained in the battle) and add local config ( monsters)
 
Last edited:
attempt to call method "getmonster" (a nil value)
Sorry, I'll stop using stuff copied from vankk. lmao
Code:
local monsters = {"rat", "cave rat"}

function onKill(creature, target)
    if not target:isMonster() then
        return true
    end

    if not isInArray(monsters, target:getName():lower()) then
        return true
    end

    local value = math.random(0, 10)

    player:setBankBalance(player:getBankBalance() + value)
    player:sendTextMessage(MESSAGE_STATUS_DEFAULT, value)

    return true
end
 
How i can add a math.random for each creature name?
Just going to put this out there, that I'm not a great scripter. :p
But this is how I would personally do it.
Code:
function onKill(creature, target)
    if not target:isMonster() then
        return true
    end

    local monsters = {
        [1] = { name = "rat",      min_gold = 1, max_gold = 100 },
        [2] = { name = "cave rat", min_gold = 1, max_gold = 100 }
    }

    for i = 1, #monsters do
        if monsters[i].name == target:getName():lower() then
            local value = math.random(monsters[i].min_gold, monsters[i].max_gold)
            player:setBankBalance(player:getBankBalance() + value)
            player:sendTextMessage(MESSAGE_STATUS_DEFAULT, value)
            break
        end
    end

    return true
end
 
Back
Top