Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· openaiHugging Face Suzhen/CodeChat

what can i do to lower the resmon on this fivem client script lua -- This script allows…

  1. User

    what can i do to lower the resmon on this fivem client script lua

    -- This script allows the user to throw an item to create an oil slick this will make cars that hit it slide local AnimDict = "core"; local AnimName = "ent_anim_paparazzi_flash" local WeaponModel = "w_ex_oiltub" local Animations = {"anim@heists@ornate_bank@thermal_charge", "cover_eyes_intro"} local OilSlickEquiped = false local ped = GetPlayerPed(-1) local slipping = false

    --table for oil slicks local_oil_slicks = {}

    --local table of coords to handle local coords_to_handle = {}

    --function for adding slip to vehicle function AddSlip() --create a thread Citizen.CreateThread(function() --get vehicle ped is in local vehicle = GetVehiclePedIsIn(ped, false) local old_val = GetVehicleHandlingFloat(vehicle, "CHandlingData", "fTractionCurveLateral") --set new traction value print('car should be slipping') SetVehicleHandlingFloat(vehicle, "CHandlingData", "fTractionCurveLateral", old_val * 6) --wait 7 seconds Wait(7000) --set traction back to old value print('car should no longer be slipping') SetVehicleHandlingFloat(vehicle, "CHandlingData", "fTractionCurveLateral", old_val) --set slipping to false slipping = false end) end

    --event to check local table and remove slick at location RegisterNetEvent('oil:delete_petrol_decal') AddEventHandler('oil:delete_petrol_decal', function(coords) for k,v in pairs(coords_to_handle) do if v == coords then RemoveDecal(k) --check local oil_slicks table and remove slick at location for k,v in pairs(local_oil_slicks) do if v == coords then local_oil_slicks[k]=nil end end end end end)

    Citizen.CreateThread(function() while true do --if player in vehicle if IsPedInAnyVehicle(ped,false) and not slipping then for k,v in pairs(local_oil_slicks) do --get distance to slick local dist = #(GetEntityCoords(ped) - v) if dist < 4 then --tell server to tell clients to delete TriggerServerEvent('oil:delete_oil_slick', v) if slipping == false then slipping = true AddSlip() end end end end Wait(200) end end)

    --function to create oil slick function CreateOilSlick(handle) -- thread do check for collision with ground Citizen.CreateThread(function() while true do --get coords of entity local coords = GetEntityCoords(handle) --draw line to it -- DrawLine(coords.x, coords.y, coords.z, coords.x, coords.y, coords.z - 10.0, 255, 0, 0, 255) local h = GetEntityHeightAboveGround(handle) --if collided with ground if h <= 0.3 then local coords = GetEntityCoords(handle) TriggerServerEvent('oil:create_oil_slick', coords) break end Wait(0) end end) end

    Citizen.CreateThread(function() while true do ped = GetPlayerPed(-1) if OilSlickEquiped == false then if GetSelectedPedWeapon(ped) == 277905663 then OilSlickEquiped = true; end else if IsPedShooting(ped) then OilSlickEquiped = false; local pos = GetEntityCoords(ped) Wait(100) local handle = GetClosestObjectOfType(pos.x,pos.y,pos.z,50.0,GetHashKey(WeaponModel),false,false,false) if handle ~= 0 then CreateOilSlick(handle) end end end Wait(0) end end)

    --function to create the newly added slick and update local table RegisterNetEvent('oil:create_petrol_decal') AddEventHandler('oil:create_petrol_decal', function(coords, oil_slicks) --get the ground z coord local ret_val, ground_z, normal = GetGroundZFor_3dCoord(coords.x, coords.y, coords.z) local new_slick = AddPetrolDecal(coords.x, coords.y, ground_z, 3.0, 6.0, 0.6) coords_to_handle[new_slick] = coords local_oil_slicks = oil_slicks end)

  2. Assistant

    To lower the resource monitor (resmon) on this FiveM client script, you can optimize the script by reducing the frequency of loops and unnecessary calculations. Here are some suggestions for optimization:

    1. Increase the wait time in the main thread loop to reduce its frequency.
    Citizen.CreateThread(function()
      while true do
          --if player in vehicle
          if IsPedInAnyVehicle(ped,false)  and not slipping then
            for k,v in pairs(local_oil_slicks) do
              --get distance to slick
              local dist = #(GetEntityCoords(ped) - v)
              if dist < 4 then
                --tell server to tell clients to delete
                TriggerServerEvent('oil:delete_oil_slick', v)
                if slipping == false then
                  slipping = true
                  AddSlip()
                end
              end
          end
        end
        Wait(500) -- Increase the wait time from 200 to 500
      end
    end)
    
    1. Cache the position of the player in the thread that checks if the player is shooting.
    Citizen.CreateThread(function()
      local playerPos
      while true do
        ped = GetPlayerPed(-1)
        playerPos = GetEntityCoords(ped)
        if OilSlickEquiped == false then
          if GetSelectedPedWeapon(ped) == 277905663 then 
            OilSlickEquiped = true;
          end
        else
          if IsPedShooting(ped) then
            OilSlickEquiped = false;
            Wait(100)
            local handle = GetClosestObjectOfType(playerPos.x,playerPos.y,playerPos.z,50.0,GetHashKey(WeaponModel),false,false,false)
            if handle ~= 0 then 
              CreateOilSlick(handle)
            end
          end
        end
        Wait(0)
      end
    end)
    
    1. Remove unnecessary print statements, as they can cause performance issues.
    --function for adding slip to vehicle
    function AddSlip()
      --create a thread
      Citizen.CreateThread(function()
        --get vehicle ped is in
        local vehicle = GetVehiclePedIsIn(ped, false)
        local old_val = GetVehicleHandlingFloat(vehicle, "CHandlingData", "fTractionCurveLateral")
        --set new traction value
        SetVehicleHandlingFloat(vehicle, "CHandlingData", "fTractionCurveLateral", old_val * 6)
        --wait 7 seconds
        Wait(7000)
        --set traction back to old value
        SetVehicleHandlingFloat(vehicle, "CHandlingData", "fTractionCurveLateral", old_val)
        --set slipping to false
        slipping = false
      end)
    end
    

    These optimizations should help reduce the resource usage of the script. Always test the changes in a test environment to ensure they have the desired effect on performance.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.