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

Solved Return False/True. How does it really work?

Ramirow

Veteran OT User
Joined
Aug 22, 2009
Messages
584
Solutions
15
Reaction score
301
Location
Argentina
YouTube
ramirogrant
Hello guys, I have a doubt, as the title says, how does really Return in LUA works?

For instance, I have this script.

Code:
for i=1,#runes,1 do
   if runes[i].name == string.lower((words.." "..param)) then
     local voc = player:getVocation():getId()
     if isInArray({3, 7, 4, 8}, voc) then
       player:sendTextMessage(MESSAGE_STATUS_SMALL, "Your vocation cannot use this spell.")
       player:getPosition():sendMagicEffect(CONST_ME_POFF)
       return true
     end

After that, I have some more 'if's', returning true after that statement would stop the script execution? And if I do return false, it will carry on?
Or that's not the way it works?

Hopefully someone could clarify it. Thanks in advance!
 
Hello guys, I have a doubt, as the title says, how does really Return in LUA works?

For instance, I have this script.

Code:
for i=1,#runes,1 do
   if runes[i].name == string.lower((words.." "..param)) then
     local voc = player:getVocation():getId()
     if isInArray({3, 7, 4, 8}, voc) then
       player:sendTextMessage(MESSAGE_STATUS_SMALL, "Your vocation cannot use this spell.")
       player:getPosition():sendMagicEffect(CONST_ME_POFF)
       return true
     end

After that, I have some more 'if's', returning true after that statement would stop the script execution? And if I do return false, it will carry on?
Or that's not the way it works?

Hopefully someone could clarify it. Thanks in advance!
I can give you an example, tho I may not be 2000% correct on how it works. Example (based on 0.3.6 revs +)

Code:
function blabla thingy
if getPlayerLevel(cid) >= 3000 then
return false
end

if getPlayerLevel(cid) >= 1000 then
dostuff
end
return true
end

Its a very bad example on return false, but it can be used to make max level limits etc
 
any return stops the script execution
true/false doesnt matter, just if you have like
Code:
function a(n)
return n
print("test")
end
in this case
x = a(2), x = 2 because a(...) just returns what you put into it, and the print("test") will not execute because return n stops the script execution afaik
 
Last edited:
i just tested it and that example function will actually give an error, due to it having things after return (which isnt allowed)

and i can see you took an early version of the script, i would recommend taking a newer one since it has better performance and is more explanatory in errors :)
 
Ohhh you mean the 'for' part? Yeah! Got the idea from one of the posts here in support lol, even tough my script now has more than 150 lines, it worked, but I needed to clarify if Return indeed stopped script execution, I'm relieved it does lol.

Thanks a lot guys!
 
The only time you would return a value whether it be true or false is if it were used as a comparison function.

For instance
Code:
function d(a)
      return a
end

if d(5) == 5 then
     -- do something
end

However you can just use the functions return value to determine the outcome.
Code:
-- if a + b = c return true else return false
function add(a, b, c)
      return (a + b) == c
end

if add(2, 3, 5) then
     -- do something
end

If you just want to terminate the function at a given point would simply write return, which turns control back over to the function which called it.
Code:
function countTo(a)
     for i = 1, a do
          if i == 9 then
               return
          end
     end
end

Now if your intention is not to return but rather stop the loop execution you simply use break
Code:
function countTo(a)
     for i = 1, a do
          if i == 9 then
               break
          end
     end
end
What break does is it exits the loop, now you can continue with the rest of your function or let it run its course which will eventually exit the function and send control back to which called it.
 
This goes far beyond what was already mentioned here.
1) return can be used to tell the code to stop at a certain point so we don't let it read unnecessary codelines.
Code:
if x == y then
   something happens
   return true
end
if z == q then
   another thing happens
   return false
end
if x == y then it does not read anything after the next end.
In this case we talk about the z == q part.

2) It can be used as a return value, returning a number / string / table / userdata
Code:
return math.random(1,1000) -- returns a random number between 1 and 1000
return "a string" -- returns a string containing "a string"
return {"a table", 1} -- returns a table
return Player("Random Player") -- returns the userdata of "Random Player"

3) It interactes with c functions and tells them to carry on or not.
This is an example with a door.
In case we return false, we tell the c function that the door cannot be opened so it should not carry on reading the codelines.
In case we return true, we tell the c function to normaly proceed and open the door.
Code:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
   if player:getName() == "Evil Hero" then
     return true
   else
     return false
   end
end
This would open the door only if the player name would be "Evil Hero"
 
Or..
Code:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
   return player:getName() == "Evil Hero"
end
Of course if player doesn't exist which would be weird in an action script you would f'd :p
 
This goes far beyond what was already mentioned here.
1) return can be used to tell the code to stop at a certain point so we don't let it read unnecessary codelines.
Code:
if x == y then
   something happens
   return true
end
if z == q then
   another thing happens
   return false
end
if x == y then it does not read anything after the next end.
In this case we talk about the z == q part.

2) It can be used as a return value, returning a number / string / table / userdata
Code:
return math.random(1,1000) -- returns a random number between 1 and 1000
return "a string" -- returns a string containing "a string"
return {"a table", 1} -- returns a table
return Player("Random Player") -- returns the userdata of "Random Player"

3) It interactes with c functions and tells them to carry on or not.
This is an example with a door.
In case we return false, we tell the c function that the door cannot be opened so it should not carry on reading the codelines.
In case we return true, we tell the c function to normaly proceed and open the door.
Code:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
   if player:getName() == "Evil Hero" then
     return true
   else
     return false
   end
end
This would open the door only if the player name would be "Evil Hero"
Just adding, this may not be true for all callbacks as there are some that don't use the returned value at all.
 
Back
Top