[Help] CreatePedInsideVehicle crashing my game

Hey, i got an error. When i trying to spawn a ped inside my vehicle, are my game crashing. Anyone got an fix on it?

Citizen.CreateThread(function()
  while true do 
      Citizen.Wait(1)
      local player = GetPlayerPed(-1)    
      local coord = GetEntityCoords(player)
      local PedVehicle = GetVehiclePedIsIn(GetPlayerPed(-1), false) 

      while GetDistanceBetweenCoords(1372.6826171875,-1522.6258544922,57.055507659912, GetEntityCoords(GetPlayerPed(-1))) < 5.0 do
          delay = 1 
            Citizen.Wait(delay)
            DrawText3D(1372.6826171875,-1522.6258544922,57.055507659912, "~b~[E]~s~ - Hent Ralles ven")
             if(IsControlJustReleased(1, 51))then
                if hasjob == true then
                  exports['og-progressbar']:StartProgress(15000, 'Henter Ralles ven')
                  Citizen.Wait(15000)
                  CreatePedInsideVehicle(PedVehicle, 4, vehiclehash, 0, true, false)
                  TriggerEvent("pNotify:SendNotification",{text = "Du har hentet Ralles ven. Aflever ham tilbage til Ralle",type = "success",timeout = (6000),layout = "bottomCenter",queue = "fart",animation = {open = "gta_effects_fade_in", close = "gta_effects_fade_out"}})
                  hent = true
                  SetNewWaypoint(315.21597290039,-128.14164733887)
                else
                  TriggerEvent("pNotify:SendNotification",{text = "Du kan ikke hente Ralles ven.",type = "success",timeout = (6000),layout = "bottomCenter",queue = "fart",animation = {open = "gta_effects_fade_in", close = "gta_effects_fade_out"}})
                end
             end
        end
    end
end)

You’re trying to get the vehicle the player is in without checking whether they’re in one.

There’s no need for a nested while loop. You’re calling GetPlayerPed(-1) 3 times and getting the players coords twice in the same thread which is inefficient - use PlayerPedId() instead. You’re using GetDistanceBetweenCoords() which is also inefficient.

Try this:

Citizen.CreateThread(function()
    local coords = vector3(1372.6826171875, -1522.6258544922, 57.055507659912)

    while true do
        Citizen.Wait(0)

        local player    = PlayerPedId()    
        local pcoords   = GetEntityCoords(player)
        local inVehicle = IsPedInAnyVehicle(player, false)

        if inVehicle then -- we're in a vehicle so proceed with checks
            local PedVehicle = GetVehiclePedIsIn(player, false)
            local dist = #(coords - pcoords) -- distance between the given coords and the player

            if dist < 5.0 then -- distance check
                DrawText3D(coords.x, coords.y, coords.z, "~b~[E]~s~ - Hent Ralles ven")

                if IsControlJustReleased(1, 51) then
                    -- keypress logic
                end
            end
        else
            -- Let the thread sleep if we're not in a vehicle
            Citizen.Wait(1000)
        end
    end
end)

Where is CreatePedInsideVehicle?
The thing with this script are, there need to spawn a ped inside a vehicle player is sitting in.

Just replace -- keypress logic with the original keypress code in your post.

It still crashing when it try to spawn a ped inside Ped vehicle
image

Citizen.CreateThread(function()
  local coords = vector3(1372.6826171875, -1522.6258544922, 57.055507659912)

  while true do
      Citizen.Wait(0)

      local player    = PlayerPedId()    
      local pcoords   = GetEntityCoords(player)
      local inVehicle = IsPedInAnyVehicle(player, false)

      if inVehicle then -- we're in a vehicle so proceed with checks
          local PedVehicle = GetVehiclePedIsIn(player, false)
          local dist = #(coords - pcoords) -- distance between the given coords and the player

          if dist < 5.0 then -- distance check
              DrawText3D(coords.x, coords.y, coords.z, "~b~[E]~s~ - Hent Ralles ven")

              if IsControlJustReleased(1, 51) then
                if hasjob == true then
                  exports['og-progressbar']:StartProgress(15000, 'Henter Ralles ven')
                  Citizen.Wait(15000)
                  CreatePedInsideVehicle(PedVehicle, 4, vehiclehash, 0, true, false)
                  TriggerEvent("pNotify:SendNotification",{text = "Du har hentet Ralles ven. Aflever ham tilbage til Ralle",type = "success",timeout = (6000),layout = "bottomCenter",queue = "fart",animation = {open = "gta_effects_fade_in", close = "gta_effects_fade_out"}})
                  hent = true
                  SetNewWaypoint(315.21597290039,-128.14164733887)
                else
                  TriggerEvent("pNotify:SendNotification",{text = "Du kan ikke hente Ralles ven.",type = "success",timeout = (6000),layout = "bottomCenter",queue = "fart",animation = {open = "gta_effects_fade_in", close = "gta_effects_fade_out"}})
                end
              end
          end
      else
          -- Let the thread sleep if we're not in a vehicle
          Citizen.Wait(1000)
      end
  end
end)