Need help with toggleable vehicle freeze script

I’m trying to figure out how to make this script work

if Config.VehicleFreezeEnabled then
    RegisterCommand('+vfreeze', function() VFreeze() end, false)
    RegisterCommand('-vfreeze', function() StopVFreeze() end, false)
    RegisterKeyMapping("+vfreeze", "Toggleable Freeezing Vehicle", "keyboard", Config.VehicleFreezeKeybind)
	
	local stop = true
	function VFreeze()

		local player = GetPlayerPed(-1)
		local vehicle = GetVehiclePedIsIn(player, false)

		if Config.VehicleFreezeToggle then
			stop = not stop
		else
			stop = false
		end

		while not stop do
			FreezeEntityPosition(vehicle, false)
			Wait(0)
		end
	end

	function StopVFreeze()
		if Config.VehicleFreezeToggle then return end
		stop = true
	end
end

It works, but only if I set it to false or true

FreezeEntityPosition(vehicle, false)

The FreezeEntityPosition native does not need to be looped (and probably shouldn’t). I’m not 100% sure what you are trying to achieve with the code, but presuming that you want the vehicle to be frozen in place while a key is pressed then something like this would probably be what you want:

if Config.VehicleFreezeEnabled then
    RegisterCommand('+vfreeze', function() VFreeze(true) end, false)
    RegisterCommand('-vfreeze', function() VFreeze(false) end, false)
    RegisterKeyMapping("+vfreeze", "Toggleable Freeezing Vehicle", "keyboard", Config.VehicleFreezeKeybind)

    function VFreeze(freeze)
        local player = PlayerPedId()
        local vehicle = GetVehiclePedIsIn(player, false)

        FreezeEntityPosition(vehicle, freeze)
    end
end