How to write your own Script in Lua
today I want to show you how to write your own Lua Script. This tutorial is only for beginner who haven't any clue how to write a Script.
I know it's not easy at all the start but I promise you, you'll be able to write your own simple Script after you have read this tutorial.
![[Image: Requirements.jpg]](https://s32.postimg.org/gaphpbpid/Requirements.jpg)
![[Image: 00d20f770def4e349d223cc899f8e43b.png]](http://image.prntscr.com/image/00d20f770def4e349d223cc899f8e43b.png)
The begin of your own Script
Every script starts with the name of the script, the author of the script and the description of the script.
Easy, isn't it? But this is not all what you have to do. There is much more to do, so let's go.
In the future you have to include two functions on every Script what you're writing, I will describe these two functions a little bit.
function onPathAction()
![[Image: Examples.jpg]](https://s32.postimg.org/mzw15caud/Examples.jpg)
![[Image: Examples.jpg]](https://s32.postimg.org/mzw15caud/Examples.jpg)
function onBattleAction()
![[Image: Examples.jpg]](https://s32.postimg.org/mzw15caud/Examples.jpg)
Our first complete Script would look like this, simple but effective!
Thank you for reading this little tutorial. I hope you have understand how to write your first simple Lua Script. If you got any questions or have any suggestions feel free to ask me.
today I want to show you how to write your own Lua Script. This tutorial is only for beginner who haven't any clue how to write a Script.
I know it's not easy at all the start but I promise you, you'll be able to write your own simple Script after you have read this tutorial.
![[Image: Requirements.jpg]](https://s32.postimg.org/gaphpbpid/Requirements.jpg)
- Notepad ++ - its not necessary but this texteditor includes Syntax Highlighting, so its easier to find bugs in your code (picture below)
Download
- Motivation
- Time
![[Image: 00d20f770def4e349d223cc899f8e43b.png]](http://image.prntscr.com/image/00d20f770def4e349d223cc899f8e43b.png)
The begin of your own Script
Every script starts with the name of the script, the author of the script and the description of the script.
name = "This is the name of your Script, for example >Route 1<"
author = "Put the name of you here, for example >Royal<"
description = "Descripe your Script a little bit, descripe what it do, for example >Leveling in Route 1 and catch every Shiny>"
Easy, isn't it? But this is not all what you have to do. There is much more to do, so let's go.
In the future you have to include two functions on every Script what you're writing, I will describe these two functions a little bit.
function onPathAction()
onPathAction This function will be called on everytime when the bot is moving, swimming or whatever. Without this function the bot wouldn't move so it's fully necessary.
But this function is not enough to move around. You have to include some more functions in this function (I know, its a bit confusing).
You need to have atleast four more functions to write your "moving" part of the Script.
These functions are called:
isPokemonUsable(x)
getMapName()
moveToMap(name of the map)
moveToGrass()
The first function, isPokemonUsable(x), will check if your Pokemon on index 'x' isn't KO and have some offensive Attacks (this will also check if some Power Points are available) if this is true it will return true, if this is false it will return false.
The second function, getMapName(), will check on which map you are. So for example you are in Route 1 it will return "Route 1".
The third function, moveToMap(name of the map), will move to the map which you set inside the "()". But you can't went from Route 1 to Route 5, you have to write a full Path (I'll include a example below).
Last but not least the fourth function, moveToGrass(), you call this function if you are on your desired place. For example you are in Route 2 and call this function the bot will randomly walk in the grass to fight against wild Pokemons.
![[Image: Examples.jpg]](https://s32.postimg.org/mzw15caud/Examples.jpg)
function onPathAction()
if isPokemonUsable(1) then
if getMapName() == "Pokecenter Oldale Town" then
moveToMap("Oldale Town")
elseif getMapName() == "Oldale Town" then
moveToRoute("Route 101")
elseif getMapName() == "Route 101" then
moveToGrass()
end
end
end
onPathAction() But what will happen if your first Pokemon is KO or haven't any Power Points on offensive attacks have left? So, we need to go to the next Pokecenter and heal our Pokemons there, but how?
In that case we need a new function this function is called usePokecenter() but you only can call this function if you are inside a Pokecenter. So, we have to write a function to move to the next Pokecenter.
![[Image: Examples.jpg]](https://s32.postimg.org/mzw15caud/Examples.jpg)
function onPathAction()
if isPokemonUsable(1) then
if getMapName() == "Pokecenter Oldale Town" then
moveToMap("Oldale Town")
elseif getMapName() == "Oldale Town" then
moveToRoute("Route 101")
elseif getMapName() == "Route 101" then
moveToGrass()
end
else
if getMapName() == "Route 101" then
moveToMap("Oldale Town")
elseif getMapName() == "Oldale Town" then
moveToMap("Pokecenter Oldale Town")
elseif getMapName() == "Pokecenter Oldale Town" then
usePokecenter()
end
end
end
onPathAction() So, this is our function onPathAction() it looks nice, isn't it? But as I said at the start this is only the part of our movement. So, how do we write our battle part? Let's go!
function onBattleAction()
onBattleAction() This function will be called everytime when you enter a battle. There are also a few function what you should include in your script.
isWildBattle()
isOpponentShiny()
getActivePokemonNumber()
attack()
run()
sendUsablePokmemon()
I know, there are a few more functions what you should include in the battle part, but its worth it.
First of all we talk about the first function, isWildBattle(), this function will return a true if you are inside a battle against a wild pokemon and return a false if you're not.
The second function, isOpponentShiny(), this function will return a true if you are fighting against a Shiny Pokemon and return a false if you're not.
The third function, getActivePokemonNumber(), will return the index number of your Pokemon where is fighting in the battle.
The fourth function, attack(), will leads your Pokemon to attack the opponent with a offensive move.
The fifth function, run(), will leads you to run away (its necessary if you don't have any offensive moves left).
The sixth function, sendUsablePokmemon(), will send a usable Pokemon into the battle (if your first is KO or haven't any offensive moves left).
![[Image: Examples.jpg]](https://s32.postimg.org/mzw15caud/Examples.jpg)
function onBattleAction()
if isWildBattle() and isOpponentShiny() then
if useItem("Ultra Ball") or useItem("Great Ball") or useItem("Pokeball") then
return
end
end
return attack() or sendUsablePokemon() or run()
end
onBattleAction() So, this is our function onBattleAction() it looks nice, isn't it? There are much more things where you can include, but as I said it's only a simple script not an advanced one.
Our first complete Script would look like this, simple but effective!
name = "Route 101 Level-Script"
author = "Royal"
description = [[Levelling in Route 101 and catch every Shiny Pokemon!]]
function onPathAction()
if isPokemonUsable(1) then
if getMapName() == "Pokecenter Oldale Town" then
moveToMap("Oldale Town")
elseif getMapName() == "Oldale Town" then
moveToMap("Route 101")
elseif getMapName() == "Route 101" then
moveToGrass()
end
else
if getMapName() == "Route 101" then
moveToMap("Oldale Town")
elseif getMapName() == "Oldale Town" then
moveToMap("Pokecenter Oldale Town")
elseif getMapName() == "Pokecenter Oldale Town" then
usePokecenter()
end
end
end
function onBattleAction()
if isWildBattle() and isOpponentShiny() then
if useItem("Ultra Ball") or useItem("Great Ball") or useItem("Pokeball") then
return
end
end
return attack() or sendUsablePokemon() or run() or sendAnyPokemon()
end
Thank you for reading this little tutorial. I hope you have understand how to write your first simple Lua Script. If you got any questions or have any suggestions feel free to ask me.