Brazzers-Development / brazzers-harness

GNU General Public License v3.0
23 stars 13 forks source link

Fixed a few things, figured might wanna see. #5

Open SQUIDzillaNFT opened 6 months ago

SQUIDzillaNFT commented 6 months ago

I'm too lazy to type it out piece by piece. but here is my seatbelt lua on the latest qb-core. It allows you to use both seatbelt and harness. When I first tried the harness worked but it broke the regular seatbelt. I'm using the get out of car without taking off. It will also play the unbuckle sound when a player exits. Use it if you want, don't if you don't, I left my commented out stuff in also. awesome script though thanks!


local QBCore = exports['qb-core']:GetCoreObject()
local seatbeltOn = false
local harnessOn = false
local harnessHp = Config.HarnessUses
local handbrake = 0
local sleep = 0
local harnessData = {}
local newVehBodyHealth = 0
local currVehBodyHealth = 0
local frameBodyChange = 0
local lastFrameVehSpeed = 0
local lastFrameVehSpeed2 = 0
local thisFrameVehSpeed = 0
local tick = 0
local damageDone = false
local modifierDensity = true
local lastVeh = nil
local veloc

-- Functions

local function ejectFromVehicle()
    local ped = PlayerPedId()
    local veh = GetVehiclePedIsIn(ped, false)
    local coords = GetOffsetFromEntityInWorldCoords(veh, 1.0, 0.0, 1.0)
    SetEntityCoords(ped, coords.x, coords.y, coords.z)
    Wait(1)
    SetPedToRagdoll(ped, 5511, 5511, 0, 0, 0, 0)
    SetEntityVelocity(ped, veloc.x * 4, veloc.y * 4, veloc.z * 4)
    local ejectSpeed = math.ceil(GetEntitySpeed(ped) * 8)
    if GetEntityHealth(ped) - ejectSpeed > 0 then
        SetEntityHealth(ped, GetEntityHealth(ped) - ejectSpeed)
    elseif GetEntityHealth(ped) ~= 0 then
        SetEntityHealth(ped, 0)
    end
end

local function toggleSeatbelt()
    seatbeltOn = not seatbeltOn
    SeatBeltLoop()
    TriggerEvent("seatbelt:client:ToggleSeatbelt")
    TriggerServerEvent("InteractSound_SV:PlayWithinDistance", 5.0, seatbeltOn and "carbuckle" or "carunbuckle", 0.25)
end

local function toggleHarness()
    harnessOn = not harnessOn
    if not harnessOn then return end
    toggleSeatbelt()
end

local function resetHandBrake()
    if handbrake <= 0 then return end
    handbrake -= 1
end

function SeatBeltLoop()
    --[[
    CreateThread(function()
        while true do
            sleep = 0
            if seatbeltOn or harnessOn then
                DisableControlAction(0, 75, true)
                DisableControlAction(27, 75, true)
            end
            if not IsPedInAnyVehicle(PlayerPedId(), false) then
                seatbeltOn = false
                harnessOn = false
                TriggerEvent("seatbelt:client:ToggleSeatbelt")
                break
            end
            if not seatbeltOn and not harnessOn then break end
            Wait(sleep)
        end
    end)
    ]]
    --[[
    CreateThread(function()
        while true do
            sleep = 1000
            if IsPedInAnyVehicle(PlayerPedId(), false) then
                sleep = 0
                if seatbeltOn or harnessOn then
                    if IsControlJustPressed(0, 75) then
                        seatbeltOn = false
                        harnessOn = false
                    end
                end
            else
                seatbeltOn = false
                harnessOn = false
            end
            Wait(sleep)
        end
    end)
    ]]
    CreateThread(function()
        local wasInVehicle = false -- Track whether the player was in a vehicle in the previous frame
        while true do
            local sleep = 1000
            local isInVehicle = IsPedInAnyVehicle(PlayerPedId(), false)
            if isInVehicle then
                sleep = 0
                if seatbeltOn or harnessOn then
                    if IsControlJustPressed(0, 75) then -- Exiting vehicle
                        seatbeltOn = false
                        harnessOn = false
                        -- Trigger the unbuckle sound here because the player is exiting the vehicle
                        TriggerServerEvent("InteractSound_SV:PlayWithinDistance", 5.0, "carunbuckle", 0.25)
                    end
                end
                wasInVehicle = true -- Player is currently in a vehicle, so update the tracker
            else
                if wasInVehicle then -- Player was in a vehicle before but isn't anymore, meaning they've exited
                    if seatbeltOn or harnessOn then -- Play sound only if they had the seatbelt or harness on
                        TriggerServerEvent("InteractSound_SV:PlayWithinDistance", 5.0, "carunbuckle", 0.25)
                    end
                    seatbeltOn = false
                    harnessOn = false
                end
                wasInVehicle = false -- Update the tracker since the player is not in a vehicle now
            end
            Wait(sleep)
        end
    end)

end

-- Export

---Checks whether you have the harness on or not
---@return boolean 
local function hasHarness()
    return harnessOn
end

exports("HasHarness", hasHarness)

-- Ejection Logic

RegisterNetEvent('QBCore:Client:EnteredVehicle', function()
    local ped = PlayerPedId()
    while IsPedInAnyVehicle(ped, false) do
        Wait(0)
        local currVehicle = GetVehiclePedIsIn(ped, false)
        if currVehicle and currVehicle ~= false and currVehicle ~= 0 then
            SetPedHelmet(ped, false)
            lastVeh = GetVehiclePedIsIn(ped, false)
            if GetVehicleEngineHealth(currVehicle) < 0.0 then
                SetVehicleEngineHealth(currVehicle, 0.0)
            end
            if (GetVehicleHandbrake(currVehicle) or (GetVehicleSteeringAngle(currVehicle)) > 25.0 or (GetVehicleSteeringAngle(currVehicle)) < -25.0) then
                if handbrake == 0 then
                    handbrake = 100
                    resetHandBrake()
                else
                    handbrake = 100
                end
            end

            thisFrameVehSpeed = GetEntitySpeed(currVehicle) * 3.6
            currVehBodyHealth = GetVehicleBodyHealth(currVehicle)
            if currVehBodyHealth == 1000 and frameBodyChange ~= 0 then
                frameBodyChange = 0
            end
            if frameBodyChange ~= 0 then
                if lastFrameVehSpeed > 110 and thisFrameVehSpeed < (lastFrameVehSpeed * 0.75) and not damageDone then
                    if frameBodyChange > 18.0 then
                        if not seatbeltOn and not IsThisModelABike(currVehicle) then
                            if math.random(math.ceil(lastFrameVehSpeed)) > 60 then
                                if not harnessOn then
                                    ejectFromVehicle()
                                else
                                    harnessHp -= 1
                                    TriggerServerEvent('seatbelt:DoHarnessDamage', harnessHp, harnessData)
                                end
                            end
                        elseif (seatbeltOn or harnessOn) and not IsThisModelABike(currVehicle) then
                            if lastFrameVehSpeed > 150 then
                                if math.random(math.ceil(lastFrameVehSpeed)) > 150 then
                                    if not harnessOn then
                                        ejectFromVehicle()
                                    else
                                        harnessHp -= 1
                                        TriggerServerEvent('seatbelt:DoHarnessDamage', harnessHp, harnessData)
                                    end
                                end
                            end
                        end
                    else
                        if not seatbeltOn and not IsThisModelABike(currVehicle) then
                            if math.random(math.ceil(lastFrameVehSpeed)) > 60 then
                                if not harnessOn then
                                    ejectFromVehicle()
                                else
                                    harnessHp -= 1
                                    TriggerServerEvent('seatbelt:DoHarnessDamage', harnessHp, harnessData)
                                end
                            end
                        elseif (seatbeltOn or harnessOn) and not IsThisModelABike(currVehicle) then
                            if lastFrameVehSpeed > 120 then
                                if math.random(math.ceil(lastFrameVehSpeed)) > 200 then
                                    if not harnessOn then
                                        ejectFromVehicle()
                                    else
                                        harnessHp -= 1
                                        TriggerServerEvent('seatbelt:DoHarnessDamage', harnessHp, harnessData)
                                    end
                                end
                            end
                        end
                    end
                    damageDone = true
                    SetVehicleEngineOn(currVehicle, false, true, true)
                end
                if currVehBodyHealth < 350.0 and not damageDone then
                    damageDone = true
                    SetVehicleEngineOn(currVehicle, false, true, true)
                    Wait(1000)
                end
            end
            if lastFrameVehSpeed < 100 then
                Wait(100)
                tick = 0
            end
            frameBodyChange = newVehBodyHealth - currVehBodyHealth
            if tick > 0 then
                tick -= 1
                if tick == 1 then
                    lastFrameVehSpeed = GetEntitySpeed(currVehicle) * 3.6
                end
            else
                if damageDone then
                    damageDone = false
                    frameBodyChange = 0
                    lastFrameVehSpeed = GetEntitySpeed(currVehicle) * 3.6
                end
                lastFrameVehSpeed2 = GetEntitySpeed(currVehicle) * 3.6
                if lastFrameVehSpeed2 > lastFrameVehSpeed then
                    lastFrameVehSpeed = GetEntitySpeed(currVehicle) * 3.6
                end
                if lastFrameVehSpeed2 < lastFrameVehSpeed then
                    tick = 25
                end

            end
            if tick < 0 then
                tick = 0
            end
            newVehBodyHealth = GetVehicleBodyHealth(currVehicle)
            if not modifierDensity then
                modifierDensity = true
            end
            veloc = GetEntityVelocity(currVehicle)
        else
            if lastVeh then
                SetPedHelmet(ped, true)
                Wait(200)
                newVehBodyHealth = GetVehicleBodyHealth(lastVeh)
                if not damageDone and newVehBodyHealth < currVehBodyHealth then
                    damageDone = true
                    SetVehicleEngineOn(lastVeh, false, true, true)
                    Wait(1000)
                end
                lastVeh = nil
            end
            lastFrameVehSpeed2 = 0
            lastFrameVehSpeed = 0
            newVehBodyHealth = 0
            currVehBodyHealth = 0
            frameBodyChange = 0
            Wait(2000)
            break
        end
    end
end)

-- Events
--[[
RegisterNetEvent('seatbelt:client:UseHarness', function(ItemData) -- On Item Use (registered server side)
    local ped = PlayerPedId()
    local inVeh = IsPedInAnyVehicle(ped, false)
    local class = GetVehicleClass(GetVehiclePedIsUsing(ped))
    if inVeh and class ~= 8 and class ~= 13 and class ~= 14 then
        if not harnessOn then
            LocalPlayer.state:set("inv_busy", true, true)
            QBCore.Functions.Progressbar("harness_equip", Lang:t('seatbelt.use_harness_progress'), 5000, false, true, {
                disableMovement = false,
                disableCarMovement = false,
                disableMouse = false,
                disableCombat = true,
            }, {}, {}, {}, function()
                LocalPlayer.state:set("inv_busy", false, true)
                toggleHarness()
                TriggerServerEvent('equip:harness', ItemData)
            end)
            harnessHp = ItemData.info.uses
            harnessData = ItemData
            TriggerEvent('hud:client:UpdateHarness', harnessHp)
        else
            LocalPlayer.state:set("inv_busy", true, true)
            QBCore.Functions.Progressbar("harness_equip", Lang:t('seatbelt.remove_harness_progress'), 5000, false, true, {
                disableMovement = false,
                disableCarMovement = false,
                disableMouse = false,
                disableCombat = true,
            }, {}, {}, {}, function()
                LocalPlayer.state:set("inv_busy", false, true)
                toggleHarness()
            end)
        end
    else
        QBCore.Functions.Notify(Lang:t('seatbelt.no_car'), 'error')
    end
end)
]]

RegisterNetEvent('seatbelt:client:UseHarness', function(ItemData, updateInfo) -- On Item Use (registered server side)
    local ped = PlayerPedId()
    local inveh = IsPedInAnyVehicle(ped, false)
    local class = GetVehicleClass(GetVehiclePedIsUsing(ped))
    if inveh and class ~= 8 and class ~= 13 and class ~= 14 then
        if not harnessOn then
            LocalPlayer.state:set("inv_busy", true, true)
            QBCore.Functions.Progressbar("harness_equip", "Attaching Race Harness", 1000, false, true, {
                disableMovement = false,
                disableCarMovement = false,
                disableMouse = false,
                disableCombat = true,
            }, {}, {}, {}, function()
                LocalPlayer.state:set("inv_busy", false, true)
                toggleHarness()
                if updateInfo then TriggerServerEvent('equip:harness', ItemData) end
            end)
            if updateInfo then
                harnessHp = ItemData.info.uses
                harnessData = ItemData
                TriggerEvent('hud:client:UpdateHarness', harnessHp)
            end
        else
            harnessOn = false
            ToggleSeatbelt()
        end
    else
        QBCore.Functions.Notify('You\'re not in a car.', 'error')
    end
end)

--- to try making seatbelt and harness work
RegisterNetEvent('seatbelt:client:UseSeatbelt', function()
    toggleSeatbelt() -- Assuming this toggles the seatbelt on or off.
end)

-- Register Key
--[[
RegisterCommand('toggleseatbelt', function()
    if not IsPedInAnyVehicle(PlayerPedId(), false) or IsPauseMenuActive() then return end
    local class = GetVehicleClass(GetVehiclePedIsUsing(PlayerPedId()))
    if class == 8 or class == 13 or class == 14 then return end
    toggleSeatbelt()
end, false)

RegisterKeyMapping('toggleseatbelt', 'Toggle Seatbelt', 'keyboard', 'B')

]]
RegisterCommand('toggleseatbelt', function()
    if not IsPedInAnyVehicle(PlayerPedId(), false) or IsPauseMenuActive() then return end
    local class = GetVehicleClass(GetVehiclePedIsUsing(PlayerPedId()))
    if class == 8 or class == 13 or class == 14 then return end
    local vehicle = GetVehiclePedIsIn(PlayerPedId(), false)
    local plate = QBCore.Functions.GetPlate(vehicle)

    TriggerServerEvent('brazzers-harness:server:toggleBelt', plate)
end, false)

RegisterKeyMapping('toggleseatbelt', 'Toggle Seatbelt', 'keyboard', 'B')
`
Psycho66626 commented 2 months ago

I'm too lazy to type it out piece by piece. but here is my seatbelt lua on the latest qb-core. It allows you to use both seatbelt and harness. When I first tried the harness worked but it broke the regular seatbelt. I'm using the get out of car without taking off. It will also play the unbuckle sound when a player exits. Use it if you want, don't if you don't, I left my commented out stuff in also. awesome script though thanks!

local QBCore = exports['qb-core']:GetCoreObject()
local seatbeltOn = false
local harnessOn = false
local harnessHp = Config.HarnessUses
local handbrake = 0
local sleep = 0
local harnessData = {}
local newVehBodyHealth = 0
local currVehBodyHealth = 0
local frameBodyChange = 0
local lastFrameVehSpeed = 0
local lastFrameVehSpeed2 = 0
local thisFrameVehSpeed = 0
local tick = 0
local damageDone = false
local modifierDensity = true
local lastVeh = nil
local veloc

-- Functions

local function ejectFromVehicle()
    local ped = PlayerPedId()
    local veh = GetVehiclePedIsIn(ped, false)
    local coords = GetOffsetFromEntityInWorldCoords(veh, 1.0, 0.0, 1.0)
    SetEntityCoords(ped, coords.x, coords.y, coords.z)
    Wait(1)
    SetPedToRagdoll(ped, 5511, 5511, 0, 0, 0, 0)
    SetEntityVelocity(ped, veloc.x * 4, veloc.y * 4, veloc.z * 4)
    local ejectSpeed = math.ceil(GetEntitySpeed(ped) * 8)
    if GetEntityHealth(ped) - ejectSpeed > 0 then
        SetEntityHealth(ped, GetEntityHealth(ped) - ejectSpeed)
    elseif GetEntityHealth(ped) ~= 0 then
        SetEntityHealth(ped, 0)
    end
end

local function toggleSeatbelt()
    seatbeltOn = not seatbeltOn
    SeatBeltLoop()
    TriggerEvent("seatbelt:client:ToggleSeatbelt")
    TriggerServerEvent("InteractSound_SV:PlayWithinDistance", 5.0, seatbeltOn and "carbuckle" or "carunbuckle", 0.25)
end

local function toggleHarness()
    harnessOn = not harnessOn
    if not harnessOn then return end
    toggleSeatbelt()
end

local function resetHandBrake()
    if handbrake <= 0 then return end
    handbrake -= 1
end

function SeatBeltLoop()
    --[[
    CreateThread(function()
        while true do
            sleep = 0
            if seatbeltOn or harnessOn then
                DisableControlAction(0, 75, true)
                DisableControlAction(27, 75, true)
            end
            if not IsPedInAnyVehicle(PlayerPedId(), false) then
                seatbeltOn = false
                harnessOn = false
                TriggerEvent("seatbelt:client:ToggleSeatbelt")
                break
            end
            if not seatbeltOn and not harnessOn then break end
            Wait(sleep)
        end
    end)
    ]]
    --[[
    CreateThread(function()
        while true do
            sleep = 1000
            if IsPedInAnyVehicle(PlayerPedId(), false) then
                sleep = 0
                if seatbeltOn or harnessOn then
                    if IsControlJustPressed(0, 75) then
                        seatbeltOn = false
                        harnessOn = false
                    end
                end
            else
                seatbeltOn = false
                harnessOn = false
            end
            Wait(sleep)
        end
    end)
    ]]
    CreateThread(function()
        local wasInVehicle = false -- Track whether the player was in a vehicle in the previous frame
        while true do
            local sleep = 1000
            local isInVehicle = IsPedInAnyVehicle(PlayerPedId(), false)
            if isInVehicle then
                sleep = 0
                if seatbeltOn or harnessOn then
                    if IsControlJustPressed(0, 75) then -- Exiting vehicle
                        seatbeltOn = false
                        harnessOn = false
                        -- Trigger the unbuckle sound here because the player is exiting the vehicle
                        TriggerServerEvent("InteractSound_SV:PlayWithinDistance", 5.0, "carunbuckle", 0.25)
                    end
                end
                wasInVehicle = true -- Player is currently in a vehicle, so update the tracker
            else
                if wasInVehicle then -- Player was in a vehicle before but isn't anymore, meaning they've exited
                    if seatbeltOn or harnessOn then -- Play sound only if they had the seatbelt or harness on
                        TriggerServerEvent("InteractSound_SV:PlayWithinDistance", 5.0, "carunbuckle", 0.25)
                    end
                    seatbeltOn = false
                    harnessOn = false
                end
                wasInVehicle = false -- Update the tracker since the player is not in a vehicle now
            end
            Wait(sleep)
        end
    end)

end

-- Export

---Checks whether you have the harness on or not
---@return boolean 
local function hasHarness()
    return harnessOn
end

exports("HasHarness", hasHarness)

-- Ejection Logic

RegisterNetEvent('QBCore:Client:EnteredVehicle', function()
    local ped = PlayerPedId()
    while IsPedInAnyVehicle(ped, false) do
        Wait(0)
        local currVehicle = GetVehiclePedIsIn(ped, false)
        if currVehicle and currVehicle ~= false and currVehicle ~= 0 then
            SetPedHelmet(ped, false)
            lastVeh = GetVehiclePedIsIn(ped, false)
            if GetVehicleEngineHealth(currVehicle) < 0.0 then
                SetVehicleEngineHealth(currVehicle, 0.0)
            end
            if (GetVehicleHandbrake(currVehicle) or (GetVehicleSteeringAngle(currVehicle)) > 25.0 or (GetVehicleSteeringAngle(currVehicle)) < -25.0) then
                if handbrake == 0 then
                    handbrake = 100
                    resetHandBrake()
                else
                    handbrake = 100
                end
            end

            thisFrameVehSpeed = GetEntitySpeed(currVehicle) * 3.6
            currVehBodyHealth = GetVehicleBodyHealth(currVehicle)
            if currVehBodyHealth == 1000 and frameBodyChange ~= 0 then
                frameBodyChange = 0
            end
            if frameBodyChange ~= 0 then
                if lastFrameVehSpeed > 110 and thisFrameVehSpeed < (lastFrameVehSpeed * 0.75) and not damageDone then
                    if frameBodyChange > 18.0 then
                        if not seatbeltOn and not IsThisModelABike(currVehicle) then
                            if math.random(math.ceil(lastFrameVehSpeed)) > 60 then
                                if not harnessOn then
                                    ejectFromVehicle()
                                else
                                    harnessHp -= 1
                                    TriggerServerEvent('seatbelt:DoHarnessDamage', harnessHp, harnessData)
                                end
                            end
                        elseif (seatbeltOn or harnessOn) and not IsThisModelABike(currVehicle) then
                            if lastFrameVehSpeed > 150 then
                                if math.random(math.ceil(lastFrameVehSpeed)) > 150 then
                                    if not harnessOn then
                                        ejectFromVehicle()
                                    else
                                        harnessHp -= 1
                                        TriggerServerEvent('seatbelt:DoHarnessDamage', harnessHp, harnessData)
                                    end
                                end
                            end
                        end
                    else
                        if not seatbeltOn and not IsThisModelABike(currVehicle) then
                            if math.random(math.ceil(lastFrameVehSpeed)) > 60 then
                                if not harnessOn then
                                    ejectFromVehicle()
                                else
                                    harnessHp -= 1
                                    TriggerServerEvent('seatbelt:DoHarnessDamage', harnessHp, harnessData)
                                end
                            end
                        elseif (seatbeltOn or harnessOn) and not IsThisModelABike(currVehicle) then
                            if lastFrameVehSpeed > 120 then
                                if math.random(math.ceil(lastFrameVehSpeed)) > 200 then
                                    if not harnessOn then
                                        ejectFromVehicle()
                                    else
                                        harnessHp -= 1
                                        TriggerServerEvent('seatbelt:DoHarnessDamage', harnessHp, harnessData)
                                    end
                                end
                            end
                        end
                    end
                    damageDone = true
                    SetVehicleEngineOn(currVehicle, false, true, true)
                end
                if currVehBodyHealth < 350.0 and not damageDone then
                    damageDone = true
                    SetVehicleEngineOn(currVehicle, false, true, true)
                    Wait(1000)
                end
            end
            if lastFrameVehSpeed < 100 then
                Wait(100)
                tick = 0
            end
            frameBodyChange = newVehBodyHealth - currVehBodyHealth
            if tick > 0 then
                tick -= 1
                if tick == 1 then
                    lastFrameVehSpeed = GetEntitySpeed(currVehicle) * 3.6
                end
            else
                if damageDone then
                    damageDone = false
                    frameBodyChange = 0
                    lastFrameVehSpeed = GetEntitySpeed(currVehicle) * 3.6
                end
                lastFrameVehSpeed2 = GetEntitySpeed(currVehicle) * 3.6
                if lastFrameVehSpeed2 > lastFrameVehSpeed then
                    lastFrameVehSpeed = GetEntitySpeed(currVehicle) * 3.6
                end
                if lastFrameVehSpeed2 < lastFrameVehSpeed then
                    tick = 25
                end

            end
            if tick < 0 then
                tick = 0
            end
            newVehBodyHealth = GetVehicleBodyHealth(currVehicle)
            if not modifierDensity then
                modifierDensity = true
            end
            veloc = GetEntityVelocity(currVehicle)
        else
            if lastVeh then
                SetPedHelmet(ped, true)
                Wait(200)
                newVehBodyHealth = GetVehicleBodyHealth(lastVeh)
                if not damageDone and newVehBodyHealth < currVehBodyHealth then
                    damageDone = true
                    SetVehicleEngineOn(lastVeh, false, true, true)
                    Wait(1000)
                end
                lastVeh = nil
            end
            lastFrameVehSpeed2 = 0
            lastFrameVehSpeed = 0
            newVehBodyHealth = 0
            currVehBodyHealth = 0
            frameBodyChange = 0
            Wait(2000)
            break
        end
    end
end)

-- Events
--[[
RegisterNetEvent('seatbelt:client:UseHarness', function(ItemData) -- On Item Use (registered server side)
    local ped = PlayerPedId()
    local inVeh = IsPedInAnyVehicle(ped, false)
    local class = GetVehicleClass(GetVehiclePedIsUsing(ped))
    if inVeh and class ~= 8 and class ~= 13 and class ~= 14 then
        if not harnessOn then
            LocalPlayer.state:set("inv_busy", true, true)
            QBCore.Functions.Progressbar("harness_equip", Lang:t('seatbelt.use_harness_progress'), 5000, false, true, {
                disableMovement = false,
                disableCarMovement = false,
                disableMouse = false,
                disableCombat = true,
            }, {}, {}, {}, function()
                LocalPlayer.state:set("inv_busy", false, true)
                toggleHarness()
                TriggerServerEvent('equip:harness', ItemData)
            end)
            harnessHp = ItemData.info.uses
            harnessData = ItemData
            TriggerEvent('hud:client:UpdateHarness', harnessHp)
        else
            LocalPlayer.state:set("inv_busy", true, true)
            QBCore.Functions.Progressbar("harness_equip", Lang:t('seatbelt.remove_harness_progress'), 5000, false, true, {
                disableMovement = false,
                disableCarMovement = false,
                disableMouse = false,
                disableCombat = true,
            }, {}, {}, {}, function()
                LocalPlayer.state:set("inv_busy", false, true)
                toggleHarness()
            end)
        end
    else
        QBCore.Functions.Notify(Lang:t('seatbelt.no_car'), 'error')
    end
end)
]]

RegisterNetEvent('seatbelt:client:UseHarness', function(ItemData, updateInfo) -- On Item Use (registered server side)
    local ped = PlayerPedId()
    local inveh = IsPedInAnyVehicle(ped, false)
    local class = GetVehicleClass(GetVehiclePedIsUsing(ped))
    if inveh and class ~= 8 and class ~= 13 and class ~= 14 then
        if not harnessOn then
            LocalPlayer.state:set("inv_busy", true, true)
            QBCore.Functions.Progressbar("harness_equip", "Attaching Race Harness", 1000, false, true, {
                disableMovement = false,
                disableCarMovement = false,
                disableMouse = false,
                disableCombat = true,
            }, {}, {}, {}, function()
                LocalPlayer.state:set("inv_busy", false, true)
                toggleHarness()
                if updateInfo then TriggerServerEvent('equip:harness', ItemData) end
            end)
            if updateInfo then
                harnessHp = ItemData.info.uses
                harnessData = ItemData
                TriggerEvent('hud:client:UpdateHarness', harnessHp)
            end
        else
            harnessOn = false
            ToggleSeatbelt()
        end
    else
        QBCore.Functions.Notify('You\'re not in a car.', 'error')
    end
end)

--- to try making seatbelt and harness work
RegisterNetEvent('seatbelt:client:UseSeatbelt', function()
    toggleSeatbelt() -- Assuming this toggles the seatbelt on or off.
end)

-- Register Key
--[[
RegisterCommand('toggleseatbelt', function()
    if not IsPedInAnyVehicle(PlayerPedId(), false) or IsPauseMenuActive() then return end
    local class = GetVehicleClass(GetVehiclePedIsUsing(PlayerPedId()))
    if class == 8 or class == 13 or class == 14 then return end
    toggleSeatbelt()
end, false)

RegisterKeyMapping('toggleseatbelt', 'Toggle Seatbelt', 'keyboard', 'B')

]]
RegisterCommand('toggleseatbelt', function()
    if not IsPedInAnyVehicle(PlayerPedId(), false) or IsPauseMenuActive() then return end
    local class = GetVehicleClass(GetVehiclePedIsUsing(PlayerPedId()))
    if class == 8 or class == 13 or class == 14 then return end
    local vehicle = GetVehiclePedIsIn(PlayerPedId(), false)
    local plate = QBCore.Functions.GetPlate(vehicle)

    TriggerServerEvent('brazzers-harness:server:toggleBelt', plate)
end, false)

RegisterKeyMapping('toggleseatbelt', 'Toggle Seatbelt', 'keyboard', 'B')
`

yeh, it works so much better! Thank you so much!

Psycho66626 commented 2 months ago

but i'm facing this error

image

CPatelCLT commented 1 month ago

but i'm facing this error

image

On line 341, there's a slight typo. Replace the ToggleSeatbelt() with toggleSeatbelt() to match the function name it's meant to be referencing

SQUIDzillaNFT commented 1 month ago

Heres the updated version i fixed my typo in. I forgot to update it here after it was fixed, thanks for the reminder.


local QBCore = exports['qb-core']:GetCoreObject()
local seatbeltOn = false
local harnessOn = false
local harnessHp = Config.HarnessUses
local handbrake = 0
local sleep = 0
local harnessData = {}
local newVehBodyHealth = 0
local currVehBodyHealth = 0
local frameBodyChange = 0
local lastFrameVehSpeed = 0
local lastFrameVehSpeed2 = 0
local thisFrameVehSpeed = 0
local tick = 0
local damageDone = false
local modifierDensity = true
local lastVeh = nil
local veloc

-- Functions

local function ejectFromVehicle()
    local ped = PlayerPedId()
    local veh = GetVehiclePedIsIn(ped, false)
    local coords = GetOffsetFromEntityInWorldCoords(veh, 1.0, 0.0, 1.0)
    SetEntityCoords(ped, coords.x, coords.y, coords.z)
    Wait(1)
    SetPedToRagdoll(ped, 5511, 5511, 0, 0, 0, 0)
    SetEntityVelocity(ped, veloc.x * 4, veloc.y * 4, veloc.z * 4)
    local ejectSpeed = math.ceil(GetEntitySpeed(ped) * 8)
    if GetEntityHealth(ped) - ejectSpeed > 0 then
        SetEntityHealth(ped, GetEntityHealth(ped) - ejectSpeed)
    elseif GetEntityHealth(ped) ~= 0 then
        SetEntityHealth(ped, 0)
    end
end

local function toggleSeatbelt()
    seatbeltOn = not seatbeltOn
    SeatBeltLoop()
    TriggerEvent("seatbelt:client:ToggleSeatbelt")
    TriggerServerEvent("InteractSound_SV:PlayWithinDistance", 5.0, seatbeltOn and "carbuckle" or "carunbuckle", 0.25)
end

local function toggleHarness()
    harnessOn = not harnessOn
    if not harnessOn then return end
    toggleSeatbelt()
end

local function resetHandBrake()
    if handbrake <= 0 then return end
    handbrake -= 1
end

function SeatBeltLoop()
    --[[
    CreateThread(function()
        while true do
            sleep = 0
            if seatbeltOn or harnessOn then
                DisableControlAction(0, 75, true)
                DisableControlAction(27, 75, true)
            end
            if not IsPedInAnyVehicle(PlayerPedId(), false) then
                seatbeltOn = false
                harnessOn = false
                TriggerEvent("seatbelt:client:ToggleSeatbelt")
                break
            end
            if not seatbeltOn and not harnessOn then break end
            Wait(sleep)
        end
    end)
    ]]
    --[[
    CreateThread(function()
        while true do
            sleep = 1000
            if IsPedInAnyVehicle(PlayerPedId(), false) then
                sleep = 0
                if seatbeltOn or harnessOn then
                    if IsControlJustPressed(0, 75) then
                        seatbeltOn = false
                        harnessOn = false
                    end
                end
            else
                seatbeltOn = false
                harnessOn = false
            end
            Wait(sleep)
        end
    end)
    ]]
    CreateThread(function()
        local wasInVehicle = false -- Track whether the player was in a vehicle in the previous frame
        while true do
            local sleep = 1000
            local isInVehicle = IsPedInAnyVehicle(PlayerPedId(), false)
            if isInVehicle then
                sleep = 0
                if seatbeltOn or harnessOn then
                    if IsControlJustPressed(0, 75) then -- Exiting vehicle
                        seatbeltOn = false
                        harnessOn = false
                        -- Trigger the unbuckle sound here because the player is exiting the vehicle
                        TriggerServerEvent("InteractSound_SV:PlayWithinDistance", 5.0, "carunbuckle", 0.25)
                    end
                end
                wasInVehicle = true -- Player is currently in a vehicle, so update the tracker
            else
                if wasInVehicle then -- Player was in a vehicle before but isn't anymore, meaning they've exited
                    if seatbeltOn or harnessOn then -- Play sound only if they had the seatbelt or harness on
                        TriggerServerEvent("InteractSound_SV:PlayWithinDistance", 5.0, "carunbuckle", 0.25)
                    end
                    seatbeltOn = false
                    harnessOn = false
                end
                wasInVehicle = false -- Update the tracker since the player is not in a vehicle now
            end
            Wait(sleep)
        end
    end)

end

-- Export

---Checks whether you have the harness on or not
---@return boolean 
local function hasHarness()
    return harnessOn
end

exports("HasHarness", hasHarness)

-- Ejection Logic

RegisterNetEvent('QBCore:Client:EnteredVehicle', function()
    local ped = PlayerPedId()
    while IsPedInAnyVehicle(ped, false) do
        Wait(0)
        local currVehicle = GetVehiclePedIsIn(ped, false)
        if currVehicle and currVehicle ~= false and currVehicle ~= 0 then
            SetPedHelmet(ped, false)
            lastVeh = GetVehiclePedIsIn(ped, false)
            if GetVehicleEngineHealth(currVehicle) < 0.0 then
                SetVehicleEngineHealth(currVehicle, 0.0)
            end
            if (GetVehicleHandbrake(currVehicle) or (GetVehicleSteeringAngle(currVehicle)) > 25.0 or (GetVehicleSteeringAngle(currVehicle)) < -25.0) then
                if handbrake == 0 then
                    handbrake = 100
                    resetHandBrake()
                else
                    handbrake = 100
                end
            end

            thisFrameVehSpeed = GetEntitySpeed(currVehicle) * 3.6
            currVehBodyHealth = GetVehicleBodyHealth(currVehicle)
            if currVehBodyHealth == 1000 and frameBodyChange ~= 0 then
                frameBodyChange = 0
            end
            if frameBodyChange ~= 0 then
                if lastFrameVehSpeed > 110 and thisFrameVehSpeed < (lastFrameVehSpeed * 0.75) and not damageDone then
                    if frameBodyChange > 18.0 then
                        if not seatbeltOn and not IsThisModelABike(currVehicle) then
                            if math.random(math.ceil(lastFrameVehSpeed)) > 60 then
                                if not harnessOn then
                                    ejectFromVehicle()
                                else
                                    harnessHp -= 1
                                    TriggerServerEvent('seatbelt:DoHarnessDamage', harnessHp, harnessData)
                                end
                            end
                        elseif (seatbeltOn or harnessOn) and not IsThisModelABike(currVehicle) then
                            if lastFrameVehSpeed > 150 then
                                if math.random(math.ceil(lastFrameVehSpeed)) > 150 then
                                    if not harnessOn then
                                        ejectFromVehicle()
                                    else
                                        harnessHp -= 1
                                        TriggerServerEvent('seatbelt:DoHarnessDamage', harnessHp, harnessData)
                                    end
                                end
                            end
                        end
                    else
                        if not seatbeltOn and not IsThisModelABike(currVehicle) then
                            if math.random(math.ceil(lastFrameVehSpeed)) > 60 then
                                if not harnessOn then
                                    ejectFromVehicle()
                                else
                                    harnessHp -= 1
                                    TriggerServerEvent('seatbelt:DoHarnessDamage', harnessHp, harnessData)
                                end
                            end
                        elseif (seatbeltOn or harnessOn) and not IsThisModelABike(currVehicle) then
                            if lastFrameVehSpeed > 120 then
                                if math.random(math.ceil(lastFrameVehSpeed)) > 200 then
                                    if not harnessOn then
                                        ejectFromVehicle()
                                    else
                                        harnessHp -= 1
                                        TriggerServerEvent('seatbelt:DoHarnessDamage', harnessHp, harnessData)
                                    end
                                end
                            end
                        end
                    end
                    damageDone = true
                    SetVehicleEngineOn(currVehicle, false, true, true)
                end
                if currVehBodyHealth < 350.0 and not damageDone then
                    damageDone = true
                    SetVehicleEngineOn(currVehicle, false, true, true)
                    Wait(1000)
                end
            end
            if lastFrameVehSpeed < 100 then
                Wait(100)
                tick = 0
            end
            frameBodyChange = newVehBodyHealth - currVehBodyHealth
            if tick > 0 then
                tick -= 1
                if tick == 1 then
                    lastFrameVehSpeed = GetEntitySpeed(currVehicle) * 3.6
                end
            else
                if damageDone then
                    damageDone = false
                    frameBodyChange = 0
                    lastFrameVehSpeed = GetEntitySpeed(currVehicle) * 3.6
                end
                lastFrameVehSpeed2 = GetEntitySpeed(currVehicle) * 3.6
                if lastFrameVehSpeed2 > lastFrameVehSpeed then
                    lastFrameVehSpeed = GetEntitySpeed(currVehicle) * 3.6
                end
                if lastFrameVehSpeed2 < lastFrameVehSpeed then
                    tick = 25
                end

            end
            if tick < 0 then
                tick = 0
            end
            newVehBodyHealth = GetVehicleBodyHealth(currVehicle)
            if not modifierDensity then
                modifierDensity = true
            end
            veloc = GetEntityVelocity(currVehicle)
        else
            if lastVeh then
                SetPedHelmet(ped, true)
                Wait(200)
                newVehBodyHealth = GetVehicleBodyHealth(lastVeh)
                if not damageDone and newVehBodyHealth < currVehBodyHealth then
                    damageDone = true
                    SetVehicleEngineOn(lastVeh, false, true, true)
                    Wait(1000)
                end
                lastVeh = nil
            end
            lastFrameVehSpeed2 = 0
            lastFrameVehSpeed = 0
            newVehBodyHealth = 0
            currVehBodyHealth = 0
            frameBodyChange = 0
            Wait(2000)
            break
        end
    end
end)

-- Events
--[[
RegisterNetEvent('seatbelt:client:UseHarness', function(ItemData) -- On Item Use (registered server side)
    local ped = PlayerPedId()
    local inVeh = IsPedInAnyVehicle(ped, false)
    local class = GetVehicleClass(GetVehiclePedIsUsing(ped))
    if inVeh and class ~= 8 and class ~= 13 and class ~= 14 then
        if not harnessOn then
            LocalPlayer.state:set("inv_busy", true, true)
            QBCore.Functions.Progressbar("harness_equip", Lang:t('seatbelt.use_harness_progress'), 5000, false, true, {
                disableMovement = false,
                disableCarMovement = false,
                disableMouse = false,
                disableCombat = true,
            }, {}, {}, {}, function()
                LocalPlayer.state:set("inv_busy", false, true)
                toggleHarness()
                TriggerServerEvent('equip:harness', ItemData)
            end)
            harnessHp = ItemData.info.uses
            harnessData = ItemData
            TriggerEvent('hud:client:UpdateHarness', harnessHp)
        else
            LocalPlayer.state:set("inv_busy", true, true)
            QBCore.Functions.Progressbar("harness_equip", Lang:t('seatbelt.remove_harness_progress'), 5000, false, true, {
                disableMovement = false,
                disableCarMovement = false,
                disableMouse = false,
                disableCombat = true,
            }, {}, {}, {}, function()
                LocalPlayer.state:set("inv_busy", false, true)
                toggleHarness()
            end)
        end
    else
        QBCore.Functions.Notify(Lang:t('seatbelt.no_car'), 'error')
    end
end)
]]

RegisterNetEvent('seatbelt:client:UseHarness', function(ItemData, updateInfo) -- On Item Use (registered server side)
    local ped = PlayerPedId()
    local inveh = IsPedInAnyVehicle(ped, false)
    local class = GetVehicleClass(GetVehiclePedIsUsing(ped))
    if inveh and class ~= 8 and class ~= 13 and class ~= 14 then
        if not harnessOn then
            LocalPlayer.state:set("inv_busy", true, true)
            QBCore.Functions.Progressbar("harness_equip", "Attaching Race Harness", 1000, false, true, {
                disableMovement = false,
                disableCarMovement = false,
                disableMouse = false,
                disableCombat = true,
            }, {}, {}, {}, function()
                LocalPlayer.state:set("inv_busy", false, true)
                toggleHarness()
                if updateInfo then TriggerServerEvent('equip:harness', ItemData) end
            end)
            if updateInfo then
                harnessHp = ItemData.info.uses
                harnessData = ItemData
                TriggerEvent('hud:client:UpdateHarness', harnessHp)
            end
        else
            harnessOn = false
            toggleSeatbelt()
        end
    else
        QBCore.Functions.Notify('You\'re not in a car.', 'error')
    end
end)

--- to try making seatbelt and harness work
RegisterNetEvent('seatbelt:client:UseSeatbelt', function()
    toggleSeatbelt() -- Assuming this toggles the seatbelt on or off.
end)

-- Register Key
--[[
RegisterCommand('toggleseatbelt', function()
    if not IsPedInAnyVehicle(PlayerPedId(), false) or IsPauseMenuActive() then return end
    local class = GetVehicleClass(GetVehiclePedIsUsing(PlayerPedId()))
    if class == 8 or class == 13 or class == 14 then return end
    toggleSeatbelt()
end, false)

RegisterKeyMapping('toggleseatbelt', 'Toggle Seatbelt', 'keyboard', 'B')

]]
RegisterCommand('toggleseatbelt', function()
    if not IsPedInAnyVehicle(PlayerPedId(), false) or IsPauseMenuActive() then return end
    local class = GetVehicleClass(GetVehiclePedIsUsing(PlayerPedId()))
    if class == 8 or class == 13 or class == 14 then return end
    local vehicle = GetVehiclePedIsIn(PlayerPedId(), false)
    local plate = QBCore.Functions.GetPlate(vehicle)

    TriggerServerEvent('brazzers-harness:server:toggleBelt', plate)
end, false)

RegisterKeyMapping('toggleseatbelt', 'Toggle Seatbelt', 'keyboard', 'B')
drhannibalseca commented 1 month ago

If I want it so I have to toggle the harness to get out of the car? What would I change?