TFNRP / seatbelt

Seatbelt script for FiveM, with LEO detection
https://forum.cfx.re/t/4779995
MIT License
10 stars 6 forks source link

Passengers fly out of the car constantly but driver does not #12

Open Legacy-TacticalGamingInteractive opened 1 year ago

Legacy-TacticalGamingInteractive commented 1 year ago

When no seatbelt is on. The passengers fly out of the car randomly. Even without speeding or hitting things. Does anyone know a fix? The driver doesn't fall out... its just the passenger with the issue.

MrGriefs commented 1 year ago

Reproduction steps?

Legacy-TacticalGamingInteractive commented 1 year ago

I haven't really changed much from the base resource I simply installed it as is. Running onesync infinity (if it matters)

I am having so many reports from passengers in vehicle falling out of car.

The driver is fine. The driver seems to respond properly to the handling of the seatbelt function.

Both driver and passenger are not wearing the seatbelt. But only the passenger falls out. This is when driving normally, not crashing or braking hard or anything. It just randomly happens.

My client lua

local showingWarning = false
local lastSpeed = 0
local lastVelocity = vector3(0, 0, 0)
local showHelp = false
local activated
local hasSeatbelt

RegisterKeyMapping('seatbelt', 'Seatbelt', 'keyboard', Constants.DefaultKeybind)
RegisterFrameworkCommand('seatbelt', function()
  local ped = PlayerPedId()
  if IsPedInAnyVehicle(ped) then
    local vehcileHasSeatbelt, strong = DoesPedVehicleHaveSeatbelt(ped)
    if vehcileHasSeatbelt and not strong then
      if activated then
        DeactivateSeatbelt()
      else
        ActivateSeatbelt()
      end
      SendNUIMessage({ State = activated })
    end
  end
end)

function ActivateSeatbelt()
  if activated == true then
    return error('seatbelt attempted to activate when already active.')
  end
  SetWarning(false)
  -- compat for other resources like carhud
  TriggerEvent('seatbelt:stateChange', true)

  -- disable exit keys
  Citizen.CreateThread(function()
    while activated do
      Citizen.Wait(1)
      DisableControlAction(0, 75, true)
      DisableControlAction(27, 75, true)
    end
  end)

  -- quick unbuckled
  Citizen.CreateThread(function()
    while activated do
      Citizen.Wait(1)
      if IsDisabledControlJustPressed(0, 75) and IsControlPressed(0, 21) then
        DeactivateSeatbelt()
      end
    end
  end)

  -- validation
  Citizen.CreateThread(function()
    while activated do
      if not IsPedInAnyVehicle(PlayerPedId()) then
        DeactivateSeatbelt()
      end
      Citizen.Wait(50)
    end
  end)

  activated = true
end

function DeactivateSeatbelt()
  if activated == false then
    return error('seatbelt attempted to deactivate when already deactivated.')
  end
  TriggerEvent('seatbelt:stateChange', false)

  -- HUD
  Citizen.CreateThread(function()
    while not activated do
      local ped = PlayerPedId()
      if IsPedInAnyVehicle(ped) and hasSeatbelt and not IsHudHidden() then
        local vehicle = GetVehiclePedIsIn(ped)
        local speed = GetEntitySpeed(vehicle) * 3.6

        if speed > 20 and not (IsPlayerDead(PlayerId()) or IsPauseMenuActive()) then
          SetWarning(true)
          showHelp = true
        else
          SetWarning(false)
          showHelp = false
        end
      else
        SetWarning(false)
        showHelp = false
      end
      Citizen.Wait(2e3)
    end
  end)

  -- help text separate from hud
  Citizen.CreateThread(function()
    while not activated do
      if showHelp then
        ShowHelpText(message)
        for _ = 0, 8 do
          Citizen.Wait(5)
          ShowHelpText(message, false)        
        end
      end
      Citizen.Wait(65)
    end
  end)

  -- handling
  Citizen.CreateThread(function()
    while not activated do
      local ped = PlayerPedId()
      if IsPedInAnyVehicle(ped) then
        local _hasSeatbelt, strong = DoesPedVehicleHaveSeatbelt(ped)
        hasSeatbelt = _hasSeatbelt
        if hasSeatbelt and not strong then
          local vehicle = GetVehiclePedIsIn(ped)
          local speed = GetEntitySpeed(vehicle)

          if speed > (50 / 3.6) and (lastSpeed - speed) > (speed * .2) then
            local coords = GetEntityCoords(ped)
            local fw = Fwv(ped)
            SetEntityCoords(ped, coords.x + fw.x, coords.y + fw.y, coords.z - .47, true, true, true)
            SetEntityVelocity(ped, lastVelocity.x, lastVelocity.y, lastVelocity.z)
            SetPedToRagdoll(ped, 1e3, 1e3, 0, false, false, false)
          end

          lastSpeed = speed
          lastVelocity = GetEntityVelocity(vehicle)
        end
      end
      Citizen.Wait(50)
    end
  end)

  -- notification
  Citizen.CreateThread(function()
    while not activated do
      local ped = PlayerPedId()
      local vehicle = GetVehiclePedIsIn(ped)
      if IsPedInAnyVehicle(ped) and hasSeatbelt and GetEntitySpeed(vehicle) * 3.6 > 10 then
        TriggerServerEvent('seatbelt:ServerNotify')
      end
      Citizen.Wait(3e3)
    end
  end)

  activated = false
end

function SetWarning(bool)
  if bool ~= showingWarning then
    SendNUIMessage({ Enabled = bool })
    showingWarning = bool
  end
end

RegisterNetEvent('seatbelt:ClientNotify', function(serverId)
  local ped = PlayerPedId()
  local player = GetPlayerFromServerId(serverId) -- onesync notice: returns -1 if not loaded
  local playerPed = GetPlayerPed(player)
  if player ~= PlayerId() and player > 0 and IsLEO() and not IsHudHidden() and IsPedInAnyVehicle(playerPed) then
    local vehicle = GetVehiclePedIsIn(playerPed)
    local identifier = GetPlayerIdentifier_(serverId, playerPed, vehicle)
    if #(GetEntityCoords(ped) - GetEntityCoords(GetPlayerPed(player))) < Constants.Distance and identifier then
      ShowNotification(
        identifier ..
        ' is not weaing a seatbelt in <C>~y~' ..
        GetVehicleNumberPlateText(vehicle) ..
        '~s~</C>.'
      )
    end
  end
end)

Citizen.CreateThread(function()
  DeactivateSeatbelt()
end)
MrGriefs commented 1 year ago

It is strange that drivers are not being ejected where passengers are - this shouldn't be possible as the code is the same.

The likely conclusion is that, whilst the driver is not experiencing hard braking, the passenger is (due to OneSync likely nastily snapping the vehicle into a "correct" position, or perhaps the driver or the passenger themselves have a lower-end system or network).

What version of OneSync is in use here? Perhaps I should read more carefully next time. I'll try to test this myself and find the root cause, but it will take some time.

MrGriefs commented 1 year ago

Have you reproduced this yourself? If so, could you let me know at what speeds the ejection has occurred at.

Legacy-TacticalGamingInteractive commented 1 year ago

Have you reproduced this yourself? If so, could you let me know at what speeds the ejection has occurred at.

I have indeed. I've tested it multiple times myself as the passenger. (Very high end system and 1000mbps internet.) My initial thought was also maybe some weird desync issue we cannot see really even on our end. The funny thing is... even if there is Desync, we dont fall out when Desync happens! Its when driving totally normally. Check this out https://streamable.com/qe0ki1

Perhaps for now I will comment out that function for the handling. As its not usable like this. I love everything else about the script though. I agree its strange. Sorry I don't have the speed in this clip as I'm the passenger.

Snydro commented 1 year ago

Does commenting out the handling fix the issue?