• 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 Own function contais a function from TFS 0.3.6 and doesn't work.

waqmaz

Member
Joined
Jun 17, 2015
Messages
203
Reaction score
11
Hello. I am learning about functions in lua. I will post a piece of a code, which doesn't work. I would like to know, how to make it working and why it doesn't work. Please, help me. I will give a like and will be grateful. :)
This is the code:
Code:
    local function add_something(bbb, msg, cid)
        if msgcontains(msg, bbb) and talkState[talkUser] == 1 then
            selfSay("You have been "..bbb..".", cid)
            talkState[talkUser] = 0
        end
        return true
    end
 
    add_something("the text", msg, cid)

I am PHP programmer. I've just started some days ago to learn lua language. Big Thanks for all reply.
 
just found solution:
Code:
 local function add_something(msg, bbb, cid)
        if msgcontains(msg, bbb) and talkState[talkUser] == 1 then
            selfSay("You have been "..bbb..".", cid)
            talkState[talkUser] = 0
        end
        return true
    end
    add_something("msg, the text", cid)
hope will help someone
 
Your add_something function when called only has 2 arguments, but the definition of the function has 3.
Definition:
add_something(msg, bbb, cid)

Called
add_something("msg, the text", cid)

I used the colors to show how the arguments are being passed.

Since there is only 2 arguments being passed to the function add_something, msgcontains will do its job and check if the value of cid is inside the string "msg, the text", since it isn't it skips over this if statement and just returns true.

The thing about and is that both operands have to be true in order for the condition to be true.

Its great that you are learning to code but you should write simpler things which will help you understand parameters, arguments, conditions and return values of a function.

:)
 
Last edited:
Back
Top