• If Statement Not Working on Menu
    7 replies, posted
Alright, I've made it so that the value is == 0, yet it still shows 0 instead of nothing like it's set up to do in this script. [lua]if Item2 == 0 or nil then local iteml2 = vgui.Create( "DLabel", OldItemsFrame ) -- Create the icons in the Model select iteml2:SetText( nothing ) -- This sets the model to the actual path iteml2:SizeToContents() iteml2:SetPos( 5, 40 ) -- This adds the icon else local iteml2 = vgui.Create( "DLabel", OldItemsFrame ) -- Create the icons in the Model select iteml2:SetText( Item2 ) -- This sets the model to the actual path iteml2:SizeToContents() iteml2:SetPos( 5, 40 ) -- This adds the icon end[/lua] How do I fix this?
Well iirc, if a value is 0 and you check against it like its a boolean (even if it isn't necessarily) it will return false. So you can simplify your statement into one check [lua] if not Item2 then --First block of your code else --Second block of your code end [/lua]
[QUOTE=Feihc;28813114]Well iirc, if a value is 0 and you check against it like its a boolean (even if it isn't necessarily) it will return false. So you can simplify your statement into one check [lua] if not Item2 then --First block of your code else --Second block of your code end [/lua][/QUOTE] Lua does not work that way. If a value is 0, then it is 0 so the second block will be called if Item2 is 0. And for your check you could use his code but add the thing for 0 [lua] if not Item2 or (Item2 and Item2 == 0) then -- (Just to make sure that it won't error) --First block of your code else --Second block of your code end [/lua] Wait a second, I just noticed that in your code you are checking if the item is 0 or nil is true it will turn up as nothing, that's a pretty big mistake, since nil will never be checked.
Actually, it does. [url]http://wiki.garrysmod.com/?title=G.tobool[/url] A numeric value of 0 will equate to "false" A numeric value of anything else will equate to "true" [code] ] lua_run print(tobool(1)) > print(tobool(1))... true ] lua_run print(tobool(13)) > print(tobool(13))... true ] lua_run print(tobool(-13)) > print(tobool(-13))... true ] lua_run print(tobool(0)) > print(tobool(0))... false [/code] You don't HAVE to use tobool on it, 0 == false either way.
That's only if you use the function tobool, which isn't needed all times. (Since he wants the first code to be run if the value is 0)
As leiftiger said, the problem is [i]if Item2 == 0 or nil then[/i]. You can't check if a variable is equal to more than one value at once. It should be [i]if Item2 == nil or Item2 == 0 then[/i].
[QUOTE=Feihc;28814012]You don't HAVE to use tobool on it, 0 == false either way.[/QUOTE] [url]http://codepad.org/x6fXdYNf[/url] Enjoy building your box fort.
Ok... So I switched it up to cl_begin.lua [lua]if tobool(Item2) == false or (Item2 and Item2 == 0) then local iteml2 = vgui.Create( "DLabel", OldItemsFrame ) -- Create the icons in the Model select iteml2:SetText( "Nothing" ) -- This sets the model to the actual path iteml2:SizeToContents() iteml2:SetPos( 5, 40 ) -- This adds the icon else local iteml2 = vgui.Create( "DLabel", OldItemsFrame ) -- Create the icons in the Model select iteml2:SetText( Item2 ) -- This sets the model to the actual path iteml2:SizeToContents() iteml2:SetPos( 5, 40 ) -- This adds the icon end[/lua] It works now, thanks guys!
Sorry, you need to Log In to post a reply to this thread.