• Vector.x and Vector.y not working?
    10 replies, posted
Hi facepunch. For some reason this code is not working. [code] local vdiff = pl:GetPos()-LocalPlayer():GetPos() if vdiff.x > 0 and vdiff.y > 0 then local px = (vdiff.x) local py = -(vdiff.y) elseif vdiff.x < 0 and vdiff.y < 0 then local px = -(vdiff.x) local py = (vdiff.y) elseif vdiff.x > 0 and vdiff.y < 0 then local px = (vdiff.x) local py = (vdiff.y) elseif vdiff.x < 0 and vdiff.y > 0 then local px = -(vdiff.x) local py = -(vdiff.y) elseif vdiff.x == 0 and vdiff.y == 0 then local px = 10 local py = 10 end [/code] When I try to use px or py it says it is a nil value, please help!
[QUOTE=WolfNinja2;42921424]Hi facepunch. For some reason this code is not working. [code] local vdiff = pl:GetPos()-LocalPlayer():GetPos() if vdiff.x > 0 and vdiff.y > 0 then local px = (vdiff.x) local py = -(vdiff.y) elseif vdiff.x < 0 and vdiff.y < 0 then local px = -(vdiff.x) local py = (vdiff.y) elseif vdiff.x > 0 and vdiff.y < 0 then local px = (vdiff.x) local py = (vdiff.y) elseif vdiff.x < 0 and vdiff.y > 0 then local px = -(vdiff.x) local py = -(vdiff.y) elseif vdiff.x == 0 and vdiff.y == 0 then local px = 10 local py = 10 end [/code] When I try to use px or py it says it is a nil value, please help![/QUOTE] It is because you keep re-declaring your variables inside each if-block. Using 'local' makes it a local variable, once the current block of code is finished, it is discarded. You can get around it by moving your local declaration to outside the if blocks. [lua]local vdiff = pl:GetPos()-LocalPlayer():GetPos() local px,py if vdiff.x > 0 and vdiff.y > 0 then px = (vdiff.x) py = -(vdiff.y) elseif vdiff.x < 0 and vdiff.y < 0 then px = -(vdiff.x) py = (vdiff.y) elseif vdiff.x > 0 and vdiff.y < 0 then px = (vdiff.x) py = (vdiff.y) elseif vdiff.x < 0 and vdiff.y > 0 then px = -(vdiff.x) py = -(vdiff.y) elseif vdiff.x == 0 and vdiff.y == 0 then px = 10 py = 10 end [/lua] As a simple example, this code would print 'A', not 'B' [lua]local A = 'A' if true then local A = 'B' --this is local to the if-statement end print(A)[/lua]
That was a silly mistake, thank you but it's still saying px is a nil value. [code] local players = player.GetAll() for i, pl in ipairs(players) do local cx = MapX+MapW/2 local cy = MapY+MapH/2 local vdiff = pl:GetPos()-LocalPlayer():GetPos() local px, py if vdiff.x > 0 and vdiff.y > 0 then px = (vdiff.x) py = -(vdiff.y) elseif vdiff.x < 0 and vdiff.y < 0 then px = -(vdiff.x) py = (vdiff.y) elseif vdiff.x > 0 and vdiff.y < 0 then px = (vdiff.x) py = (vdiff.y) elseif vdiff.x < 0 and vdiff.y > 0 then px = -(vdiff.x) py = -(vdiff.y) end --print(px) draw.RoundedBox( 4, cx+px, cy+py, 8, 8, Color(150,150,150,255) ) end [/code]
[QUOTE=WolfNinja2;42921547]That was a silly mistake, thank you but it's still saying px is a nil value. [code] local players = player.GetAll() for i, pl in ipairs(players) do local cx = MapX+MapW/2 local cy = MapY+MapH/2 local vdiff = pl:GetPos()-LocalPlayer():GetPos() local px, py if vdiff.x > 0 and vdiff.y > 0 then px = (vdiff.x) py = -(vdiff.y) elseif vdiff.x < 0 and vdiff.y < 0 then px = -(vdiff.x) py = (vdiff.y) elseif vdiff.x > 0 and vdiff.y < 0 then px = (vdiff.x) py = (vdiff.y) elseif vdiff.x < 0 and vdiff.y > 0 then px = -(vdiff.x) py = -(vdiff.y) end --print(px) draw.RoundedBox( 4, cx+px, cy+py, 8, 8, Color(150,150,150,255) ) end [/code][/QUOTE] Since your if statements don't have an else with no conditions it is possible that px and py are never being set. Your conditions are checking for greater than and less than 0, if they are equal to 0 then px and py are never going to be set. You can either set them to 0 (local px,py = 0,0), or fix up your if statements to cover all possible inputs.
Instead of using strictly less than or strictly greater than, try including <= or >=. I say this because when you loop through the table of players, you will be included in that table. so vdiff will be (0,0,0).
well directly before this i exlude local player to be drawn during this loop, which is why I didn't do <= or >= , what else could be going wrong?
for i, pl in ipairs(players) do if ( pl == LocalPlayer() ) then continue end local cx = MapX+MapW/2
No, I want everything EXCEPT the localplayers, which is whly thlis is so hard.
[QUOTE=WolfNinja2;42923988]No, I want everything EXCEPT the localplayers, which is whly thlis is so hard.[/QUOTE] This is exactly what my code does. If pl == LocalPlayer() then it skips to next thing in the for-loop without executing the code after the if.
[QUOTE=WolfNinja2;42923988]No, I want everything EXCEPT the localplayers, which is whly thlis is so hard.[/QUOTE] Did you even bother to look up what the keyword 'continue' actually does? Wait no, of course you didn't. You thought it was smarter to cook up a stupid response like that instead.
[QUOTE=EvacX;42924189]Did you even bother to look up what the keyword 'continue' actually does? Wait no, of course you didn't. You thought it was smarter to cook up a stupid response like that instead.[/QUOTE] I apolagise for my assumption. It's just I'm very frusterated over this thing. [QUOTE=Robotboy655;42923805]for i, pl in ipairs(players) do if ( pl == LocalPlayer() ) then continue end local cx = MapX+MapW/2[/QUOTE] I am already excluding the LocalPlayer from the for loop, but thanks for the handy little bit of code :) Still having this issue though :P
Sorry, you need to Log In to post a reply to this thread.