Forum

> > CS2D > Scripts > Script for do
Forums overviewCS2D overview Scripts overviewLog in to reply

English Script for do

18 replies
To the start Previous 1 Next To the start

old Script for do

Slooper 1
BANNED Off Offline

Quote
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

old Re: Script for do

GeoB99
Moderator Off Offline

Quote
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.

old Re: Script for do

Rainoth
Moderator Off Offline

Quote
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".

old Re: Script for do

Dousea
User Off Offline

Quote
Will do pretty much the same as user GeoB99's and user Rainoth's.
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

old Re: Script for do

Rainoth
Moderator Off Offline

Quote
@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.

old Re: Script for do

Nekomata
User Off Offline

Quote
user Dousea has written
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?

old Re: Script for do

Rainoth
Moderator Off Offline

Quote
@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

old Re: Script for do

Nekomata
User Off Offline

Quote
Ah, I sort of get it now. Also that
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.

old Re: Script for do

Dousea
User Off Offline

Quote
@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.

old Re: Script for do

Dousea
User Off Offline

Quote
@user VADemon: Are you praising or insulting ? About the string.lower, put it under line 3.
1
message = message:lower()

old Re: Script for do

Zeik
User Off Offline

Quote
user Nekomata has written
user Dousea has written
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".

old Re: Script for do

_Yank
User Off Offline

Quote
You can use this various ways

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"

old Re: Script for do

Infinite Rain
Reviewer Off Offline

Quote
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:
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

old Re: Script for do

Dousea
User Off Offline

Quote
Just an addition, you can't concatenate the expression with other strings without brackets.
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.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview