• 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!

Script request

VIngal

New Member
Joined
May 2, 2019
Messages
8
Reaction score
0
Good morning,

I've tried writing a code, however could not make it work. My codding skills are below basic.

I need a script that makes the player say a specific sentence once the monster health hit a certain percentage.
For example, if a Juggernaut hits the 30% HP mark, the player will use a certain spell.

I wrote this code:

cycleEvent(function ()
if g_game.isOnline()
then local p = g_game.getMonsterInfo('MONSTER NAME')
if p:getHealth()/p:getMaxHealth() > 0.3 then
then g_game.talk('SENTENCE')
end end end end end, 500)

However, it's not working. I'm getting this message at the terminal:


ERROR: lua function callback failed: LUA ERROR:
[string "cycleEvent(function () if g_game.isOnline()..."]:1: attempt to call field 'getMonsterInfo' (a nil value)
stack traceback:
[C]: in function 'getMonsterInfo'
[string "cycleEvent(function () if g_game.isOnline()..."]:1: in function <[string "cycleEvent(function () if g_game.isOnline()..."]:1>

Thank you all in advance.
 
Sooo, basically you'll need some sort of 'trigger'.

How you go about triggering the script is up to you, but for my example I'll use 'onStatsChange'.

Lua:
local creatures = {}

-- cid = creature being attacked, attacker = person dealing the damage, value = damage about to be applied
function onStatsChange(cid, attacker, type, combat, value)

    -- if attacker is not a player, then do nothing
    if not isPlayer(attacker) then
        return true
    end
   
    -- if the damage type is health loss, then do something.
    if type == STATSCHANGE_HEALTHLOSS then
   
        -- check if this monster has forced a player to say something before
        if creatures[cid] then
            return true
        end
       
        -- check that it's the correct monster.
        if getCreatureName(cid):lower() ~= "juggernaut" then
            return true
        end
       
        -- calculate monsters hp at 30%, and compare hp
        if (getCreatureHealth(cid) - value) <= ((getCreatureMaxHealth(cid) / 100) * 30) then
       
            -- make the player say something
            doCreatureSay(attacker, "The Juggernaut is getting weak!\nDon't let the beast run away!\nFIGHT!!")
           
            -- add creature to list so that this doesn't trigger multiple times
            creatures[cid] = 1
            return true
        end
    end
    return true
end
 
So, I've tried the following:

I've established a trigger (hitting a certain percentage of health of a certain creature) and an action to take (send a message):

Code:
cycleEvent(function ()
if g_game.isOnline()
if getCreatureName(cid):lower() ~= "Juggernaut" then
if (getCreatureHealth(cid) - value) <= ((getCreatureMaxHealth(cid) / 100) * 30) then
then g_game.talk('The Juggernaut is getting weak! Don't let the beast run away! FIGHT!!')
end end end end end, 500)

Still got this error:
ERROR: command failed: C++ call failed: LUA ERROR: attempt to cast a 'string' lua value to 'int'
stack traceback:
[C]: ?
[C]: in function 'cycleEvent'
/corelib/globals.lua:29: in function '?'
/client_terminal/terminal.lua:348: in function </client_terminal/terminal.lua:348>
[C]: in function 'pcall'
/client_terminal/terminal.lua:366: in function 'executeCommand'
/client_terminal/terminal.lua:105: in function </client_terminal/terminal.lua:103>
[C]: in function 'pcall'
/corelib/util.lua:311: in function </corelib/util.lua:309>
(tail call): ?
stack traceback:
[C]: ?
[C]: in function 'cycleEvent'
/corelib/globals.lua:29: in function '?'
/client_terminal/terminal.lua:348: in function </client_terminal/terminal.lua:348>
[C]: in function 'pcall'
/client_terminal/terminal.lua:366: in function 'executeCommand'
/client_terminal/terminal.lua:105: in function </client_terminal/terminal.lua:103>
[C]: in function 'pcall'
/corelib/util.lua:311: in function </corelib/util.lua:309>
(tail call): ?

Can't figure out what's wrong. 😔
 
I mean.. I'm just using the normal lua functions available in the server.. you seem to be trying to compile or something? idk
Those aren't normal errors I'm used to seeing.
 
I'm trying to run this script on the terminal, with the compiled version of the client.

The logic and the functions for the code aren't wrong, I guess.
Can't figure out what's wrong.
 
This should be on server side, not client side. Xikini gave you a script to do so and you completely ignored it.
 
This should be on server side, not client side. Xikini gave you a script to do so and you completely ignored it.

Sorry about the misunderstanding. As I said, my coding skills are nearly zero. I came to this forum looking for help.

So you suggest that I try using his complete code for the desired function? Or am I getting it all wrong (since the provided code works on the developer instance and I am running on the compiled client)?
 
I'm confused. Are you trying to modify a server's client to your own needs, or are you a developer of your own server trying to accomplish this task in any way possible?
 
First of all, I've never said I wasn't trying to meet my goal or need.
You assumed that. And it's not a bad thing.

However, indeed I came to this forum looking for help to encode a function in a server that allows me to do so.

If you can read Portuguese, I can link the same request I did here in the that server's forum (Wich has not been answered) prior to this one.

As I said before my need is to develop a code that I can use on the client's terminal to automatically send a message once the monster's hp reach a certain percentage.

If you are not willing to help, I assume there's no point on arguing.
 
His script will accomplish what you need. Use what he provided and modify it where necessary.
 
can you run code via otclient terminal?

I've pasted the whole script at the terminal.
Nothing happened.

Tried editing the code to this one:


Code:
cycleEvent(function()
if g_game.isOnline() then
local creatures = {}
function onStatsChange(cid, attacker, type, combat, value)
if not isPlayer(attacker) then
return true
end
if type == STATSCHANGE_HEALTHLOSS then
if creatures[cid] then
return true
end
if getCreatureName(cid):lower() ~= "juggernaut" then
return true
end
if (getCreatureHealth(cid) - value) <= ((getCreatureMaxHealth(cid) / 100) * 30) then
g_game.talk('TEST')
creatures[cid] = 1
return true
end
end
return true
end, 1000)


Still couldn't make it work.
 
I've pasted the whole script at the terminal.
Nothing happened.

Tried editing the code to this one:


Code:
cycleEvent(function()
if g_game.isOnline() then
local creatures = {}
function onStatsChange(cid, attacker, type, combat, value)
if not isPlayer(attacker) then
return true
end
if type == STATSCHANGE_HEALTHLOSS then
if creatures[cid] then
return true
end
if getCreatureName(cid):lower() ~= "juggernaut" then
return true
end
if (getCreatureHealth(cid) - value) <= ((getCreatureMaxHealth(cid) / 100) * 30) then
g_game.talk('TEST')
creatures[cid] = 1
return true
end
end
return true
end, 1000)


Still couldn't make it work.
The script I provided won't work through the client.

It would need to be placed into the server at
Code:
data/creaturescripts/scripts/file_name.lua
Additionally, you'd need to make sure it's registered in
Code:
data/creaturescripts/creaturescripts.xml
And also in the monster file.
Code:
data/monsters/juggernaut.xml

The code I provided above was for TFS 0.3.6 / 0.4
If you are using TFS 1.1/1.2/1.3 you may need to adjust some of the code.
 
The script I provided won't work through the client.

It would need to be placed into the server at
Code:
data/creaturescripts/scripts/file_name.lua
Additionally, you'd need to make sure it's registered in
Code:
data/creaturescripts/creaturescripts.xml
And also in the monster file.
Code:
data/monsters/juggernaut.xml

The code I provided above was for TFS 0.3.6 / 0.4
If you are using TFS 1.1/1.2/1.3 you may need to adjust some of the code.


I really appreciate your will to help and would like to thank you for you attention.

Unfortunately, I don't have this kind of access to in-server data.

I could only run the requested script at the client's terminal. As a cyclic event so the character would constantly be checking the creature info and trigger the message.
 
I really appreciate your will to help and would like to thank you for you attention.

Unfortunately, I don't have this kind of access to in-server data.

I could only run the requested script at the client's terminal. As a cyclic event so the character would constantly be checking the creature info and trigger the message.
If you're creating this inside the client.. try looking through this stuff?
Useful Links (https://otland.net/threads/useful-links.169907/)
There might be enough information there to help you find the functions you're looking for.

I haven't personally fiddled around with anything on the client, so I'd be less then useless in this regard.
Good luck!
 
Thank you all.
I'll dive into those tutorials and see what I can do.
 
Back
Top