Jump to content

Please help Bad argument @ 'isPedInVehicle'


DeadReacen

Recommended Posts

Hello gentlemen, everything okay?, My problem here is I can't remove the warning from the debug script

function Cavar(playerSource)
	if not isPedInVehicle(playerSource) then
		local Emprego = getElementData(playerSource, "Emprego") or "Desempregado"
		if Emprego == "Albañil" then
			if not isElement(saco[playerSource]) then
				if getPedWeapon(playerSource) == 6 then
					setTimer(function()
					end, 11300, 1)
				else
				end
			end
		end
	end
end
addEventHandler("onMarkerHit", cavar, Cavar)
--For scripter safety I had to delete lines

WARNING: server.lua:48: Bad argument @ 'isPedInVehicle' [Expected ped at argument 1, got vehicle]

   if not playerSource  then  
     return 
   end 
   if isPedInVehicle(playerSource) then
--use these functions but they don't work

The truth is I did everything to try but something else does not occur to me.

I hope you have an idea, I will thank you with all my heart

Link to comment

The event onMarkerHit is triggered anytime an element hits the marker, not only when a player hits a marker. Turns out the vehicle is also an element and usually when a player runs over a marker driving a vehicle the vehicle element hits the marker before the player element.

You have two options:

1. Use onPlayerMarketHit instead. Note it's not a drop in replacement, you'll have to change your code to work with onPlayerMarkerHit
2. At the very top of the Cavar function, check for the element type before executing any action

local function Cavar(hitElement)
  if getElementType(hitElement) ~= "player" then
    return
  end
  
  -- Here you place your current code
end

 

Excuse me to give you a general tip around programming. Avoid nesting to much if statements, always check by the failure condition and return your function.

Dont:

-- Evite muitos IFs encadeados!

local function Cavar(hitElement)
  if getElementType(hitElement) == "player" then
    if not isPedInVehicle(hitElement) then
      --- code ...
    end
  end
end

 

Do:

-- Muito mais elegante

local function Cavar(hitElement)
  if getElementType(hitElement) ~= "player" or
     isPedInVehicle(hitElement)
  then
   	return
  end
  
  -- code ...
end

 

You can find reference for onMarkerHitonPlayerMarkerHit and getElementType on the official wiki.
 

Edited by vicisdev
  • Like 1
Link to comment
34 minutes ago, vicisdev said:

The event onMarkerHit is triggered anytime an element hits the marker, not only when a player hits a marker. Turns out the vehicle is also an element and usually when a player runs over a marker driving a vehicle the vehicle element hits the marker before the player element.

You have two options:

1. Use onPlayerMarketHit instead. Note it's not a drop in replacement, you'll have to change your code to work with onPlayerMarkerHit
2. At the very top of the Cavar function, check for the element type before executing any action

local function Cavar(hitElement)
  if getElementType(hitElement) ~= "player" then
    return
  end
  
  -- Here you place your current code
end

 

Excuse me to give you a general tip around programming. Avoid nesting to much if statements, always check the failure condition and return your function.

Dont:

-- Evite muitos IFs encadeados!

local function Cavar(hitElement)
  if getElementType(hitElement) ~= "player" then
    if not isPedInVehicle(hitElement) then
      --- code ...
    end
  end
end

 

Do:

-- Muito mais elegante

local function Cavar(hitElement)
  if getElementType(hitElement) ~= "player" or
     isPedInVehicle(hitElement)
  then
   	return
  end
  
  -- code ...
end

 

You can find reference for onMarkerHitonPlayerMarkerHit and getElementType on the official wiki.
 

I can't believe it for hours trying to fix it and I couldn't, Thank you very much and what you told me I'll take into account.

Fixed issue Close Post

  • Like 1
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...