English Script for do

18 replies
Goto Page
To the start Previous 1 Next To the start
02.01.16 03:34:32 pm
Up
Slooper 1
BANNED
Offline Off
hi,how to make if say "!off" then i false , if say "!on" then it true ._., i want all helps cause i'm creating script
bye
Multiple account of Ya_Bogalb (a.k.a TN Anonymous)
02.01.16 03:53:45 pm
Up
Nekomata
User
Offline Off
You're going to have to explain some more to get what you need here...
02.01.16 04:03:05 pm
Up
GeoB99
Moderator
Offline Off
Code:
1
2
3
4
5
6
7
8
9
function say_(id, text)
     if string.sub(text,1,4) == "!off" then
          -- Do something
          return false
     elseif string.sub(text,1,3) == "!on" then
          -- Do something
          return true
     end
end

However, it's advisable to explain what exactly do you want because nobody understands what do you mean by "if i !on then it's true" and such. Here I gave you a code but I am still in doubt what do you want precisely though.
02.01.16 04:27:28 pm
Up
Rainoth
Moderator
Offline Off
Code:
1
2
3
4
5
6
7
8
addhook("say","_s")
function _s(id,t)
     if t == "!off" then
          bool = false
     elseif t == "!on" then
          bool = true
     end
end


He didn't want to explain it but it's enough of an explanation. He wants to be able to control something by saying "on/off".
02.01.16 04:51:46 pm
Up
Dousea
User
Offline Off
Will do pretty much the same as user GeoB99's and user Rainoth's.
Code:
1
2
3
4
5
6
7
addhook("say", "sayhook")

function sayhook(id, message)
     if (message == "!off" or message == "!on") then
          boolean = message == "!on"
     end
end
I'm awesome ... and I really like cookies.
02.01.16 08:11:16 pm
Up
Rainoth
Moderator
Offline Off
@user Dousea: Ooooh. I was aware of a similar method for writing an IF but not like how you did. You're incorporating an 'if' while assigning a value to 'boolean'. Daaam. I learned sth new today.
02.01.16 08:32:03 pm
Up
Nekomata
User
Offline Off
user Dousea has written:
Code:
1
2
3
4
5
6
7
addhook("say", "sayhook")

function sayhook(id, message)
     if (message == "!off" or message == "!on") then
          boolean = message == "!on"
     end
end

That's still going over my head.
Wouldn't variable 'boolean' return the text instead of a true boolean value? How does this work?
02.01.16 08:54:42 pm
Up
Rainoth
Moderator
Offline Off
@user Nekomata: well, he's comparing the message just like you do in an if. An if always returns either 'true' or 'false' values (not in text of course) and then he assigns the boolean to be the result of the if
02.01.16 09:26:38 pm
Up
Nekomata
User
Offline Off
Ah, I sort of get it now. Also that
Code:
1
boolean = message == "!on"
sets "!on" as true from the two values. But I'm still not sure how that is working code-wise and behind the scenes.

Either I'm missing out something really simple minded (I tend to do that) or I need to hit the basics of conditions.

Nevertheless, til here as well. Time to rewrite parts of my scripts and to hit the reference & docs.
03.01.16 03:46:54 am
Up
Dousea
User
Offline Off
@user Nekomata: Well message must be either "!on" or "!off" because of message == "!off" or message == "!on" condition. boolean must be the value of message == "!on" comparison. boolean must be either true or false based on the comparison.
I'm awesome ... and I really like cookies.
03.01.16 05:40:10 am
Up
VADemon
User
Offline Off
@user Dousea: that's a damn smartass if-condition

But ya all forgot string.lower
03.01.16 08:18:28 am
Up
Dousea
User
Offline Off
@user VADemon: Are you praising or insulting ? About the string.lower, put it under line 3.
Code:
1
message = message:lower()
I'm awesome ... and I really like cookies.
03.01.16 08:21:21 am
Up
Zeik
User
Offline Off
user Nekomata has written:
user Dousea has written:
Code:
1
2
3
4
5
6
7
addhook("say", "sayhook")

function sayhook(id, message)
     if (message == "!off" or message == "!on") then
          boolean = message == "!on"
     end
end

That's still going over my head.
Wouldn't variable 'boolean' return the text instead of a true boolean value? How does this work?

LUA - Precedence
I would use parenthesis just to make it clearer tho.

(message == "!on") as a condition, returns true or false, which is a boolean value. Then the condition's result is assigned to a variable. In this case the variable's name is "boolean".
03.01.16 10:23:57 am
Up
GeoB99
Moderator
Offline Off
@user Dousea: Oh, wow that's something new for me. Though I haven't learn many things in Lua, this trick actually intrigued me.
Maybe shouldn't you post in thread cs2d Tricks in CS2D Scripting that you might not know.? Just asking.
03.01.16 11:08:01 am
Up
Dousea
User
Offline Off
@user GeoB99: I thought everyone knows it. It just uses logic.
I'm awesome ... and I really like cookies.
03.01.16 10:43:20 pm
Up
Nekomata
User
Offline Off
Got it, it was definitely intriguing tho.
03.01.16 11:30:31 pm
Up
_Yank
User
Offline Off
You can use this various ways

Code:
1
2
print((type({1,2,3}) == "string") and "this is a string") or "this is not a string" ) -- Prints "this is not a string"
print((type("Hello") == "string") and "this is a string") or "this is not a string" ) -- Prints "this is a string"
04.01.16 03:16:07 am
Up
Infinite Rain
Reviewer
Offline Off
Expressions (x == y) are either true or false. You can set variables via expressions and you can also return the result of an expression. I'm surprised that this many people didn't know that even though it's the basics of how the if statements work.

@user _Yank:
You have a typo in that example. You're opening 3 brackets and closing 4 brackets. Fixed version:
Code:
1
2
print(type({1, 2, 3}) == "string" and "this is a string" or "this is not a string")
print(type("string") == "string" and "this is a string" or "this is not a string")
edited 3×, last 04.01.16 03:38:49 am
A thousand may fall at your side, ten thousand at your right hand, but it will not come near you. You will only look with your eyes and see the recompense of the wicked. - Psalm 91:7-8 ESV
04.01.16 08:29:42 am
Up
Dousea
User
Offline Off
Just an addition, you can't concatenate the expression with other strings without brackets.
Code:
1
2
print("This is " .. type("") == "string" and "" or "not" .. " a string!") -- Wrong!
print("This is " .. (type("") == "string" and "" or "not") .. " a string!") -- Right!

Well I guess we're off-topic so it better ends now.
I'm awesome ... and I really like cookies.
To the start Previous 1 Next To the start