Script works opposite as intended after quitting and joining server until conditions are met

So hopefully I can explain this clearly.

I started with this free resource:
LX - Forced Camera - FiveM Releases - Cfx.re Community
GitHub - deLiinux/lx-forcedcam: A script that changes camera when aiming a gun inside a vehicle.

And it has the ability to limit the players camera view to one of the three levels of third person view & first person view, and the ability to force first person when doing driveby’s in vehicles.

However, I would prefer this to be locked to the near and medium third person views.

I was able to do just that by modifying a couple values in the original client.lua. Now the script limits on foot Cameras to near and medium third person view, and car cameras to medium third person view only while retaining it’s ability to force players into first person view for drievbys, exactly what I wanted.

BUT… when I quit and rejoin the server, this works in the opposite way. It instead locks me into medium third person camera while on foot, and allows me to switch between near and medium while in a car… BUT… when i do get in a car, and then aim to make the secondary function of forcing first person for driveby’s, it then starts to work as intended just fine again.

I’m going to post the code I’m currently using that is producing these results. Anybody have any ideas what could be causing this strange behavior and how I could fix it so that it works when you quit and rejoin without having to get in a car and aim down sights?

local check = false
local camera = Config.Cam
local fps = 4

CreateThread(function()
    while true do
        if Config.ForcedFPSinVehicle then
            if not IsPedInAnyVehicle(PlayerPedId()) then Wait(600) end
            if IsPedDoingDriveby(PlayerPedId()) then
                if GetFollowVehicleCamViewMode() == fps and check == false then
                    check = false
                else
                    SetFollowVehicleCamViewMode(fps)
                    check = true
                end
            else
                if check == true then
                    SetFollowVehicleCamViewMode(camera)
                    check = false
                end
            end
            Wait(1)
        else
            Wait(60000)
        end
    end
end)

CreateThread(function()
    local setpov = false
    while true do
        if Config.Only2CamViews then
            DisableControlAction(0, 0, true)
            local VehicleCurrentCamViewMode = GetFollowVehicleCamViewMode(PlayerPedId())
            local context = GetCamActiveViewModeContext()
            if not setpov and IsDisabledControlJustPressed(0, 0) then
                setpov = true
                SetCamViewModeForContext(context, 0)
            elseif setpov and IsDisabledControlJustPressed(0, 0) then
                setpov = false
                SetCamViewModeForContext(context, camera)
            end
            Wait(1)
        else
            Wait(60000)
        end
    end
end)

Is this the only script that you’re using for your custom resource (other than your config script)?
Is there some sort of initiator? Or server sided script that you’re using?

Does the issue still happen if you manually switch the camera view right after spawning?

If manually switching the camera mode after spawning fixes the issue until you restart, It could be that the camera mode is not being correctly initialized at spawn.

This means you will most likely need to explicitly set the camera mode when player joins.

No, there is no server side to this script. Just the client and config files.

I believe the issue is related to how the second function is disabling controls right away not allowing it to function as intended to lock camera types to third person near and third person medium

But I’m not entirely sure how the first function is able to completely fix the issue and cause the second function to work properly after it registers a player as doing a drive-by.

I’ve been trying to also modify the second function to use IsControlPressed rather than disabling controls, but every attempt I make at this seems to just break the script or causes it to function oddly and not at all how I intended.

Forgot to post this in the initial thread but here is the full config file as well if it helps to understand how it’s functioning better:

config.lua:

Config = Config or {}

Config.ForcedFPSinVehicle = true -- If this is set to true, it will enable forcing players going in first person when shooting from vehicles. -- Runs at 0.02ms while in a vehicle and if enabled. Otherwise runs at 0.00ms

Config.Only2CamViews = true -- If this is set to true, it will enable that all players only have two camera views much like Nopixel. -- Runs at 0.01ms while enabled. Otherwise runs at 0.00ms

Config.Cam = 1 -- | 0 = Near | 1 = Medium | 2 = Far |

You can’t manually switch the camera after spawning and that’s the issue. the second thread is suppose to limit on foot camera options to third person near and third person medium based on these config options:


Config.Only2CamViews = true -- If this is set to true, it will enable that all players only have two camera views much like Nopixel. -- Runs at 0.01ms while enabled. Otherwise runs at 0.00ms

Config.Cam = 1 -- | 0 = Near | 1 = Medium | 2 = Far |

and also limit in-car camera options to third person medium only while forcing you into FPS camera when it detects you doing a drive-by with the first function.

But upon spawning into the server it locks the on-foot camera to third person medium, like its supposed to in a car. While also allowing both third person near and third person medium cameras while in a car, like its suppose to on-foot.

It isn’t until it does detect the player is performing a drive-by and forces them into the FPS camera one time that it will start functioning as it’s intended only allowing 2 cameras on foot and the one in car with the forced FPS for drive-by’s.

However, if you are on the server and restart the script, it continues to work as intended. It only seems to mess up when you quit the server and then rejoin it.

Do you need help with this still? I can look into it otherwise :slight_smile: