Jump to content

Bad argument @ "getPedOccupiedVehicle"


NovaCrew

Recommended Posts

So I have an error that says Bad argument @ "getPedOccupiedVehicle" [Expected ped at argument 1, got nil] and I have no idea how to fix it. If someone could help me I would really appreciate it.

local speed = 20
local buoyancy = 1.5
local drag = 1

function swimVehicle (vehicle, seat, jacked)
	local id = getPedOccupiedVehicle (vehicle)
	if id == 407 then
	  local x, y, z = getElementPosition ( vehicle ) 
      local level = getWaterLevel ( x, y, z )
	  if id == 407 and level and z < level then
		setElementData(vehicle, "buoyancy", 1.5)
		setElementData(vehicle, "vehicleDrag", 1)
		setElementData(vehicle, "centerOfMass", {0,0,-1})
		setVehicleHandling(vehicle, "engineAcceleration", 20)
        else
		setElementData(vehicle, "buoyancy", 0)
		setElementData(vehicle, "vehicleDrag", 0)
		setVehicleHandling(vehicle, "engineAcceleration", 0)
	    end  
	end	
end

 addEventHandler("onClientRender", root, swimVehicle)
  • Like 1
Link to comment
1 hour ago, NovaCrew said:
local id = getPedOccupiedVehicle (vehicle)
	if id == 407 then

getPedOccupiedVehicle() should be receiving LocalPlayer, and i will return the Element Vehicle. Then you can use GetElementID() to compare here.

Something like this i believe: 

local id = getElementID(getPedOccupiedVehicle(LocalPlayer))
if id == '407' then
  --YOUR CODE

 

 

If you have a doubt, just ask or if it helped, you can leave a thanks ?

  • Like 1
Link to comment
33 minutes ago, Rougue90 said:

getPedOccupiedVehicle() should be receiving LocalPlayer, and i will return the Element Vehicle. Then you can use GetElementID() to compare here.

Something like this i believe: 

local id = getElementID(getPedOccupiedVehicle(LocalPlayer))
if id == '407' then
  --YOUR CODE

 

 

If you have a doubt, just ask or if it helped, you can leave a thanks ?

hello there is error in your comment, I recommend you to use getElementModel instead of getElementID

  • Like 1
  • Thanks 1
Link to comment
1 hour ago, Rougue90 said:

Wow, indeed. I don't know why i did that way, Thank you

So the verification must compared to an int value

local id = getElementModel(getPedOccupiedVehicle(LocalPlayer))
if id == 407 then
  --YOUR CODE

 

yes, you can be more careful in such matters, recheck the information about what you did well before saying anything.

I always say, it may be better to test and submit the correct codes, because the codes we publish may contain erroneous or incomplete typos.

When I will share code samples in the forum community I will try the codes on my own test server and share the codes properly to MTA players, thank you for your help, have a nice day

And I can express my special thanks for helping the player.

  • Like 1
Link to comment
local speed = 20
local buoyancy = 1.5
local drag = 1

function swimVehicle()
    local player = localPlayer
    local vehicle = getPedOccupiedVehicle(player)
    
    if vehicle and getElementModel(vehicle) == 407 then
        local x, y, z = getElementPosition(vehicle)
        local level = getWaterLevel(x, y, z)
        
        if z < level then
            setElementData(vehicle, "buoyancy", buoyancy)
            setElementData(vehicle, "vehicleDrag", drag)
            setElementData(vehicle, "centerOfMass", {0, 0, -1})
            setVehicleHandling(vehicle, "engineAcceleration", speed)
        else
            setElementData(vehicle, "buoyancy", 0)
            setElementData(vehicle, "vehicleDrag", 0)
            setVehicleHandling(vehicle, "engineAcceleration", 0)
        end
    end
end

addEventHandler("onClientRender", root, swimVehicle)

 

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...