Hello There, Guest! Login Register


How to write your own Script in Lua
#1
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]
  • 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]

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]

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]

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]

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.
 
Reply
#2
Nice, ty Royal, you just miss write: "The begin of your own Skript" Skript hahaha
"No blood. No sticky, hot, messy, awful blood; no blood at all. Why hadn't I thought of that? No blood. What a beautiful idea!" ~~ Dexter Morgan
 
Reply
#3
BestKiller Nice, ty Royal, you just miss write: "The begin of your own Skript" Skript hahaha

My fault. In German its „Skript“. Tongue
 
Reply
#4
How do i make this work in caves
 
Reply
#5
treck How do i make this work in caves

In that case:

moveToGrass()

Won't work, you need to use:

moveToRectangle(minX, minY, maxX, maxY)

You get the coordinates (x and y) ingame if you use the command "/loc".
 
Reply
#6
Ok thx
 
Reply
#7
Sublime text 3.0
 
Reply
#8
Hello plz can u tell how to buy pokeballs?? i know i must use hasItem(itemName) and getItemQuantity(itemName) but i dont know how to get it right!!
 
Reply
#9
talkToNpc("npcname")
buyItem("Poke Ball", 307) -- Buys 307 Poke Ball
https://api.proshine-bot.com/#buyitem
penguins
 
Reply
#10
thanks a lot
 
Reply
#11
[Image: Update_Royal.jpg]

If you have any suggestions or questions fell free to talk with me. Smile
 
Reply
#12
it does not work when the pokemon move this KO the script stops
solutions ?
my code: route 11

name = "Route 11 by Alex"
author = "by: Alexandre"
description = "Atack a route 11 whit 6 pokemon>"


function onPathAction()
if isPokemonUsable(1) or isPokemonUsable(2) or isPokemonUsable(3) or isPokemonUsable(4) then
                if getMapName() == "Pokecenter Vermilion" then
                    moveToMap("Vermilion City")
                elseif getMapName() == "Vermilion City" then
                    moveToMap("Route 11")
                elseif getMapName() == "Route 11" then
                    moveToGrass()
                end
            else
                if getMapName() == "Route 11" then
                    moveToMap("Vermilion City")
                elseif getMapName() == "Vermilion City" then
                    moveToMap("Pokecenter Vermilion")
                elseif getMapName() == "Pokecenter Vermilion" then
                    usePokecenter()
                end
end
end

function onBattleAction()
    if isWildBattle() and isOpponentShiny() then
        if useItem("Ultra Ball") or useItem("Pokeball") or useItem("Pokeball") then
            return
        end
    end
    if getActivePokemonNumber() >= 1 then
        return attack() or sendUsablePokemon() or run() or sendAnyPokemon()
    end
end
 
Reply
#13
as restarting the script if you stop?
[2:40:10] Bot stop ?
 
Reply
#14
AlexanderTMz as restarting the script if you stop?
[2:40:10] Bot stop ?

At the moment I'm in school. I'll contact you later this day.

Could you join nn Discord? It's easier to communicate with each other.
 
Reply
#15
how do i find which cell is on map for moveToCell function ?
 
Reply
#16
use the "/loc" command ingame
 
Reply
#17
Helped me a lot, thx
 
Reply
#18
How do I capture a specific pokemon? and have anyway to put a minimum level?

exemple... Catch a Pigey level 10 or more

I spotted this only after, but even there some details are missing ...

I do not spotted as leaving the text to capture a specific pokemon ... Is it there or misread?

Sorry, I do not speak very good English and sometimes lose part of the text.
 
Reply
#19
It only shows how to create a script. It does not give every possible cases.
What's your looking for is here (Here to be exact)
Please take in mind the api documentation isn't complete.
Here is the full list of API Functions
I do NOT publish script and don't accept script requests.
I just help people do their own scripts.
 
Reply
#20
(2016-08-28, 23:12:54)Someone It only shows how to create a script. It does not give every possible cases.
What's your looking for is here (Here to be exact)
Please take in mind the api documentation isn't complete.
Here is the full list of API Functions

Tks.. now i go read all!!!
Big Grin Big Grin Big Grin Big Grin Big Grin Big Grin
 
Reply
#21
Help, when pokemon runout PP i want go back pokecenter how i write it, it only die and back die and back, i cant save money. plz help me : (
 
Reply
#22
(2016-08-31, 07:41:40)drchinsu Help, when pokemon runout PP i want go back pokecenter how i write it, it only die and back die and back, i cant save money. plz help me : (

isPokemonUsable(index)
Checks for healthstatus and PP
No support for scripts from other authors or edited scripts ( except config.lua ). If you change something on the "core" and it isnt working anymore then you can fix it by your own.
Feel free to contact me for informations and tips !

 
Reply
#23
Hi, so im trying to write some scripts of my own, and while i did succeed to do some stuffs, i cant seem to get the SortTeamByLevelAscending() working properly, i tried putting it everywhere (in any place) but it always gives me an error, "the script can only execute one action per frame" or something like that, i would like some advice on where to put it.
 
Reply
#24
(2016-09-05, 01:10:54)Nikaniko Hi, so im trying to write some scripts of my own, and while i did succeed to do some stuffs, i cant seem to get the SortTeamByLevelAscending() working properly, i tried putting it everywhere (in any place) but it always gives me an error, "the script can only execute one action per frame" or something like that, i would like some advice on where to put it.

if isTeamSortedByLevelAscending() then
    -- do stuff
else
    sortTeamByLevelAscending()
end

enjoy Big Grin
No support for scripts from other authors or edited scripts ( except config.lua ). If you change something on the "core" and it isnt working anymore then you can fix it by your own.
Feel free to contact me for informations and tips !

 
Reply
#25
function onPathAction ()
    if isPokemonUsable(1) then
        if getMapName () == "Pokecenter Lilycove" then
            moveToMap ("Lilycove City")
        elseif getMapName () == "Lilycove City" then
            moveToMap ("Route 121")
        elseif getMapName () == "Route 121"
            moveToMap ("Hoenn Safari Zone Lobby")
        elseif getMapName () == "Hoenn Safari Zone Lobby" then
            moveToMap ("Hoenn Safari Zone 1")
        elseif getMapName () == "Hoenn Safari Zone 1" then
            moveToGrass ()
        end
    else
        if getMapName () == "Hoenn Safari Zone Lobby" then
            moveToMap "Route 121"
        elseif getMapName () == "Route 121" then
            moveToMap ("Lilycove City")
        elseif getMapName () == "Lilycove City" then
            moveToMap ("Pokecenter Lilycove")
        elseif getMapName () == "Pokecenter Lilycove" then
            usePokecenter ()

function onBattleAction ()
    if isWildBattle () and isOpponentShiny () or getOpponentName () == "Treecko" then
        if useItem ("Ultra Ball") or useItem ("Great Ball") or useItem ("Pokeball") then
            return
        end
    end
    return sendUsablePoemon () or run()
end

Am I a retard or what?

[20:38:02] Could not load script Treecko Pudding.txt:
chunk_1: (12,3-12): unexpected symbol near 'moveToMap'
 
Reply
  


Forum Jump:


Browsing: 1 Guest(s)