• 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 Add script/function on TFS 1.5

absorc

Ask-her
Joined
Oct 7, 2013
Messages
57
Reaction score
4
Location
Sweden
I can't seem to understand how to add a new script/function to my server.
If we take "Killing in the name of" quest as an example. There you need to kill x amount of monsters to reach the reward.

I just want that when you kill 10x rats you gain a additional 1000exp (one time only) then when you reach 50 rats you gain 5000exp.
Also if there is to make a similar. Kill any monsters x times to get additional exp.
I can try to write it myself if nobody has the time to help out with the code but I just need help to know which folders to apply this.
Movements?
Actions?

Regards,
Absorc
 
data/creaturescripts folder
or
if using the revscript system you'd use data/scripts

Here's a revscript example for your script idea.
Lua:
local monsterKillBonuses = {
    ["rat"] = { -- make sure the creature name is lowercase
        storage = 45001,
        maxCount = 50, -- just so the script doesn't count forever
        stages = {
            [10] = {experience = 1000},
            [50] = {experience = 5000}
        }
    },
    ["demon"] = {
        storage = 45002,
        maxCount = 50,
        stages = {
            [10] = {experience = 10000},
            [50] = {experience = 50000}
        }
    }
}

local creatureevent = CreatureEvent("onKill_MonsterKillBonuses")

function creatureevent.onKill(player, creature)
    if not Player(player) then
        return true
    end
    if not Monster(creature) then
        return true
    end
    
    local index = monsterKillBonuses[creature:getName():lower()]
    if not index then
        return true
    end
    
    local storage = player:getStorageValue(index.storage)
    if storage == index.maxCount then
        return true
    end
    storage = storage > 0 and storage + 1 or 1
    
    index = index.stages[storage]
    if index then
        player:addExperience(index.experience)
        player:sendTextMessage(MESSAGE_STATUS_DEFAULT, "You've received " .. index.experience .. " bonus experience for killing " .. storage .. " " .. creature:getName():lower() .. "s!")
    end
    return true
end

creatureevent:register()



local creatureevent = CreatureEvent("onLogin_MonsterKillBonuses")

function creatureevent.onLogin(player)
    player:registerEvent("onKill_MonsterKillBonuses")
    return true
end

creatureevent:register()
 
Thank you for this @Xikini ! I will try it right away.
Quick question tho.
The storage, it needs to be something that has not been used before right?

Regards,
Absorc

P.s gonna post the results here soon.
Killed 5 or more rats, didn't get any additional exp.
I feel like I should add this to the actions.xml aswell? Need some guidance and I'll remember how to do it correctly in the future!
Thanks.
 
Last edited:
Thank you for this @Xikini ! I will try it right away.
Quick question tho.
The storage, it needs to be something that has not been used before right?

Regards,
Absorc

P.s gonna post the results here soon.
Killed 5 or more rats, didn't get any additional exp.
I feel like I should add this to the actions.xml aswell? Need some guidance and I'll remember how to do it correctly in the future!
Thanks.
5 rats isn't 10. xD
Kill 10 rats -> gain 1000 experience bonus (1 time reward)
Kill 50 rats -> gain 5000 experience bonus (1 time reward)

It doesn't give 100 experience 10 times. It gives 1000 experience, one time, after killing the 10th rat.

If that isn't what you wanted, you'll have to modify the script to make it do what you want.

--

Actions -> anything you click with your mouse to do something. (Think of a shovel or a lever)
Creaturescripts -> Generally anything to do with a monster or player. (onLogin, onKill, onDeath)
Globalevents -> Anything that happens globally. (onStartUp, onTime)
movements -> for moving items / walking onto tile / equipping items

There's a ton more folders, but that's the main one's.

You can find more information in the OTS guide
 
5 rats isn't 10. xD
Kill 10 rats -> gain 1000 experience bonus (1 time reward)
Kill 50 rats -> gain 5000 experience bonus (1 time reward)

It doesn't give 100 experience 10 times. It gives 1000 experience, one time, after killing the 10th rat.

If that isn't what you wanted, you'll have to modify the script to make it do what you want.

--

Actions -> anything you click with your mouse to do something. (Think of a shovel or a lever)
Creaturescripts -> Generally anything to do with a monster or player. (onLogin, onKill, onDeath)
Globalevents -> Anything that happens globally. (onStartUp, onTime)
movements -> for moving items / walking onto tile / equipping items

There's a ton more folders, but that's the main one's.

You can find more information in the OTS guide
Oh wow. Just realized that I cannot count to 10.
Jokes aside. I just changed your script from 10 to 5 to make the process go faster.

Well I guess now I know why it's not working.
Since this script has to do with monsters then I'll need to add it to creaturescripts for it to work. Hopefully I am on the right path.
Btw thanks for the quick and smart summary of the explanations.

Regards,
Absorc
 
Oh wow. Just realized that I cannot count to 10.
Jokes aside. I just changed your script from 10 to 5 to make the process go faster.

Well I guess now I know why it's not working.
Since this script has to do with monsters then I'll need to add it to creaturescripts for it to work. Hopefully I am on the right path.
Kinda. If you converted the revscript into the base login/onKill methods, then yeah you'd put it into creaturescripts folder.

If you want to use it as-is, as the revscript, you'd want to put it into data/scripts

 
Kinda. If you converted the revscript into the base login/onKill methods, then yeah you'd put it into creaturescripts folder.

If you want to use it as-is, as the revscript, you'd want to put it into data/scripts

That was my attempt. To put it into data/scripts, as it is. Didn't work.
Reading the link you posted about revscriptsys. Hmm.. Guess it should've worked but it didn't. Neither did I get any errors in the console.
Tried the following folders:
data/scripts/creaturescripts
data/scripts/monsters
data/scripts/quests
 
Last edited:
That was my attempt. To put it into data/scripts, as it is. Didn't work.
Reading the link you posted about revscriptsys. Hmm.. Guess it should've worked but it didn't. Neither did I get any errors in the console.
Alright, let's add some prints and figure out where it's going wrong.

Check your console. :)
Lua:
local monsterKillBonuses = {
    ["rat"] = { -- make sure the creature name is lowercase
        storage = 45001,
        maxCount = 50, -- just so the script doesn't count forever
        stages = {
            [10] = {experience = 1000},
            [50] = {experience = 5000}
        }
    },
    ["demon"] = {
        storage = 45002,
        maxCount = 50,
        stages = {
            [10] = {experience = 10000},
            [50] = {experience = 50000}
        }
    }
}

local creatureevent = CreatureEvent("onKill_MonsterKillBonuses")

function creatureevent.onKill(player, creature)
    print("----------")
    print("onKill event triggered.")
    if not Player(player) then
        print("Creature activating event is not a player. (Died or no longer logged in is most probable.)")
        return true
    end
    if not Monster(creature) then
        print("Creature killed is not a monster.")
        return true
    end
    
    print("A " .. creature:getName():lower() .. " was killed.")
    local index = monsterKillBonuses[creature:getName():lower()]
    if not index then
        print("This creature is not in the monsterKillBonuses table.")
        return true
    end
    
    local storage = player:getStorageValue(index.storage)
    print("current storage value -> " ..  storage)
    if storage == index.maxCount then
        print("Max storage value previously reached.")
        return true
    end
    storage = storage > 0 and storage + 1 or 1
    
    print("New storage value -> " .. storage)
    index = index.stages[storage]
    if index then
        print("stage " .. storage .. " found. Giving experience.")
        player:addExperience(index.experience)
        player:sendTextMessage(MESSAGE_STATUS_DEFAULT, "You've received " .. index.experience .. " bonus experience for killing " .. storage .. " " .. creature:getName():lower() .. "s!")
    else
        print("stage " .. storage .. " NOT found. (Not in table, therefore no experience bonus to be given.)")
    end
    return true
end

creatureevent:register()



local creatureevent = CreatureEvent("onLogin_MonsterKillBonuses")

function creatureevent.onLogin(player)
    print("Player registered for onKill event.")
    player:registerEvent("onKill_MonsterKillBonuses")
    return true
end

creatureevent:register()
 
Alright, let's add some prints and figure out where it's going wrong.

Check your console. :)
Lua:
local monsterKillBonuses = {
    ["rat"] = { -- make sure the creature name is lowercase
        storage = 45001,
        maxCount = 50, -- just so the script doesn't count forever
        stages = {
            [10] = {experience = 1000},
            [50] = {experience = 5000}
        }
    },
    ["demon"] = {
        storage = 45002,
        maxCount = 50,
        stages = {
            [10] = {experience = 10000},
            [50] = {experience = 50000}
        }
    }
}

local creatureevent = CreatureEvent("onKill_MonsterKillBonuses")

function creatureevent.onKill(player, creature)
    print("----------")
    print("onKill event triggered.")
    if not Player(player) then
        print("Creature activating event is not a player. (Died or no longer logged in is most probable.)")
        return true
    end
    if not Monster(creature) then
        print("Creature killed is not a monster.")
        return true
    end
   
    print("A " .. creature:getName():lower() .. " was killed.")
    local index = monsterKillBonuses[creature:getName():lower()]
    if not index then
        print("This creature is not in the monsterKillBonuses table.")
        return true
    end
   
    local storage = player:getStorageValue(index.storage)
    print("current storage value -> " ..  storage)
    if storage == index.maxCount then
        print("Max storage value previously reached.")
        return true
    end
    storage = storage > 0 and storage + 1 or 1
   
    print("New storage value -> " .. storage)
    index = index.stages[storage]
    if index then
        print("stage " .. storage .. " found. Giving experience.")
        player:addExperience(index.experience)
        player:sendTextMessage(MESSAGE_STATUS_DEFAULT, "You've received " .. index.experience .. " bonus experience for killing " .. storage .. " " .. creature:getName():lower() .. "s!")
    else
        print("stage " .. storage .. " NOT found. (Not in table, therefore no experience bonus to be given.)")
    end
    return true
end

creatureevent:register()



local creatureevent = CreatureEvent("onLogin_MonsterKillBonuses")

function creatureevent.onLogin(player)
    print("Player registered for onKill event.")
    player:registerEvent("onKill_MonsterKillBonuses")
    return true
end

creatureevent:register()
Btw thanks for the help. Appreciate it a lot!

onKill event triggered.
A rat was killed.
current storage value -> -1
New storage value -> 1
stage 1 NOT found. (Not in table, therefore no experience bonus to be given.)
 
Btw thanks for the help. Appreciate it a lot!

onKill event triggered.
A rat was killed.
current storage value -> -1
New storage value -> 1
stage 1 NOT found. (Not in table, therefore no experience bonus to be given.)
I see. lol
Forgot to actually save the storage.

Here you go.
Lua:
local monsterKillBonuses = {
    ["rat"] = { -- make sure the creature name is lowercase
        storage = 45001,
        maxCount = 50, -- just so the script doesn't count forever
        stages = {
            [10] = {experience = 1000},
            [50] = {experience = 5000}
        }
    },
    ["demon"] = {
        storage = 45002,
        maxCount = 50,
        stages = {
            [10] = {experience = 10000},
            [50] = {experience = 50000}
        }
    }
}

local creatureevent = CreatureEvent("onKill_MonsterKillBonuses")

function creatureevent.onKill(player, creature)
    print("----------")
    print("onKill event triggered.")
    if not Player(player) then
        print("Creature activating event is not a player. (Died or no longer logged in is most probable.)")
        return true
    end
    if not Monster(creature) then
        print("Creature killed is not a monster.")
        return true
    end
    
    print("A " .. creature:getName():lower() .. " was killed.")
    local index = monsterKillBonuses[creature:getName():lower()]
    if not index then
        print("This creature is not in the monsterKillBonuses table.")
        return true
    end
    
    local storage = player:getStorageValue(index.storage)
    print("current storage value -> " ..  storage)
    if storage == index.maxCount then
        print("Max storage value previously reached.")
        return true
    end
    storage = storage > 0 and storage + 1 or 1
    player:setStorageValue(index.storage, storage)
    
    print("New storage value -> " .. storage)
    index = index.stages[storage]
    if index then
        print("stage " .. storage .. " found. Giving experience.")
        player:addExperience(index.experience)
        player:sendTextMessage(MESSAGE_STATUS_DEFAULT, "You've received " .. index.experience .. " bonus experience for killing " .. storage .. " " .. creature:getName():lower() .. "s!")
    else
        print("stage " .. storage .. " NOT found. (Not in table, therefore no experience bonus to be given.)")
    end
    return true
end

creatureevent:register()



local creatureevent = CreatureEvent("onLogin_MonsterKillBonuses")

function creatureevent.onLogin(player)
    print("Player registered for onKill event.")
    player:registerEvent("onKill_MonsterKillBonuses")
    return true
end

creatureevent:register()
 
I see. lol
Forgot to actually save the storage.

Here you go.
Lua:
local monsterKillBonuses = {
    ["rat"] = { -- make sure the creature name is lowercase
        storage = 45001,
        maxCount = 50, -- just so the script doesn't count forever
        stages = {
            [10] = {experience = 1000},
            [50] = {experience = 5000}
        }
    },
    ["demon"] = {
        storage = 45002,
        maxCount = 50,
        stages = {
            [10] = {experience = 10000},
            [50] = {experience = 50000}
        }
    }
}

local creatureevent = CreatureEvent("onKill_MonsterKillBonuses")

function creatureevent.onKill(player, creature)
    print("----------")
    print("onKill event triggered.")
    if not Player(player) then
        print("Creature activating event is not a player. (Died or no longer logged in is most probable.)")
        return true
    end
    if not Monster(creature) then
        print("Creature killed is not a monster.")
        return true
    end
   
    print("A " .. creature:getName():lower() .. " was killed.")
    local index = monsterKillBonuses[creature:getName():lower()]
    if not index then
        print("This creature is not in the monsterKillBonuses table.")
        return true
    end
   
    local storage = player:getStorageValue(index.storage)
    print("current storage value -> " ..  storage)
    if storage == index.maxCount then
        print("Max storage value previously reached.")
        return true
    end
    storage = storage > 0 and storage + 1 or 1
    player:setStorageValue(index.storage, storage)
   
    print("New storage value -> " .. storage)
    index = index.stages[storage]
    if index then
        print("stage " .. storage .. " found. Giving experience.")
        player:addExperience(index.experience)
        player:sendTextMessage(MESSAGE_STATUS_DEFAULT, "You've received " .. index.experience .. " bonus experience for killing " .. storage .. " " .. creature:getName():lower() .. "s!")
    else
        print("stage " .. storage .. " NOT found. (Not in table, therefore no experience bonus to be given.)")
    end
    return true
end

creatureevent:register()



local creatureevent = CreatureEvent("onLogin_MonsterKillBonuses")

function creatureevent.onLogin(player)
    print("Player registered for onKill event.")
    player:registerEvent("onKill_MonsterKillBonuses")
    return true
end

creatureevent:register()
I get some other text in my console.
It says: Xikini is a G!! Thanks a bunch. Now let me see... How do I remove the text from appearing in the console? Remove print?
 
I see. lol
Forgot to actually save the storage.

Here you go.
Lua:
local monsterKillBonuses = {
    ["rat"] = { -- make sure the creature name is lowercase
        storage = 45001,
        maxCount = 50, -- just so the script doesn't count forever
        stages = {
            [10] = {experience = 1000},
            [50] = {experience = 5000}
        }
    },
    ["demon"] = {
        storage = 45002,
        maxCount = 50,
        stages = {
            [10] = {experience = 10000},
            [50] = {experience = 50000}
        }
    }
}

local creatureevent = CreatureEvent("onKill_MonsterKillBonuses")

function creatureevent.onKill(player, creature)
    print("----------")
    print("onKill event triggered.")
    if not Player(player) then
        print("Creature activating event is not a player. (Died or no longer logged in is most probable.)")
        return true
    end
    if not Monster(creature) then
        print("Creature killed is not a monster.")
        return true
    end
   
    print("A " .. creature:getName():lower() .. " was killed.")
    local index = monsterKillBonuses[creature:getName():lower()]
    if not index then
        print("This creature is not in the monsterKillBonuses table.")
        return true
    end
   
    local storage = player:getStorageValue(index.storage)
    print("current storage value -> " ..  storage)
    if storage == index.maxCount then
        print("Max storage value previously reached.")
        return true
    end
    storage = storage > 0 and storage + 1 or 1
    player:setStorageValue(index.storage, storage)
   
    print("New storage value -> " .. storage)
    index = index.stages[storage]
    if index then
        print("stage " .. storage .. " found. Giving experience.")
        player:addExperience(index.experience)
        player:sendTextMessage(MESSAGE_STATUS_DEFAULT, "You've received " .. index.experience .. " bonus experience for killing " .. storage .. " " .. creature:getName():lower() .. "s!")
    else
        print("stage " .. storage .. " NOT found. (Not in table, therefore no experience bonus to be given.)")
    end
    return true
end

creatureevent:register()



local creatureevent = CreatureEvent("onLogin_MonsterKillBonuses")

function creatureevent.onLogin(player)
    print("Player registered for onKill event.")
    player:registerEvent("onKill_MonsterKillBonuses")
    return true
end

creatureevent:register()

Is there anyway to include all monsters without typing them into the script?

Regards,
Absorc
 
I get some other text in my console.
It says: Xikini is a G!! Thanks a bunch. Now let me see... How do I remove the text from appearing in the console? Remove print?
Yes. remove the lines or greentext them. Both ways would remove them from the console.
Lua:
print("testing.")
-- print("testing.") -- green texted code

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

Is there anyway to include all monsters without typing them into the script?

Regards,
Absorc
Kind of.

The script already includes all monsters, because of the onKill trigger attached to the player.
The player killing something is what triggers it.
All players/npcs/monsters who die because of the players action will trigger it.

Currently, this section of code is 'filtering out' all creatures who are not in the table.
Lua:
local index = monsterKillBonuses[creature:getName():lower()]
if not index then
    print("This creature is not in the monsterKillBonuses table.")
    return true
end

If you were to remove this part of the code, all creatures would 'go through'.
Or you could do the 'all creatures' checks before this part of the code..
Or just make another onKill script entirely to encompass the separate idea.
 
Yes. remove the lines or greentext them. Both ways would remove them from the console.
Lua:
print("testing.")
-- print("testing.") -- green texted code

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

Kind of.

The script already includes all monsters, because of the onKill trigger attached to the player.
The player killing something is what triggers it.
All players/npcs/monsters who die because of the players action will trigger it.

Currently, this section of code is 'filtering out' all creatures who are not in the table.
Lua:
local index = monsterKillBonuses[creature:getName():lower()]
if not index then
    print("This creature is not in the monsterKillBonuses table.")
    return true
end

If you were to remove this part of the code, all creatures would 'go through'.
Or you could do the 'all creatures' checks before this part of the code..
Or just make another onKill script entirely to encompass the separate idea.
Thank you. I was trying different ways with that before you posted here again and did not get it to work.
Also tried to give the player money, it worked but only 1x... couldn't get the count to work. It just shows how little I know.
That's why I'm reading the Lua scripting guide atm and after that I'll jump to the C++ Tutorials in the forum.
 
Back
Top