• 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] Learn how to make talkaction script!

Cornex

Web Developer
Staff member
Global Moderator
Joined
Jun 15, 2008
Messages
3,444
Solutions
5
Reaction score
1,166
Location
Sweden
Talkactions!

Code:
[COLOR=#333333]function onSay(cid, words, param)[/COLOR]


Ok, for this function I'm going to, just like the action one, make step by step a nice, working sript.
Action means, if a player uses something... something will happen.
Talkactions means: if a player says something... something will happen.

What shall we make this time??? Hmmm.....
[think]...[/think] I got it!
What about a script, if you say something, you'll get an item, and money from your backpack will be removed.
But which item??? Hmmm.....
[think]...[/think] An amulet of loss! People use it many times, so this command will make it faster for them to get one.

Ok here we go...!

The beginning:

Code:
[COLOR=#333333]function onSay(cid, words, param)[/COLOR]
In order to get the aol, the player has to give money. To do that, we use this command:
-doPlayerRemoveMoney(cid,count)

We have to change this command a bit, so the player has to give 15k, or: 15.000 gold, so:
(NOTE THAT YOU CAN'T PUT 15K INSTEAD OF 15000! ALWAYS IN GOLD COINS!)

Code:
[COLOR=#B1B100]function[/COLOR][COLOR=#333333] onSay[/COLOR][COLOR=#66CC66]([/COLOR][COLOR=#333333]cid, words, param[/COLOR][COLOR=#66CC66])[/COLOR]
[COLOR=#B1B100]if[/COLOR][COLOR=#333333] doPlayerRemoveMoney[/COLOR][COLOR=#66CC66]([/COLOR][COLOR=#333333]cid,[/COLOR][COLOR=#CC66CC]15000[/COLOR][COLOR=#66CC66])[/COLOR][COLOR=#66CC66]==[/COLOR][COLOR=#333333] TRUE [/COLOR][COLOR=#B1B100]then[/COLOR]
Did you see I put if before the command? It just says, if the player gives 15000 gp... then something will happen.
Also, I put == TRUE after the command. That is so the server checks if it's true which means, if the player really gives money... if I put FALSE, then the player can get as many aols as he want, because they're for free then :p

Then, we want the player to get an amulet of loss.. the itemID if an aol is 2173. To give the player an item, use this command:
-doPlayerAddItem(cid,itemid,count or type)

Now we have to change the command a bit before we add it to our script. The itemid is 2173, and the count? Only 1 right.. so:
-doPlayerAddItem(cid,2173,1)

In our script:

Code:
[COLOR=#B1B100]function[/COLOR][COLOR=#333333] onSay[/COLOR][COLOR=#66CC66]([/COLOR][COLOR=#333333]cid, words, param[/COLOR][COLOR=#66CC66])[/COLOR]
[COLOR=#B1B100]if[/COLOR] doPlayerRemoveMoney[COLOR=#66CC66]([/COLOR]cid,[COLOR=#CC66CC]15000[/COLOR][COLOR=#66CC66])[/COLOR] [COLOR=#66CC66]==[/COLOR] [COLOR=#CC66CC]1[/COLOR] [COLOR=#B1B100]then 
[/COLOR][COLOR=#333333] doPlayerAddItem[/COLOR][COLOR=#66CC66]([/COLOR][COLOR=#333333]cid,[/COLOR][COLOR=#CC66CC]2173[/COLOR][COLOR=#333333],[/COLOR][COLOR=#CC66CC]1[/COLOR][COLOR=#66CC66])[/COLOR]
Now we can end the script by placing an "end" at the end, but this one would be really boring. Why won't we send a text to the player, a magic effect and put a cancel if the player doesn't have enough money? That would me more awesome.
biggrin.gif

To send a magic effect, you'll need this command:
-doSendMagicEffect(position,type)

Now we get to something new. Where it says position, we have to put the player's position. But were not going to place like 1000000 coordinates... We have a special command for that:
-getPlayerPosition(cid)

With this command, it doesn't matter where the player is standing, it'll always work. So we have to put this command into the other, where it says position:
-doSendMagicEffect(getPlayerPosition(cid),type)

Now there is one thing left, the type. ALL the types can be found in global.lua. Let's take type 12 (uh type). So it's going to be:
-doSendMagicEffect(getPlayerPosition(cid),12)


Code:
[COLOR=#B1B100]function[/COLOR][COLOR=#333333] onSay[/COLOR][COLOR=#66CC66]([/COLOR][COLOR=#333333]cid, words, param[/COLOR][COLOR=#66CC66])[/COLOR]
[COLOR=#B1B100]if[/COLOR] doPlayerRemoveMoney[COLOR=#66CC66]([/COLOR]cid,[COLOR=#CC66CC]15000[/COLOR][COLOR=#66CC66])[/COLOR] [COLOR=#66CC66]==[/COLOR] [COLOR=#CC66CC]1[/COLOR] [COLOR=#B1B100]then[/COLOR]    
         doPlayerAddItem[COLOR=#66CC66]([/COLOR]cid,[COLOR=#CC66CC]2173[/COLOR],[COLOR=#CC66CC]1[/COLOR][COLOR=#66CC66]) 
[/COLOR][COLOR=#333333]         doSendMagicEffect[/COLOR][COLOR=#66CC66]([/COLOR][COLOR=#333333]getPlayerPosition[/COLOR][COLOR=#66CC66]([/COLOR][COLOR=#333333]cid[/COLOR][COLOR=#66CC66])[/COLOR][COLOR=#333333],[/COLOR][COLOR=#CC66CC]12[/COLOR][COLOR=#66CC66])[/COLOR]


Now there are two things left: the text, and the cancel. We'll begin with the text. To send the player a text, use this command:
-doPlayerSendTextMessage(cid,MessageClasses,message )

Where it says MessageClasses, we have to put a number. I looked into global.lua, and I saw number 22. That will do the job.
And ofcourse, the message: "You've bought an Amulet of Loss!"
So it's going to be:
-doPlayerSendTextMessage(cid,22,"You've bought an Amulet of Loss!")

In our script:

Code:
[COLOR=#B1B100]function[/COLOR][COLOR=#333333] onSay[/COLOR][COLOR=#66CC66]([/COLOR][COLOR=#333333]cid, words, param[/COLOR][COLOR=#66CC66])[/COLOR]
[COLOR=#B1B100]if[/COLOR] doPlayerRemoveMoney[COLOR=#66CC66]([/COLOR]cid,[COLOR=#CC66CC]15000[/COLOR][COLOR=#66CC66])[/COLOR] [COLOR=#66CC66]==[/COLOR] [COLOR=#CC66CC]1[/COLOR] [COLOR=#B1B100]then[/COLOR]    
         doPlayerAddItem[COLOR=#66CC66]([/COLOR]cid,[COLOR=#CC66CC]2173[/COLOR],[COLOR=#CC66CC]1[/COLOR][COLOR=#66CC66])[/COLOR]    
         doSendMagicEffect[COLOR=#66CC66]([/COLOR]getPlayerPosition[COLOR=#66CC66]([/COLOR]cid[COLOR=#66CC66])[/COLOR],[COLOR=#CC66CC]12[/COLOR][COLOR=#66CC66]) 
[/COLOR][COLOR=#333333]         doPlayerSendTextMessage[/COLOR][COLOR=#66CC66]([/COLOR][COLOR=#333333]cid,[/COLOR][COLOR=#CC66CC]22[/COLOR][COLOR=#333333],[/COLOR][COLOR=#FF0000]"You've bought an Amulet of Loss!"[/COLOR][COLOR=#66CC66])[/COLOR]

Now for the last thing: if the player doesn't have the money, we send him a cancel with this command:
-doPlayerSendCancel(cid,text)

Let's put in text: "You don't have enough money."

So:
-doPlayerSendCancel(cid,"You don't have enough money.")

I just thought, why not send also a magic-effect if the player gets a cancel? You know that kinda white flash...? It's magiceffect is 2, so:
-doSendMagicEffect(getPlayerPosition(cid),2)

In our script:

Code:
[COLOR=#B1B100]function[/COLOR][COLOR=#333333] onSay[/COLOR][COLOR=#66CC66]([/COLOR][COLOR=#333333]cid, words, param[/COLOR][COLOR=#66CC66])[/COLOR]
[COLOR=#B1B100]if[/COLOR] doPlayerRemoveMoney[COLOR=#66CC66]([/COLOR]cid,[COLOR=#CC66CC]15000[/COLOR][COLOR=#66CC66])[/COLOR] [COLOR=#66CC66]==[/COLOR] [COLOR=#CC66CC]1[/COLOR] [COLOR=#B1B100]then[/COLOR]    
             doPlayerAddItem[COLOR=#66CC66]([/COLOR]cid,[COLOR=#CC66CC]2173[/COLOR],[COLOR=#CC66CC]1[/COLOR][COLOR=#66CC66])[/COLOR]    
             doSendMagicEffect[COLOR=#66CC66]([/COLOR]getPlayerPosition[COLOR=#66CC66]([/COLOR]cid[COLOR=#66CC66])[/COLOR],[COLOR=#CC66CC]12[/COLOR][COLOR=#66CC66])[/COLOR]    
             doPlayerSendTextMessage[COLOR=#66CC66]([/COLOR]cid,[COLOR=#CC66CC]22[/COLOR],[COLOR=#FF0000]"You've bought an Amulet of Loss!"[/COLOR][COLOR=#66CC66])
[/COLOR][COLOR=#B1B100]else[/COLOR] [COLOR=#808080][I]-- if the player doesn't have enough money...[/I][/COLOR]   
             doPlayerSendCancel[COLOR=#66CC66]([/COLOR]cid,[COLOR=#FF0000]"You don't have enough money."[/COLOR][COLOR=#66CC66]) 
[/COLOR][COLOR=#333333]             doSendMagicEffect[/COLOR][COLOR=#66CC66]([/COLOR][COLOR=#333333]getPlayerPosition[/COLOR][COLOR=#66CC66]([/COLOR][COLOR=#333333]cid[/COLOR][COLOR=#66CC66])[/COLOR][COLOR=#333333],[/COLOR][COLOR=#CC66CC]2[/COLOR][COLOR=#66CC66])[/COLOR]

Now did you notice that else? It means, if the player doesn't have the requirements which are shown in the beginning of the script, he gets a cancel-message. In scripting language: else, doPlayerSendCancel(...)

Don't forget to put "end" at the end of every if! This is our final script:

Code:
[COLOR=#B1B100]function[/COLOR][COLOR=#333333] onSay[/COLOR][COLOR=#66CC66]([/COLOR][COLOR=#333333]cid, words, param[/COLOR][COLOR=#66CC66])[/COLOR]
[COLOR=#B1B100]if[/COLOR] doPlayerRemoveMoney[COLOR=#66CC66]([/COLOR]cid,[COLOR=#CC66CC]15000[/COLOR][COLOR=#66CC66])[/COLOR] [COLOR=#66CC66]==[/COLOR] [COLOR=#CC66CC]1[/COLOR] [COLOR=#B1B100]then[/COLOR]    
         doPlayerAddItem[COLOR=#66CC66]([/COLOR]cid,[COLOR=#CC66CC]2173[/COLOR],[COLOR=#CC66CC]1[/COLOR][COLOR=#66CC66])[/COLOR]    
         doSendMagicEffect[COLOR=#66CC66]([/COLOR]getPlayerPosition[COLOR=#66CC66]([/COLOR]cid[COLOR=#66CC66])[/COLOR],[COLOR=#CC66CC]12[/COLOR][COLOR=#66CC66])[/COLOR]    
         doPlayerSendTextMessage[COLOR=#66CC66]([/COLOR]cid,[COLOR=#CC66CC]22[/COLOR],[COLOR=#FF0000]"You've bought an Amulet of Loss!"[/COLOR][COLOR=#66CC66])
[/COLOR][COLOR=#B1B100]else[/COLOR]    
         doPlayerSendCancel[COLOR=#66CC66]([/COLOR]cid,[COLOR=#FF0000]"You don't have enough money."[/COLOR][COLOR=#66CC66])[/COLOR]    
         doSendMagicEffect[COLOR=#66CC66]([/COLOR]getPlayerPosition[COLOR=#66CC66]([/COLOR]cid[COLOR=#66CC66])[/COLOR],[COLOR=#CC66CC]2[/COLOR][COLOR=#66CC66])
[/COLOR]
[COLOR=#B1B100]end
[/COLOR][COLOR=#B1B100]return[/COLOR] TRUE 
[COLOR=#B1B100]end[/COLOR]

You can do everything with talkactions. Like, say something on a special tile, and you get teleported to another place. Or, you say something, and a wall will be removed.

It's quite fun :p
 
Thanks, u gave me an idea of changing Money like this:
Talkaction.xml
<talkaction words="!crystaltonugget" event="script" value="changegold/Crystal to Nugget.lua"/>
<talkaction words="!nuggettocrystal" event="script" value="changegold/Nugget to Crystal.lua"/>
Data/talkaction/changegold/Crystal to Nugget.lua
function onSay(cid, words, param)
if doPlayerRemoveItem(cid,2160,100) then
doPlayerAddItem(cid,2157,1)
doSendMagicEffect(getPlayerPosition(cid),12)
doPlayerSendTextMessage(cid,21,"You have changed 100 Crystal coins to 1 Gold nugget!")
else
doPlayerSendCancel(cid,"You don't have enough money.")
doSendMagicEffect(getPlayerPosition(cid),2)

end
return TRUE
end

and change back:
function onSay(cid, words, param)
if doPlayerRemoveItem(cid,2157,1) then
doPlayerAddItem(cid,2160,100)
doSendMagicEffect(getPlayerPosition(cid),12)
doPlayerSendTextMessage(cid,22,"You have changed 1 Gold nugget to 100 Crystal coins!")
else
doPlayerSendCancel(cid,"You don't have enough money.")
doSendMagicEffect(getPlayerPosition(cid),2)

end
return TRUE
end

but what should i do to alow changing with X count? like change 500cc to 5 nuggets with:
HTML:
!crystaltonugget 5
 
Thanks, u gave me an idea of changing Money like this:
Talkaction.xml

Data/talkaction/changegold/Crystal to Nugget.lua

and change back:


but what should i do to alow changing with X count? like change 500cc to 5 nuggets with:
HTML:
!crystaltonugget 5

To be honest, I wouldn't make it a talkaction command.. I recommend an OnUse (action command) :)
 
@up, agreed; seems like most tutorials that use color coding are screwed now.. just another reason why this forum conversion was a horrible idea.
 
how could i make a talk action to combine 2 or more items?
 
can a mod fix this and take away the color stuff so its more readable?!
 
PHP:
function onSay(cid, words, param)
PHP:
function onSay(cid, words, param)
PHP:
function onSay(cid, words, param)
    if doPlayerRemoveMoney(cid,15000) == TRUE then
PHP:
function onSay(cid, words, param)
    if doPlayerRemoveMoney(cid,15000) == 1 then
        doPlayerAddItem(cid,2173,1)
PHP:
function onSay(cid, words, param)
    if doPlayerRemoveMoney(cid,15000) == 1 then
        doPlayerAddItem(cid,2173,1)
        doSendMagicEffect(getPlayerPosition(cid),12)
PHP:
function onSay(cid, words, param)
    if doPlayerRemoveMoney(cid,15000) == 1 then
        doPlayerAddItem(cid,2173,1)
        doSendMagicEffect(getPlayerPosition(cid),12)
        doPlayerSendTextMessage(cid,22,"You've bought an Amulet of Loss!")
PHP:
function onSay(cid, words, param)
    if doPlayerRemoveMoney(cid,15000) == 1 then
         doPlayerAddItem(cid,2173,1)
         doSendMagicEffect(getPlayerPosition(cid),12)
         doPlayerSendTextMessage(cid,22,"You've bought an Amulet of Loss!")
    else -- if the player doesnt have enough money...
         doPlayerSendCancel(cid,"You don't have enough money.")
         doSendMagicEffect(getPlayerPosition(cid),2)
PHP:
function onSay(cid, words, param)
    if doPlayerRemoveMoney(cid,15000) == 1 then
        doPlayerAddItem(cid,2173,1)
        doSendMagicEffect(getPlayerPosition(cid),12)
        doPlayerSendTextMessage(cid,22,"You've bought an Amulet of Loss!")
    else
        doPlayerSendCancel(cid,"You don't have enough money.")
        doSendMagicEffect(getPlayerPosition(cid),2)
    end

    return TRUE
end
 
PHP:
function onSay(cid, words, param)
PHP:
function onSay(cid, words, param)
PHP:
function onSay(cid, words, param)
    if doPlayerRemoveMoney(cid,15000) == TRUE then
PHP:
function onSay(cid, words, param)
    if doPlayerRemoveMoney(cid,15000) == 1 then
        doPlayerAddItem(cid,2173,1)
PHP:
function onSay(cid, words, param)
    if doPlayerRemoveMoney(cid,15000) == 1 then
        doPlayerAddItem(cid,2173,1)
        doSendMagicEffect(getPlayerPosition(cid),12)
PHP:
function onSay(cid, words, param)
    if doPlayerRemoveMoney(cid,15000) == 1 then
        doPlayerAddItem(cid,2173,1)
        doSendMagicEffect(getPlayerPosition(cid),12)
        doPlayerSendTextMessage(cid,22,"You've bought an Amulet of Loss!")
PHP:
function onSay(cid, words, param)
    if doPlayerRemoveMoney(cid,15000) == 1 then
         doPlayerAddItem(cid,2173,1)
         doSendMagicEffect(getPlayerPosition(cid),12)
         doPlayerSendTextMessage(cid,22,"You've bought an Amulet of Loss!")
    else -- if the player doesnt have enough money...
         doPlayerSendCancel(cid,"You don't have enough money.")
         doSendMagicEffect(getPlayerPosition(cid),2)
PHP:
function onSay(cid, words, param)
    if doPlayerRemoveMoney(cid,15000) == 1 then
        doPlayerAddItem(cid,2173,1)
        doSendMagicEffect(getPlayerPosition(cid),12)
        doPlayerSendTextMessage(cid,22,"You've bought an Amulet of Loss!")
    else
        doPlayerSendCancel(cid,"You don't have enough money.")
        doSendMagicEffect(getPlayerPosition(cid),2)
    end

    return TRUE
end
thanks so much!
 
PHP:
function onSay(cid, words, param)
Ok, for this function I'm going to, just like the action one, make step by step a nice, working sript.
Action means, if a player uses something... something will happen.
Talkactions means: if a player says something... something will happen.

What shall we make this time??? Hmmm.....
[think]...[/think] I got it!
What about a script, if you say something, you'll get an item, and money from your backpack will be removed.
But which item??? Hmmm.....
[think]...[/think] An amulet of loss! People use it many times, so this command will make it faster for them to get one.

Ok here we go...!

The beginning:

PHP:
function onSay(cid, words, param)
In order to get the aol, the player has to give money. To do that, we use this command:
-doPlayerRemoveMoney(cid,count)

We have to change this command a bit, so the player has to give 15k, or: 15.000 gold, so:
(NOTE THAT YOU CAN'T PUT 15K INSTEAD OF 15000! ALWAYS IN GOLD COINS!)
PHP:
function onSay(cid, words, param)
    if doPlayerRemoveMoney(cid,15000) == TRUE then
Did you see I put if before the command? It just says, if the player gives 15000 gp... then something will happen.
Also, I put == TRUE after the command. That is so the server checks if it's true which means, if the player really gives money... if I put FALSE, then the player can get as many aols as he want, because they're for free then :p

Then, we want the player to get an amulet of loss.. the itemID if an aol is 2173. To give the player an item, use this command:
-doPlayerAddItem(cid,itemid,count or type)

Now we have to change the command a bit before we add it to our script. The itemid is 2173, and the count? Only 1 right.. so:
-doPlayerAddItem(cid,2173,1)

In our script:

PHP:
function onSay(cid, words, param)
    if doPlayerRemoveMoney(cid,15000) == 1 then
        doPlayerAddItem(cid,2173,1)
Now we can end the script by placing an "end" at the end, but this one would be really boring. Why won't we send a text to the player, a magic effect and put a cancel if the player doesn't have enough money? That would me more awesome.
biggrin.gif

To send a magic effect, you'll need this command:
-doSendMagicEffect(position,type)

Now we get to something new. Where it says position, we have to put the player's position. But were not going to place like 1000000 coordinates... We have a special command for that:
-getPlayerPosition(cid)

With this command, it doesn't matter where the player is standing, it'll always work. So we have to put this command into the other, where it says position:
-doSendMagicEffect(getPlayerPosition(cid),type)

Now there is one thing left, the type. ALL the types can be found in global.lua. Let's take type 12 (uh type). So it's going to be:
-doSendMagicEffect(getPlayerPosition(cid),12)

PHP:
function onSay(cid, words, param)
    if doPlayerRemoveMoney(cid,15000) == 1 then
        doPlayerAddItem(cid,2173,1)
        doSendMagicEffect(getPlayerPosition(cid),12)
Now there are two things left: the text, and the cancel. We'll begin with the text. To send the player a text, use this command:
-doPlayerSendTextMessage(cid,MessageClasses,message )

Where it says MessageClasses, we have to put a number. I looked into global.lua, and I saw number 22. That will do the job.
And ofcourse, the message: "You've bought an Amulet of Loss!"
So it's going to be:
-doPlayerSendTextMessage(cid,22,"You've bought an Amulet of Loss!")

In our script:

PHP:
function onSay(cid, words, param)
    if doPlayerRemoveMoney(cid,15000) == 1 then
        doPlayerAddItem(cid,2173,1)
        doSendMagicEffect(getPlayerPosition(cid),12)
        doPlayerSendTextMessage(cid,22,"You've bought an Amulet of Loss!")
Now for the last thing: if the player doesn't have the money, we send him a cancel with this command:
-doPlayerSendCancel(cid,text)

Let's put in text: "You don't have enough money."

So:
-doPlayerSendCancel(cid,"You don't have enough money.")

I just thought, why not send also a magic-effect if the player gets a cancel? You know that kinda white flash...? It's magiceffect is 2, so:
-doSendMagicEffect(getPlayerPosition(cid),2)

In our script:

PHP:
function onSay(cid, words, param)
    if doPlayerRemoveMoney(cid,15000) == 1 then
         doPlayerAddItem(cid,2173,1)
         doSendMagicEffect(getPlayerPosition(cid),12)
         doPlayerSendTextMessage(cid,22,"You've bought an Amulet of Loss!")
    else -- if the player doesnt have enough money...
         doPlayerSendCancel(cid,"You don't have enough money.")
         doSendMagicEffect(getPlayerPosition(cid),2)
Now did you notice that else? It means, if the player doesn't have the requirements which are shown in the beginning of the script, he gets a cancel-message. In scripting language: else, doPlayerSendCancel(...)

Don't forget to put "end" at the end of every if! This is our final script:


PHP:
function onSay(cid, words, param)
    if doPlayerRemoveMoney(cid,15000) == 1 then
        doPlayerAddItem(cid,2173,1)
        doSendMagicEffect(getPlayerPosition(cid),12)
        doPlayerSendTextMessage(cid,22,"You've bought an Amulet of Loss!")
    else
        doPlayerSendCancel(cid,"You don't have enough money.")
        doSendMagicEffect(getPlayerPosition(cid),2)
    end

    return TRUE
end
 
Back
Top