• 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 Share Exp Lua Script

natanal99

New Member
Joined
May 31, 2011
Messages
67
Reaction score
3
for some reason my script doesn't do anything at all:


Code:
function onDeath(cid, corpse, deathList)
    if (isCreature(cid)) then
        local xp = getMonsterInfo(getCreatureName(cid)).experience
        local rate = 100
        local rate165 = 1000
        local rate210 = 500
        print("xp = " ..xp)

        for _, pid in ipairs(deathList) do
            if isPlayer(pid) then
                print(getCreatureName(pid))
                if getPlayerLevel(pid) < 165 then
                    doPlayerAddExp(pid, exp * rate165)
                elseif getPlayerLevel(pid) < 210 then
                    doPlayerAddExp(pid, exp * rate210)
                else
                    doPlayerAddExp(pid, exp * rate)
                end
               
            end
        end
    end
end

off course i registered in login and made a right tag.
 
First of All,This Script need much effort,registering it in every monster file,so we gonna do a lil edit

to be
1-
Code:
function onDeath(cid, corpse, deathList)

To

Code:
function onKill(cid, target,damage,flags)

2-
Code:
isCreature(cid)

To

Code:
isMonster(target) and isPlayer(cid)

Total Code:
Code:
function onKill(cid, target,damage,flags)
  if (isMonster(target) and isPlayer(cid)) then
  local xp = getMonsterInfo(getCreatureName(target)).experience
  local rate = 100
  local rate165 = 1000
  local rate210 = 500
  print("xp = " ..xp)
  if getPlayerLevel(cid) < 165 then
  doPlayerAddExp(cid, exp * rate165)
  elseif getPlayerLevel(cid) < 210 then
  doPlayerAddExp(cid, exp * rate210)
  else
  doPlayerAddExp(cid, exp * rate)
  end
  end
end

Why your old code didn't work,because you didn't register it to any monster
 
For those who want to use it, i have to return true in the end, otherwise monsters never die, and player keeps gaining exp

Code:
function onKill(cid, target,damage,flags)
    if (isMonster(target) and isPlayer(cid)) then
        local xp = getMonsterInfo(getCreatureName(target)).experience
        local rate = 100
        local rate165 = 1000
        local rate210 = 500
      
        if getPlayerLevel(cid) < 165 then
            doPlayerAddExp(cid, xp * rate165)
            doSendAnimatedText(getCreaturePosition(cid),  xp * rate165, 154)
        elseif getPlayerLevel(cid) < 210 then
            doPlayerAddExp(cid, xp * rate210)
            doSendAnimatedText(getCreaturePosition(cid),  xp * rate210, 154)
        else
            doPlayerAddExp(cid, xp * rate)
            doSendAnimatedText(getCreaturePosition(cid),  xp * rate, 154)
        end
      
    end
return true
end
 
First of All,This Script need much effort,registering it in every monster file,so we gonna do a lil edit

to be
1-
Code:
function onDeath(cid, corpse, deathList)

To

Code:
function onKill(cid, target,damage,flags)

2-
Code:
isCreature(cid)

To

Code:
isMonster(target) and isPlayer(cid)

Total Code:
Code:
function onKill(cid, target,damage,flags)
  if (isMonster(target) and isPlayer(cid)) then
  local xp = getMonsterInfo(getCreatureName(target)).experience
  local rate = 100
  local rate165 = 1000
  local rate210 = 500
  print("xp = " ..xp)
  if getPlayerLevel(cid) < 165 then
  doPlayerAddExp(cid, exp * rate165)
  elseif getPlayerLevel(cid) < 210 then
  doPlayerAddExp(cid, exp * rate210)
  else
  doPlayerAddExp(cid, exp * rate)
  end
  end
end

Why your old code didn't work,because you didn't register it to any monster

It doesn't need much effort you can use this:

Code:
local dir = "./data/monster/"
local csdir = "./data/creaturescripts/"
local monstersXML = "monsters.xml"
local csXML = "creaturescripts.xml"
local csTag = '\n    <event type="%t" name="%n" script="%s"/>' -- Tag format on creaturescripts.xml (%t = type, %n = name, %s = script)
local defaultText =
[[function onCreatureAppear(self, creature) return false end
function onCreatureDisappear(self, creature) return false end
function onCreatureMove(self, creature, oldPosition, newPosition) return false end
function onCreatureSay(self, creature, type, message) return false end
function onThink(self, interval) return false end]]


local events = {
["monsterdeath"] = {type = "death", file="monsterdeath.lua", defaultText=[[
    function onDeath(cid, corpse, lasthitkiller, mostdamagekiller, lasthitunjustified, mostdamageunjustified)
        return true
    end]]},
}


------- Do not edit after this if you don't know what you are doing.

function parseXML(file)
    local ret = {["monster"] = {}, ["event"] = {}}
    local data = ""
    for line in file:lines() do
        data = data .. line .. "\n"
        line = line:match("<(.-)/>")
        if line then
            local element = line:match("^%s*(.-)%s")
            if element == "monster" or element == "event" then
                local t = {}
                for i, x in line:gmatch('%s+(.-)="(.-)"') do
                    t[i] = x
                end
                table.insert(ret[element], t)
            end
        end
    end
    return ret, data:sub(1, #data-1)
end

function init()
    ----- Parsing monsters.xml -----
    local monstersfile = io.open(dir .. monstersXML, "r")
    print("Parsing XML " .. dir .. monstersXML .. "...")
    local xmltable = parseXML(monstersfile)
    monstersfile:close()
    --------------------------------

    -- Creating scripts directory --

    local testfile = io.open(dir .. "scripts/test", "w")
    if not testfile then
        os.execute("mkdir data\\monster\\scripts")
        print("Directory " .. dir .. "scripts has been created.")
    else
        testfile:close()
    end
    os.remove(dir .. "scripts/test")

    ---------------------------------

    ------ Creating default.lua -------
    local defaultlua = io.open(dir .. "scripts/default.lua", "w")
    defaultlua:write(defaultText)
    defaultlua:close()
    print("File " .. dir .. "scripts/default.lua has been created.")
    ---------------------------------

    ---- Modify all monsters XML! ----
    local modified = 0
    for i, contents in ipairs(xmltable["monster"]) do
        local monsterxml = io.open(dir .. contents.file, "r")
        if monsterxml then
            local xmldata = monsterxml:read("*a")
            local _mod = false
            local s, f, content = xmldata:find("<monster%s(.-)>")
            local r, p, cs = content:find('script="(.-)"')
            monsterxml:close()
            newc = content
            if not cs then
                xmldata = xmldata:gsub(content, newc .. ' script="default.lua"')
                _mod = true
            elseif cs ~= "default.lua" then
                print('Warning: The file "' .. contents.file .. '" already has a script.')
            end

            local s, f, content = xmldata:find("<monster%s(.-)>")
            local x, q, scriptcontent = xmldata:find("<script>(.-)</script>")
            local readyevents = {}
            local addevents = ""
            if scriptcontent then
                for line in scriptcontent:gmatch("(.-)\n") do
                    local event = line:match('<event%s-name="(.-)"')
                    if event then
                        readyevents[event] = true
                    end
                end
            end
            for event, _c in pairs(events) do
                if not readyevents[event] then
                    addevents = addevents .. '<event name="' .. event .. '"/>\n'
                end
            end
            if addevents ~= "" then
                _mod = true
                if scriptcontent and scriptcontent:find("event") then
                    xmldata = xmldata:gsub(scriptcontent, scriptcontent .. addevents)
                elseif x and q then
                    xmldata = xmldata:sub(1, x-1) .. xmldata:sub(q+1)
                    xmldata = xmldata:sub(1, f) .. "\n<script>\n" .. addevents .. "\n</script>" .. xmldata:sub(f+1)
                else
                    xmldata = xmldata:sub(1, f) .. "\n<script>\n" .. addevents .. "\n</script>" .. xmldata:sub(f+1)
                end
            end
            if xmldata then
                if _mod then
                    modified = modified+1
                end
                local monsterxmlwrite = io.open(dir .. contents.file, "w")
                monsterxmlwrite:write(xmldata)
                monsterxmlwrite:close()
            else
                print("Error in: " .. contents.name)
            end
        else
            print("Warning: Error opening file " .. contents.file)
        end
    end
    print("Total monsters XML modified: " .. modified)
    ----------------------------------

    --- Parsing creaturescripts.xml --

    local csfile = io.open(csdir .. csXML, "r")
    print("Parsing XML " .. csdir .. csXML .. "...")
    local csxmltable, xmldata = parseXML(csfile)
    csfile:close()

    ----------------------------------

    --- Modify creaturescripts.xml ---
    local _mod = false
    local readytags = {}
    for i, contents in ipairs(csxmltable["event"]) do
        if events[contents.name] then
            readytags[contents.name] = true
        end
    end

    for event, contents in pairs(events) do
        if not readytags[event] then
            _mod = true
            local currenttag = csTag
            currenttag = currenttag:gsub("%%t", contents.type)
            currenttag = currenttag:gsub("%%n", event)
            currenttag = currenttag:gsub("%%s", contents.file)
            local r, w = xmldata:find("<creaturescripts>")
            xmldata = xmldata:sub(1, w) .. currenttag .. xmldata:sub(w+1)
        end
    end

    if _mod then
        local csfilewrite = io.open(csdir .. csXML, "w")
        csfilewrite:write(xmldata)
        csfilewrite:close()
        print("Modified " .. csdir .. csXML)
    end

    ----------------------------------

    ----- Creating event scripts -----

    print("Creating lua creaturescripts...")
    for event, contents in pairs(events) do
        local luafile = io.open(csdir .. "scripts/" .. contents.file, "w")
        luafile:write(contents.defaultText)
        luafile:close()
    end
    ----------------------------------
    print("All the modifications were done.")
    return true
end

local check = io.open("meventsinstall.lua", "r")


if not check then
    local ret = init()
    if ret then
        local file = io.open("meventsinstall.lua", "w")
        file:write("true")
        file:close()
    end
else
    check:close()
end

Adding this to the lib should install the desired event in every monster.
 
Back
Top