• 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 Bad Script Structure

Vikarious

New Member
Joined
Dec 16, 2008
Messages
93
Reaction score
2
I'm making a quest where you kill X creature for Y times.

As you kill more creatures your storage value is going up.

When you have killed enough creatures you come back to npc and it will reward you.

The whole thing works quite well, but when I complete the quest and kill random creatures, the counter still goes up and keep counting creature for negative (ex: you still have to kill -1 troll)

I don't know what to do, I have already tried some way on script structure but it did'nt work, it is a creaturescript.

This is the original version:
Lua:
local storage = 6004

function onDeath(cid, corpse, deathList)
    local killer = deathList
    local monster = getCreatureName(cid)
    local troll = getPlayerStorageValue(killer, storage)
    if ((monster:lower() == 'troll') and not isInArray({-1, 20}, troll)) then
        doPlayerSendTextMessage(killer, MESSAGE_STATUS_CONSOLE_BLUE, 'You still need to kill '.. 20 - troll ..' more trolls for Liutenant Simas Hawk.')
        setPlayerStorageValue(killer, storage, troll + 1)
    elseif troll == 20 then
        doPlayerSendTextMessage(killer, MESSAGE_STATUS_CONSOLE_BLUE, 'You have killed enough trolls. Come back and talk to Liutenant Simas Hawk to receive your reward.') 
    end
    return true
end

The one I thought could work by using the checkStorage function:

Lua:
local storage = 6004

function onDeath(cid, corpse, deathList)
    local killer = deathList
    local monster = getCreatureName(cid)
    local troll = getPlayerStorageValue(killer, storage)
    
	if troll > 20 then 
		if ((monster:lower() == 'troll') and not isInArray({-1, 20}, troll)) then
			doPlayerSendTextMessage(killer, MESSAGE_STATUS_CONSOLE_BLUE, 'You still need to kill '.. 20 - troll ..' more trolls for Liutenant Simas Hawk.')
			setPlayerStorageValue(killer, storage, troll + 1)
		
		
			elseif troll == 20 then
			doPlayerSendTextMessage(killer, MESSAGE_STATUS_CONSOLE_BLUE, 'You have killed enough trolls. Come back and talk to Liutenant Simas Hawk to receive your reward.') 
		end
    end
	
	return true
	
	
end

I would like to make it first check if player have a storagevalue bigger than 20 and if yes then finish itself.

But I just fail really badly at structuring script. I'm counting on your help guys!!

Thanks a lot!!

PS: 21 is the final storagevalue of quest, the value of completed.
 
Last edited:
it's better to do this "onKill" than "onDeath".

- - - Updated - - -

Lua:
local storage = 6004
 
function onKill(cid, target)
local monster = getCreatureName(target)
local troll = getPlayerStorageValue(cid, storage)
    if ((monster:lower() == 'troll') and not troll == 20) then
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, 'You still need to kill '.. 20 - troll ..' more troll'.. (troll - 20 ) == 1 and '' or 's' ..' for Liutenant Simas Hawk.')
        setPlayerStorageValue(cid, storage, troll + 1)
    elseif troll == 20 then
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, 'You have killed enough trolls. Come back and talk to Liutenant Simas Hawk to receive your reward.') 
    end
    return true
end

You need to register this for the players, in login.lua.
 
Last edited:
it's better to do this "onKill" than "onDeath".
this

and your code doesn't work, the first if is wrong and misplaced
Lua:
local storage = 6004
 
function onDeath(cid, corpse, deathList)
    local killer = deathList
    local monster = getCreatureName(cid)
    local troll = getPlayerStorageValue(killer, storage)
    if ((monster:lower() == 'troll') and troll < 20 then
        setPlayerStorageValue(killer, storage, troll + 1)
        if troll < 20 then
            doPlayerSendTextMessage(killer, MESSAGE_STATUS_CONSOLE_BLUE, 'You still need to kill '.. 20 - troll ..' more trolls for Liutenant Simas Hawk.')
        elseif troll == 20 then
            doPlayerSendTextMessage(killer, MESSAGE_STATUS_CONSOLE_BLUE, 'You have killed enough trolls. Come back and talk to Liutenant Simas Hawk to receive your reward.') 
        end
    end
    return true
end
this should do the trick using ur onDeath, but using an onKill script is much better for this situation

- - - Updated - - -

Could you please explain to me the main difference between onDeath and onKill?
onDeath runs every time someone kills something, it should be used when you want to make some effect when something dies
onKill runs every time you kill something, it should be used when your killing of a monster does some effect to yourself

examples of usage:
onDeath = when a monster dies an event ends or a cool effect happens
onKill = counting monster killings, giving exp
 
Last edited:
Alright, many thanks!!

Now I have a second question: Just by changing "onDeath" into "onKill" will it work or I'll need to adapt more things?

I suppose I'll need to change on "creaturescripts.xml"

from " <event type="death" name="Liutenant Simas Hawk" script="task/liutenant simas hawk task.lua"/>"

to

<event type="kill" name="Liutenant Simas Hawk" script="task/liutenant simas hawk task.lua"/>




Right?
 
yep

as far as the script goes, the function header of the onKill is:
Lua:
function onKill(cid, target)
cid is the player that killed, target is the monster that was killed

for easiness:
cid represents what your killer does
target represents what your cid does
 
Alright!

I'm gonna modify it all, and I'll post the finished job as soon as I have it.

Thank you very much guys!!

Thank you for your patience and time!!

To people like me, it means a lot!

- - - Updated - - -

I could come up with this fully working script:
Lua:
local storage = 6000

function onKill(cid, target)
    local killer = deathList
    local monster = getCreatureName(target)
    local rotworm = getPlayerStorageValue(cid, storage)
    
	if ((monster:lower() == 'rotworm') and rotworm < 10) then
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, 'You still need to kill '.. 10 - rotworm ..' more rotworms for Martin.')
        setPlayerStorageValue(cid, storage, rotworm + 1)
    elseif rotworm == 10 then
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, 'You have killed enough rotworms. Come back and talk to Martin to receive your reward.') 
    end
    return true
end

Everyone feel free to use it!!

Thanks for support! If it wasn't for you all guys, I would never make it!

Thank you!!
 
Last edited:
Back
Top