• 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!
  • 2026 staff recruitment is open! Check it out and consider applying!

Help with an items that gives life to a player pls

Cris2387

Member
Joined
Dec 30, 2013
Messages
177
Reaction score
9
Hello, i made some quests in my server that give you items that give you life & mana etc, but i cant get the item to work :l this is the script i made with my amateur skills lol feel free to call me a noob xD
Code:
function onUse(cid, item, fromPosition, itemEx, toPosition)
if item.uid == 3332 then
queststatus = getPlayerStorageValue(cid,3332)
if queststatus == -1 or queststatus == 0 then
  setCreatureMaxHealth(cid, getCreatureMaxHealth(cid)+50000)
    doCreatureSay(cid, "You gained 50k of life, now your health is ".. getCreatureMaxHealth(cid) .."!" ,19)
doSendMagicEffect(getCreaturePosition(cid), CONST_ME_FIREWORK_RED)
doRemoveItem(item.uid)
setPlayerStorageValue(cid,3332,1)
else
doPlayerSendTextMessage(cid,22,"You already got your life.")
end
else
return 0
end
return 1
end
this is just one of the scripts, according to me, the script will give 50k of life to the player that uses it, and if the player tries to use another scroll, it will not let him hehe, and yes i put the item in actions, this is what i have in actions
Code:
   <action itemid="6571" event="script" value="itemlife.lua"/>
help me i dont know why it isnt working, i dont get any errors when i click on the items nothing happens, it just says " you cannot use this item "
btw what do you guys think about my mapping skills lol?
upload_2015-1-18_14-50-16.png
that door only allow players to pass if they have vip 2 hehe
 
Try this instead :)
Code:
local itemStorage = 3332
function onUse(cid, item, fromPosition, itemEx, toPosition)
    if item.uid == itemStorage then
        if getPlayerStorageValue(cid, itemStorage) ~= 1 then
            setCreatureMaxHealth(cid, getCreatureMaxHealth(cid) + 50000)
            doCreatureSay(cid, "You gained 50k of life, now your health is ".. getCreatureMaxHealth(cid) .."!", 19)
            doSendMagicEffect(getCreaturePosition(cid), CONST_ME_FIREWORK_RED)
            doRemoveItem(item.uid)
            setPlayerStorageValue(cid, itemStorage, 1)
        else
            doPlayerSendTextMessage(cid, 22, "You already got your life.")
            return false
        end
    end
    return true
end

About the mapping skills, looks pretty good :)
 
Try this instead :)
Code:
local itemStorage = 3332
function onUse(cid, item, fromPosition, itemEx, toPosition)
    if item.uid == itemStorage then
        if getPlayerStorageValue(cid, itemStorage) ~= 1 then
            setCreatureMaxHealth(cid, getCreatureMaxHealth(cid) + 50000)
            doCreatureSay(cid, "You gained 50k of life, now your health is ".. getCreatureMaxHealth(cid) .."!", 19)
            doSendMagicEffect(getCreaturePosition(cid), CONST_ME_FIREWORK_RED)
            doRemoveItem(item.uid)
            setPlayerStorageValue(cid, itemStorage, 1)
        else
            doPlayerSendTextMessage(cid, 22, "You already got your life.")
            return false
        end
    end
    return true
end

About the mapping skills, looks pretty good :)
I'm trying to learn as well.
What is the reason for return false?
It already executed an action, and would return true that it finished.
I just don't understand the return true/false at all.

When should they be used?

I've asked before and the usual answer is "habit" "I'm not really sure.."
Just wondering if anyone actually knows the answer.. :[
 
I'm trying to learn as well.
What is the reason for return false?
It already executed an action, and would return true that it finished.
I just don't understand the return true/false at all.

When should they be used?

I've asked before and the usual answer is "habit" "I'm not really sure.."
Just wondering if anyone actually knows the answer.. :[

If you haven't noticed by now every single default function for tfs is a Boolean function, meaning it's return type is of bool (true / false), in order for function to execute properly it must return a value to the server of true or false.

This is why most of the time we return true at the end of function definition to let the server know everything went well, but if we return false then the server knows not to call the script again.

Especially if you call another function within the function definition like say an addevent which calls a function and when that function is done it returns control back to the function that called it. Although we could just use the function name to call the other function, addevent is just for time based events or calls

If it sounds confusing it is :) well it is because I explained it in a confusing way.
Anyhow doesn't really matter why, just add in the return false if you want the script not to continue and let the server know things did not go as planned and return true outside of the execution hierarchy to tell the server things went well.
 
Last edited:
If you haven't noticed by now every single default function for tfs is a Boolean function, meaning it's return type is of bool (true / false), in order for function to execute properly it must return a value to the server of true or false.

This is why most of the time we return true at the end of function definition to let the server know everything went well, but if we return false then the server knows not to call the script again.

Especially if you call another function within the function definition like say an addevent which calls a function and when that function is done it returns control back to the function that called it. Although we could just use the function name to call the other function, addevent is just for time based events or calls

If it sounds confusing it is :) well it is because I explained it in a confusing way.
Anyhow doesn't really matter why, just add in the return false if you want the script not to continue and let the server know things did not go as planned and return true outside of the execution hierarchy to tell the server things went well.
I understand the return true at the end of the script, I just do not understand the return false.
Wouldn't the script run properly without using the return 0?
 
I understand the return true at the end of the script, I just do not understand the return false.
Wouldn't the script run properly without using the return 0?
Well in c/c++ you normally return 0 if things went well and return 1 if things didn't go so well, although if you think about it 0 is actually false and 1 is true, but in lua 0 is not equal to false and 1 is not necessarily equal to true.

If I were to assign the value of 0 to a variable called var and ran a simple conditional the return would be true
Code:
local var = 0
if var then
    print("true")
else
    print("false")
end
-- prints true

This is because as i stated earlier 0 is not equal to false, it is a value now if you wanted to declare a variable as false without using false or delete it you would set it nil which is a non-value and is equal to false
Code:
local var = nil
if var then
    print("true")
else
    print("false")
end
-- prints false

So this is why we use true and false instead of 0 or 1

It wouldn't execute properly if you didn't use the return false
Lets take a fishing script that i upgraded
Code:
local c = {
    waterIds = {493, 4608, 4609, 4610, 4611, 4612, 4613, 4614, 4615, 4616, 4617, 4618, 4619, 4620, 4621, 4622, 4623, 4624, 4625},
    rateSkill = getConfigValue("rateSkill"),
    useWorms = true,
    wormId = 10943
}

local monsters = {
    "Quara Hydromancer",
    "Quara Constrictor",
    "Quara Mantassin",
    "Quara Pincher",
    "Quara Predator",
    "Serpent Spawn",
    "Wyrm",
    "Sea Serpent",
    "Titan Goddess of Water"
    }
local player = {
    level = 0,
    skill = 0,
    multiplier = 2, -- this is your base multiplier, do not edit
    noFishTile = 493 -- this is the water tile you can't get fish from
}

local minLevels = {
    500,
    1500,
    3000,
    5000
}

function onUse(cid, item, fromPosition, itemEx, toPosition)
    if not isInArray(c.waterIds, itemEx.itemid) then
        return false
    end

    player.level = getPlayerLevel(cid)
    player.skill = getPlayerSkill(cid, SKILL_FISHING)

    if c.useWorms then
        if getPlayerItemCount(cid, c.wormId) < 1 then
            doPlayerSendCancel(cid, "You do not have enough worms to fish.")
            return false
        end
    end

    if not getTileInfo(getCreaturePosition(cid)).protection and itemEx.itemid ~= player.noFishTile then
        if player.level >= minLevels[#minLevels] and player.skill > 99 then
            randsummon = math.random(1, (player.multiplier * #minLevels) + 1)
        else
            for i = 1, (#minLevels - 1) do
                if player.level >= minLevels[i]  then
                    randsummon = math.random(1, player.multiplier * i)
                end
            end
        end
        doSummonMonster(monsters[randsummon], getCreaturePosition(cid))
        doPlayerAddSkillTry(cid, SKILL_FISHING, c.rateSkill)
        doSendMagicEffect(toPosition, CONST_ME_LOSEENERGY)
        doPlayerRemoveItem(cid, c.wormId, 1)
    else
        doPlayerSendCancel(cid, "You cannot fish here.")
        return false
    end
    return true
end
If I didn't return false at either of these 3 stages the script would continue to execute
Code:
    if not isInArray(c.waterIds, itemEx.itemid) then
        return false
    end
Code:
    if c.useWorms then
        if getPlayerItemCount(cid, c.wormId) < 1 then
            doPlayerSendCancel(cid, "You do not have enough worms to fish.")
            return false
        end
    end
Code:
else
    doPlayerSendCancel(cid, "You cannot fish here.")
    return false
end

Which would defeat the purpose of putting those conditions in and people would be allowed to fish up monsters anywhere and if it were set to use worms they wouldn't need worms.
In your scripts you can have as many return falses as you like but you only need 1 return true.
When you return false it stops the execution of the script and returns control back to the function that called it.
 
Last edited:
Code:
else
    doPlayerSendCancel(cid, "You cannot fish here.")
    return false
end
When you return false it stops the execution of the script and returns control back to the function that called it.
I understand the first two return false, however this one I am still struggling to grasp.
When you return false at this point, you have already sent a message to the player that tells them that it wasn't possible. If you omitted this return false the script would still function as intended, right?
When it decides that the tile was not fishable, it went to the next if or elseif or else and executed those actions then it would return true that something happened and it reached the end of the script.

In fact.. you could even put a return true there, and it would stop the script as well..
and the second return false could also be set to return true.. could it not?

I guess it's still kind of muddled.
I guess it comes down to something like this?

Return true, ends script and verifies something happened.
Return false, ends script and verifies something happened that wasn't intended.

So for the third part in your fishing script.. it's just to save a couple milliseconds of time?
-- Edit
Not trying to sound stupid, I'm just trying to muddle through with my problem solving skillzz.
"returns control back to the function that called it."
What does this mean in regards to action/movement scripts?
Does it just mean that it will end the script?
Just covering all the bases. :P
 
I understand the first two return false, however this one I am still struggling to grasp.
When you return false at this point, you have already sent a message to the player that tells them that it wasn't possible. If you omitted this return false the script would still function as intended, right?
If you omit the return false, the script will continue on to the next part of the script which is return true telling the function which called it, in this case the server that everything went well when that was not the intention.

When it decides that the tile was not fishable, it went to the next if or elseif or else and executed those actions then it would return true that something happened and it reached the end of the script.
When it found that the tile was not fishable it immediately exited the script.

In fact.. you could even put a return true there, and it would stop the script as well..
and the second return false could also be set to return true.. could it not?
Sure you could put a return true there but the purpose of the return false is to prevent action.

Return false, ends script and verifies something happened that wasn't intended.
return false ends the script and tells whatever that called it nothing happen because it didn't meet the requirements

So for the third part in your fishing script.. it's just to save a couple milliseconds of time?
-- Edit
Not trying to sound stupid, I'm just trying to muddle through with my problem solving skillzz.
"returns control back to the function that called it."
What does this mean in regards to action/movement scripts?
Does it just mean that it will end the script?
Just covering all the bases.:p
As stated above it tells the tfs server this script did not meet the criteria so the server shouldn't allow any action to be taken.

If your still confused write yourself a script which performs an action, use all the different functions available in, movements, actions, etc and check out where to use return false and return true.

I don't think i could explain it any better, maybe someone else can, but this is why I am suggesting you test it yourself.
 
If you omit the return false, the script will continue on to the next part of the script which is return true telling the function which called it, in this case the server that everything went well when that was not the intention.


When it found that the tile was not fishable it immediately exited the script.


Sure you could put a return true there but the purpose of the return false is to prevent action.


return false ends the script and tells whatever that called it nothing happen because it didn't meet the requirements


As stated above it tells the tfs server this script did not meet the criteria so the server shouldn't allow any action to be taken.

If your still confused write yourself a script which performs an action, use all the different functions available in, movements, actions, etc and check out where to use return false and return true.

I don't think i could explain it any better, maybe someone else can, but this is why I am suggesting you test it yourself.
I'm not saying it shouldn't be used, I'm just saying that a return true could be used if someone really wanted to do so.

If however we used a return false in an NPC for instance.. it would cancel out and make the npc walk around aimlessly again I'm assuming.

But no, best explanation yet. :P
return true.
 
I'm not saying it shouldn't be used, I'm just saying that a return true could be used if someone really wanted to do so.

If however we used a return false in an NPC for instance.. it would cancel out and make the npc walk around aimlessly again I'm assuming.

But no, best explanation yet. :p
return true.
Sure return true could be used or no return true or no return false, but with return true in there, it's still technically going to use the item. Having return false in there prevents the use of the item or action. Best Way to demonstrate that is, put the same type of situation in a spell, what you're going to notice is when you use the spell, with the return true it's going to say the spell words above your head, add in return false and it wont. Therefore being prevented by the return false.
 
You only use a return statement if you want to exit the script beyond that you would use conditional statements.

The mindset of a programmer has always the thought process of what if?
Always asking a question while comparing it to something else or just comparing the question to itself.
 
Back
Top