• Any clues as to why this does not work?
    4 replies, posted
[lua] local parameter; timer.Create("check", 0.01, 0, function() if par_name == "x" or "x " then parameter = math.floor(pl:GetPos().x) elseif par_name == "y" or "y " then parameter = math.floor(pl:GetPos().y) elseif par_name == "z" or "z " then parameter = math.floor(pl:GetPos().z) elseif par_name == "angle" or "angle " then parameter = math.floor(pl:GetAngles().y) elseif par_name == "health" or "health " then parameter = pl:Health() end print(parameter.." "..par_name) end ) [/lua] So what I am doing is getting the par_name through a textbox, which all works correctly and prints correctly but when it comes to changing the value of parameter according to par_name it always stays at x, no matter what I put. Any idea why when I change par_name to y that I still end up getting the x value? I assume this should all work. Also is there a more elegant why to do this than a whole lot of if's?
-snip- donno thats messy as balls
[lua]if par_name == "x" or "x " then[/lua] Sadly you can't do that. You need to do it like this.. [lua]if( par_name == "x" or par_name == "x " ) then[/lua] Or an overall better way would be to use some string functions.. [lua]if( par_name:Trim( ):lower( ) == "x" ) then[/lua]
[i]if par_name == "x" or "x "[/i] will [b]always[/b] result in true, so parameter will always be set to x. Use this instead: [lua]local parameter; timer.Create("check", 0.01, 0, function() if par_name == "x" or par_name == "x " then parameter = math.floor(pl:GetPos().x) elseif par_name == "y" or par_name == "y " then parameter = math.floor(pl:GetPos().y) elseif par_name == "z" or par_name == "z " then parameter = math.floor(pl:GetPos().z) elseif par_name == "angle" or par_name == "angle " then parameter = math.floor(pl:GetAngles().y) elseif par_name == "health" or par_name == "health " then parameter = pl:Health() end print(parameter.." "..par_name) end)[/lua] Note: This still isn't the best way to do this, i am just trying to fix your code. [editline]05:02PM[/editline] NINJA'D :v:
ah hah! I see thanks for the quick replies guys! :) What would be the best way to do something this way btw? Cheers.
Sorry, you need to Log In to post a reply to this thread.