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

Lua Pass return false through function doesn't work.

waqmaz

Member
Joined
Jun 17, 2015
Messages
203
Reaction score
11
Hello there. The script works, but "return false" doesn't prevent onCombat function.

Code:
local function check(cid)

    if isPlayer(cid) then
        return false
    end

    return true
end

I want the function above to prevent the built-in game function onCombat below:

Code:
function onCombat(cid, target)

     if getCreatureName(target) == 'monster'
       check(cid)
     end

  return true
end

What am I doing wrong? Why does return false does not work? If i put "return false" instead of check(cid) in onCombat function, then it works. But with function it doesn't. Thanks!!!
 
It is important to note that when you return true, false or any other value from a function to an interface or even to another function, if it has nothing to do with the value then there is no point returning anything.
Using your example
Code:
local function check(cid)
    -- if cid is a player return false
    if isPlayer(cid) then
        return false
    end
    -- if cid isn't a player return true
    return true
end
Code:
function onCombat(cid, target)
     -- if the target's name equals monster
     if getCreatureName(target) == 'monster'
       -- check returns a value but does nothing with it
       check(cid)
     end

  return true
end
If you simply want to break the execution of a function you can just use return, all this does is exit the function.
Code:
function add(a,b)
    if a + b ~= 10 then
        return a + b
    end
    return
end

add(12, 5) -- returns 17

add(5,5) -- just returns
In most cases you don't even need to use return but for those times when you want to immediately exit the function return is the thing to use.
 
Back
Top