[help] Problem with toggle command (fixed)

so i have been trying to give this Script a toggle option but i can’t figure it out. i tried to do it with a command that changes a variable,

and then use that variable in the beginning of the actual script

but it does not work what so ever. i would really appreciate if someone could help me out.

Just a quick read first thing that sticks out in your toggle… you never change the state

if state == true then
overwriteAlpha = false
elseif state == false then
overwriteAlpha = true
else
overwriteAlpha = not overwriteAlpha
end

if you have state = true when the script starts… toggle will always try and set overwriteAlpha = false and nothing else. For it to toggle you need to change the overwriteAlpha and the state as well as in something along these lines:

if state then
overwriteAlpha = false
state = false
elseif not state then
overwriteAlpha = true
state = true
else
overwriteAlpha = not overwriteAlpha (not sure about this line at all)
end

I am really new to Lua… but this is how i see it should work.

hopefully this helps

SpikE

Thanks for your suggestion i tried this and it did not work. I took the whole toggle form SexySpeedometer it works very well there. I personally don’t have much experience with LUA i did not write the script by myself i just added the toggle and tried to implement it in the same way it was in SexySpeedometer.

Give me a min to type this out in Visual Studio so i can get it spaced a bit… simple enough to miss a end and have the script not work but still load the server… will post here when done getting the code written.

SpikE

you never pass a argument for the function call

Taking all post that used screen shots and putting in code block.

local shot = false
local check = false
local check2 = false
local count = 0
local state = true -- what ever default state you want to put it in.

RegisterCommand('togglefps', function()
    togglefps()
end)


function togglefps(state)
    if state then 
        overwriteAlpha = false
        state = false
    else
        overwriteAlpha = true
        state = true
    end
end


overwriteChecks = false
curAlpha = 0
Citizen.CreateThread(function()
    while true do
        -- Wait 5 seconds after player has loasded in and trigger the event.
        SetBlackout(false)
        Citizen.Wait(0)
        if overwriteAlpha then curAlpha = 0 end
        
        if not overwriteAlpha then
            SetBlackout(false)
            if IsPedShooting(GetPlayerPed(-1)) and shot == false and GetFollowPedCAmViewMode() ~= 4 then
                check2 = true
                shot = true
                SetFollowPedCamViewMode(4)
            end

            if IsPedShooting(GetPlayerPed(-1)) and shot == true then GetFollowPedCamViewMode() == 4 then
                count = 0
            end
            
            if not IsPedShoooting(GetPlayerPed(-1)) and shot == true then
                count = count + 1
            end
            
            if not IsPedShooting(GetPlayerPed(-1)) and shot == true then
                if not IsPedShooting(GetPlayerPed(-1)) and shot == true and count > 20 then
                    if check2 == true then
                        check2 = false
                        shot = false
                        SetFollowPedCamViewMode(1)
                    end
                end
            end
        end
    end
end)

Your Register Command was asking for more then it needed.

Your toggle wasn’t setting the state to a new state to actually toggle

Your base code was missing a end for Line 32.

Without seeing the entire script i would assume what i posted will work… but if nothing else should give you an idea of what to look for.

1 Like

If you’re going to show code, post it here in a code block, don’t bother showing it in a screenshot, that’s not useful at all.

I agree and thnx for pointing it out. I have not posted code on this site as of yet and was trying to do it with the code block and wasn’t getting it spaced out. The reason for the image was to show him the end he was missing… but the code above should hopefully it sorted out and give them a example of what to look for for next time.

2 Likes

Thank you so much for your help. I tested your code and for some reason it still doesn’t work for me. that is my entire code. ( sorry can’t figure out why it is not code block like yours)

local shot = false
local check = false
local check2 = false
local count = 0
local state = true

RegisterCommand("togglefps",function()
	togglefps()
end,false)

function togglefps(state)
	if state then
		overwriteAlpha = false
		state = false
	else
		overwriteAlpha = true
		state = true
	end
end

Citizen.CreateThread(function()
	while true do
		SetBlackout(false)
		Citizen.Wait( 1 )
		-- check if player is already in first person
		if IsPlayerFreeAiming(PlayerId()) then
		    if GetFollowPedCamViewMode() == 4 and check == false then
			    check = false
			else
			    SetFollowPedCamViewMode(4)
			    check = true
			end
		else
		    if check == true then
		        SetFollowPedCamViewMode(1)
				check = false
			end
		end
	end
end )


overwriteChecks = false
curAlpha = 0
Citizen.CreateThread(function()
    while true do
        -- Wait 5 seconds after player has loasded in and trigger the event.
        SetBlackout(false)
        Citizen.Wait(0)
        if overwriteAlpha then curAlpha = 0 end
        
        if not overwriteAlpha then
            SetBlackout(false)
            if IsPedShooting(GetPlayerPed(-1)) and shot == false and GetFollowPedCAmViewMode() ~= 4 then
                check2 = true
                shot = true
                SetFollowPedCamViewMode(4)
            end

            if IsPedShooting(GetPlayerPed(-1)) and shot == true then GetFollowPedCamViewMode() == 4 then
                count = 0
            end
            
            if not IsPedShoooting(GetPlayerPed(-1)) and shot == true then
                count = count + 1
            end
            
            if not IsPedShooting(GetPlayerPed(-1)) and shot == true then
                if not IsPedShooting(GetPlayerPed(-1)) and shot == true and count > 20 then
                    if check2 == true then
                        check2 = false
                        shot = false
                        SetFollowPedCamViewMode(1)
                    end
                end
            end
        end
    end
end)

You need to pass an argument in togglefps(). In your command, you are not passing it where it requires it. Also making a toggle can be much easier. You could replace your whole function with:

function togglefps()
	overwriteAlpha = not overwriteAlpha
end

Or even remove the function and just have the overwriteAlpha = not overwriteAlpha in the command. Both of those versions I just suggested would also allow you to remove the state variable.

Thank you so what argument do i need to pass. As i said earlier i have no idea what i am doing im still figuring this all out. Also your YouTube channel is great i ran into it the other day when trying to fix this problem

1 Like

You need to pass your toggle for state. If I were you I’d move to the 2nd solution as it should work straight off the bat. Even if you want add a print to further debug and have that satisfaction of it changing haha.

Thanks! Feel free to shoot me a PM if ya want to chat or want more info about that stuff as we shouldn’t really discuss it in a public thread. :heart:

1 Like

Ok maybe i was mistaken from the start… I was under the impression you just needed help with the toggle command… but did the reset of your script work before you tried putting in a toggle… and if so can you post that script for reference to see what you changed that is making it not work now.

Other thing that would be helpful is to post any F8 error’s that this script is giving you.

There most likely is no errors. The rest of his code should work fine.

@Exelsio I saw you liked the post. Did it help with the issue?

No the problem is that i still don’t really understand it worked before i started implementing a toggle. The problem is that this is my first time attempting any programming you guys would probably have it working by now but i spent a good 10 hours trying to figure it out.

that is the original code

local shot = false
local check = false
local check2 = false
local count = 0

Citizen.CreateThread(function()
	while true do
		SetBlackout(false)
		Citizen.Wait( 1 )
		-- check if player is already in first person
		if IsPlayerFreeAiming(PlayerId()) then
		    if GetFollowPedCamViewMode() == 4 and check == false then
			    check = false
			else
			    SetFollowPedCamViewMode(4)
			    check = true
			end
		else
		    if check == true then
		        SetFollowPedCamViewMode(1)
				check = false
			end
		end
	end
end )



Citizen.CreateThread(function()
	while true do
		-- Wait 5 seconds after player has loaded in and trigger the event.
		SetBlackout(false)
		Citizen.Wait( 1 )
		
		if IsPedShooting(GetPlayerPed(-1)) and shot == false and GetFollowPedCamViewMode() ~= 4 then
			check2 = true
			shot = true
			SetFollowPedCamViewMode(4)
		end
		
		if IsPedShooting(GetPlayerPed(-1)) and shot == true and GetFollowPedCamViewMode() == 4 then
			count = 0
		end
		
		if not IsPedShooting(GetPlayerPed(-1)) and shot == true then
		    count = count + 1
		end

        if not IsPedShooting(GetPlayerPed(-1)) and shot == true then
			if not IsPedShooting(GetPlayerPed(-1)) and shot == true and count > 20 then
		        if check2 == true then
				    check2 = false
					shot = false
					SetFollowPedCamViewMode(1)
				end
			end
		end	    
	end
end )