Jump to content

bad argument @ 'getElementPosition'


Recommended Posts

Hey,

I am an incredible noob at lua coding, but im trying to become better at it. So i am trying to create a 3 lane system like (with bound keys "Q" for jumping to left, and "E" for jumping to right.

/debugscript is saying i have a bad argument @ 'getElementPosition' at line 3.

This is my script (client):

  
thePlayer = localPlayer 
vehicle = getPedOccupiedVehicle ( thePlayer ) 
sx,sy,sz = getElementPosition( vehicle ) 
  
  
  
lane1pos = 4941.7998046875  --Put x cordinate of lane 1 here. 
lane2pos = 4931.7001953125  --Put x cordinate of lane 2 here. 
lane3pos = 4921.2001953125  --Put x cordinate of lane 3 here. 
  
  
  
  
bindKey ( "Q", "down", "links" ) 
function links ()       
    if sx == ( lane1pos ) then           
                    setElementPosition( vehicle , lane1pos, sy, sz + 0.05 ) 
                    outputChatBox ("lanepos1") 
                    elseif ( sx == lanepos2 ) then 
                       setElementPosition( vehicle , lane1pos, sy, sz + 0.05 ) 
                        elseif ( sx == lanepos3 ) then 
                            setElementPosition( vehicle , lane2pos, sy, sz + 0.05 ) 
    end 
end 
  
  
  
  
bindKey ( "E", "down", "rechts" ) 
function rechts ()       
    if sx == ( lane1pos ) then           
                    setElementPosition( vehicle , lane2pos, sy, sz + 0.05 ) 
                    elseif ( sx == lanepos2 ) then 
                        setElementPosition( vehicle , lane3pos, sy, sz + 0.05 ) 
                        elseif ( sx == lanepos3 ) then 
                            setElementPosition( vehicle , lane3pos, sy, sz + 0.05 ) 
    end 
end 
  
  

I am not asking for anyone to fix my script, just to tell me what's wrong with it, otherwise i won't learn.

Thanks!

Link to comment
  • Moderators

Well your problem is that a player isn't always in a vehicle. Which means you are trying to get the position from a vehicle which doesn't exist. so before you get it's position do "if vehicle then".

Link to comment

Thanks guys!

I got it working!

lane1pos = 4921.2001953125  --Put x cordinate of lane 1 here. 
lane2pos = 4931.7001953125  --Put x cordinate of lane 2 here. 
lane3pos = 4941.7998046875  --Put x cordinate of lane 3 here. 
spacing = 6 --Put your spacing here (the amount of space each lane counts.) 
  
function autodrive () 
    toggleControl ( "vehicle_left", false ) -- disable the vehicle_left key 
    toggleControl ( "vehicle_right", false ) -- disable the vehicle_right key 
    toggleControl ( "accelerate", false ) -- disable the accelerate key 
    toggleControl ( "vehicle_fire", false ) -- disable fire for NOS 
    toggleControl ( "brake_reverse", false ) -- disable the brake_reverse key 
    toggleControl ( "handbrake", false ) -- disable the handbrake key 
    setControlState ( "accelerate", true ) -- force the accelerate key on 
    setControlState ( "vehicle_fire", true ) -- force the fire (nos) key on 
end 
addEventHandler ( "onClientPreRender", root, autodrive ) 
  
  
  
function links ()       
    local veh = getPedOccupiedVehicle(localPlayer) 
    if (isVehicleBlown( veh ) == false) then       
    local sx,sy,sz = getElementPosition ( veh ) 
    if ( lane1pos - spacing < sx ) and ( lane1pos + spacing > sx ) then  
                    setElementPosition( veh, lane1pos, sy, sz + 0.05 ) 
                    outputChatBox ("lanepos1 links") 
                    elseif ( lane2pos - spacing < sx ) and ( lane2pos + spacing > sx ) then 
                       setElementPosition( veh, lane1pos, sy, sz + 0.05 ) 
                       outputChatBox ("lanepost2 links") 
                        elseif ( lane3pos - spacing < sx ) and ( lane3pos + spacing > sx ) then 
                            setElementPosition( veh, lane2pos, sy, sz + 0.05 ) 
                            outputChatBox ("lanepost2 links 2") 
    end 
end 
end 
bindKey ( "Q", "down", links ) 
  
  
  
  
function rechts ()    
    local veh = getPedOccupiedVehicle(localPlayer) 
    if (isVehicleBlown( veh ) == false) then           
    local sx,sy,sz = getElementPosition ( veh )    
    if ( lane1pos - spacing < sx ) and ( lane1pos + spacing > sx ) then          
                    setElementPosition( veh, lane2pos, sy, sz + 0.05 ) 
                    outputChatBox ("lanepost2 rechts") 
                    elseif ( lane2pos - spacing < sx ) and ( lane2pos + spacing > sx ) then 
                        setElementPosition( veh, lane3pos, sy, sz + 0.05 ) 
                        outputChatBox ("lanepost3 rechts") 
                        elseif ( lane3pos - spacing < sx ) and ( lane3pos + spacing > sx ) then 
                            setElementPosition( veh, lane3pos, sy, sz + 0.05 ) 
                            outputChatBox ("lanepost3 rechts2") 
    end 
end 
end 
bindKey ( "E", "down", rechts ) 
  
  
  
  
  

For some reason i had to call it local otherwise it would give me the same error. If anyone can explain that, i would appreciate it!

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...