2 posts
Posted 07 August 2014 - 01:28 AM
Hey,
not sure if I'm totally stupid atm or if something is very very strange here.
I want to have it like a toggle which saves if it was on/off last time.
And because I know there are some very very smart people here I need to say:
I know you could improve my script, I know it's not 100% like other people would to it but that's my "style" and I hope it's not too hard to understand.
To the problem:
Like you see, for the debugging I added a few "print" statements.
The saving works perfectly, after relog(Singleplayer) it prints out true, if it was on before I left, or false, if it was off.
The problem: It goes to "then" all the time, if it's true or false.
I already tried a few things to fix that, like:
In line 27 and 30 strings except booleans, and in like 8 a few things like if start == true or "true" and things like that. It went to then all the time, haven't seen the computer going to "else" once…
Hope you can help me with this prob! :)/>
Julian
3790 posts
Location
Lincoln, Nebraska
Posted 07 August 2014 - 01:52 AM
Like we see…
You didn't post any code.
154 posts
Location
London, England
Posted 07 August 2014 - 03:18 AM
Just an FYI, when testing for booleans "if bool==true then…" is unnecessary, you can just do "if bool then…"
Are you sure you haven't typo'd? Or declared one of the variables as a local in a different function so your if can't see it and so reads it as nil?
Other than those generalities I can't help without code :)/>
3057 posts
Location
United States of America
Posted 07 August 2014 - 06:06 AM
if start == true or "true" then --#not going to work
if start or start == "true" then --#will work
if start == true or start == "true" then --#will work, but is unnecessary
Short explanation:
if start == true or "true" then
--#get rid of == true
if start or "true" then
--#say start is nil or false
if false or "true" then
--#strings/function/etc. are counted a boolean true, so
if false or true then
--#now take out false
if true then
--#see the problem?
8543 posts
Posted 07 August 2014 - 07:25 AM
Your second one won't work. If it's "false", it would evaluate as true. This is a concern if you're needing to check for boolean strings as well.
2 posts
Posted 07 August 2014 - 09:09 AM
Sorry, was about 2am here when I wrote that and thought my brain was still working, but was not…
After I made this thread I realized I forgot the pastebin code and when I went to bed I thought about it and found out I tried to compare a string with a boolean '-.-
Still thanks for the nice answers :)/>
59 posts
Posted 08 August 2014 - 03:41 AM
Just an FYI, when testing for booleans "if bool==true then…" is unnecessary, you can just do "if bool then…"
To expand on this.
if start then -- is the same as if start == true then
--do stuff for if true
elseif not start then -- is the same as if start == false then
--do stuff for if false
end
Edited on 08 August 2014 - 01:41 AM
7508 posts
Location
Australia
Posted 08 August 2014 - 04:03 AM
Just an FYI, when testing for booleans "if bool==true then…" is unnecessary, you can just do "if bool then…"
To expand on this.
if start then -- is the same as if start == true then
--do stuff for if true
elseif not start then -- is the same as if start == false then
--do stuff for if false
end
that's not quite right.
if start then is not equivalent
if start == true then, it is actually equivalent to
if start ~= nil and start ~= false then. The same can be said for
if not start then, it is not equivalent to
if start == false then, but in fact is equivalent to
if start == nil or start == false then. This is due to the way Lua resolves values in conditionals,
nil and
false both resolve to
false, any other value will resolve to
true.
Edited on 08 August 2014 - 09:53 AM
59 posts
Posted 08 August 2014 - 06:14 AM
Spoiler
Just an FYI, when testing for booleans "if bool==true then…" is unnecessary, you can just do "if bool then…"
To expand on this.
if start then -- is the same as if start == true then
--do stuff for if true
elseif not start then -- is the same as if start == false then
--do stuff for if false
end
that's not quite right.
if start then is not equivalent
if start == true then, it is actually equivalent to
if start ~= nil then. The same can be said for
if not start then, it is not equivalent to
if start == false then, but in fact is equivalent to
if start == nil or start == false then. This is due to the way Lua resolves values in conditionals,
nil and
false both resolve to
false, any other value will resolve to
true.
That is good to know. It was explained to me differently. This makes me question some of the ways that I have used it in the past. Thank you for the information.
656 posts
Posted 08 August 2014 - 11:49 AM
Just an FYI, when testing for booleans "if bool==true then…" is unnecessary, you can just do "if bool then…"
To expand on this.
that's not quite right.
if start then is not equivalent
if start == true then, it is actually equivalent to
if start ~= nil then. The same can be said for
if not start then, it is not equivalent to
if start == false then, but in fact is equivalent to
if start == nil or start == false then. This is due to the way Lua resolves values in conditionals,
nil and
false both resolve to
false, any other value will resolve to
true.
Isn't
if start then
the same as
if start ~= nil and start ~= false
instead of
if start ~= nil
?
Edited on 08 August 2014 - 09:49 AM
7508 posts
Location
Australia
Posted 08 August 2014 - 11:52 AM
-snip-
whoops. good point.