Is it possible to set the opacity of DrawnText to 0 when not in a vehicle and back to full when in a vehicle?

I decided to tweak: G.Bronson’s Seatbelt Script to look like the NoPixel seatbelt indicator. It originally uses a CSS file and some JavaScript to display the animation and image. I couldn’t find the font so I decided to edit the lua file to get the one I wanted. The second “belt” is the original version which I would disable.

Seatbelt indicator changing colour when seatbelt is on.

Seatbelt indicator disappearing when exiting vehicle.

The only problem I have is I can’t combine the two. Is it possible to set the opacity of the text to 0 when not in a vehicle and back to full when in a vehicle?

local isUiOpen = false 
local speedBuffer  = {}
local velBuffer    = {}
local beltOn       = false
local wasInCar     = false
local hide         = true
local on = false

IsCar = function(veh)
        local vc = GetVehicleClass(veh)
        return (vc >= 0 and vc <= 7) or (vc >= 9 and vc <= 12) or (vc >= 17 and vc <= 20)
        end 

Fwv = function (entity)
        local hr = GetEntityHeading(entity) + 90.0
        if hr < 0.0 then hr = 360.0 + hr end
        hr = hr * 0.0174533
        return { x = math.cos(hr) * 2.0, y = math.sin(hr) * 2.0 }
      end
 
 
Citizen.CreateThread(function()
  while true do
  Citizen.Wait(0)
  
    local ped = GetPlayerPed(-1)
    local car = GetVehiclePedIsIn(ped)
    
    if car ~= 0 and (wasInCar or IsCar(car)) then
      wasInCar = true
             if isUiOpen == false and not IsPlayerDead(PlayerId()) then
                SendNUIMessage({
            	   displayWindow = 'true'
            	   })
				hide = false
                isUiOpen = true 			
            end

      if beltOn then DisableControlAction(0, 75) end

      speedBuffer[2] = speedBuffer[1]
      speedBuffer[1] = GetEntitySpeed(car)
      
      if speedBuffer[2] ~= nil 
         and not beltOn
         and GetEntitySpeedVector(car, true).y > 1.0  
         and speedBuffer[1] > 19.25 
         and (speedBuffer[2] - speedBuffer[1]) > (speedBuffer[1] * 0.255) then
         
        local co = GetEntityCoords(ped)
        local fw = Fwv(ped)
        SetEntityCoords(ped, co.x + fw.x, co.y + fw.y, co.z - 0.47, true, true, true)
        SetEntityVelocity(ped, velBuffer[2].x, velBuffer[2].y, velBuffer[2].z)
        Citizen.Wait(1)
        SetPedToRagdoll(ped, 1000, 1000, 0, 0, 0, 0)
      end
        
      velBuffer[2] = velBuffer[1]
      velBuffer[1] = GetEntityVelocity(car)
        
      if IsControlJustReleased(0, 311) and GetLastInputMethod(0) then
        beltOn = not beltOn 
        if beltOn then 
		  TriggerEvent("pNotify:SendNotification", {text = "Seatbelt On", type = "success", timeout = 1400, layout = "centerLeft"})
			
			on = true;
			
		  SendNUIMessage({
		    displayWindow = 'false'
		    })
		  hide = true
		  isUiOpen = true 
		else 
		  TriggerEvent("pNotify:SendNotification", {text = "Seatbelt Off", type = "error", timeout = 1400, layout = "centerLeft"}) 

			on = false;
		  
		  SendNUIMessage({
		     displayWindow = 'true'
		     })
		  hide = false
		  isUiOpen = true  
		end
      end
      
    elseif wasInCar then
      wasInCar = false
      beltOn = false
      speedBuffer[1], speedBuffer[2] = 0.0, 0.0
             if isUiOpen == true and not IsPlayerDead(PlayerId()) then
                SendNUIMessage({
            	   displayWindow = 'false'
            	   })
				hide = true
                isUiOpen = false 
            end
    end
    
  end
end)


Citizen.CreateThread(function()
    while true do
        Citizen.Wait(100)
        if IsPlayerDead(PlayerId()) and isUiOpen == true then
            SendNUIMessage({
                    displayWindow = 'false',
               })
			hide = true
            isUiOpen = false
        end    

    end
end)   

Citizen.CreateThread(function()
    while true do
        Citizen.Wait(0)
		ped = GetPlayerPed(source)
		if on == true and IsPedInAnyVehicle(ped) then
			SetTextFont(4)
        SetTextProportional(1)
        SetTextScale(0.0, 0.38)
        SetTextColour(0, 255, 0, 0.6)
        SetTextDropshadow(0, 0, 0, 0, 255)
        SetTextEdge(1, 0, 0, 0, 255)
        SetTextDropShadow()
        SetTextOutline()
        SetTextEntry("STRING")
		
		lastStreetName = "BELT"
		
		AddTextComponentString(lastStreetName)
            DrawText(0.26, 0.929)
			else
			SetTextFont(4)
        SetTextProportional(1)
        SetTextScale(0.0, 0.38)
        SetTextColour(255, 0, 0, 255)
        SetTextDropshadow(0, 0, 0, 0, 255)
        SetTextEdge(1, 0, 0, 0, 255)
        SetTextDropShadow()
        SetTextOutline()
        SetTextEntry("STRING")
		
		lastStreetName = "BELT"
		
		AddTextComponentString(lastStreetName)
            DrawText(0.26, 0.929)
			end
		end
end)


--[[Citizen.CreateThread(function()
    while true do
        Citizen.Wait(0)
		if hide == true then
		SetTextFont(4)
        SetTextProportional(1)
        SetTextScale(0.0, 0.38)
        SetTextColour(0, 255, 0, 0)
        SetTextDropshadow(0, 0, 0, 0, 255)
        SetTextEdge(1, 0, 0, 0, 255)
        SetTextDropShadow()
        SetTextOutline()
        SetTextEntry("STRING")
		
		lastStreetName = "BELT"
		
		AddTextComponentString(lastStreetName)
            DrawText(0.26, 0.929)
			else
			SetTextFont(4)
        SetTextProportional(1)
        SetTextScale(0.0, 0.38)
        SetTextColour(255, 0, 0, 255)
        SetTextDropshadow(0, 0, 0, 0, 255)
        SetTextEdge(1, 0, 0, 0, 255)
        SetTextDropShadow()
        SetTextOutline()
        SetTextEntry("STRING")
		
		lastStreetName = "BELT"
		
		AddTextComponentString(lastStreetName)
            DrawText(0.26, 0.929)
			end
		end
end)]]--

Any help would be greatly appreciated!

Are you talking about completely hiding the text?
I’d say just don’t draw it in the first place if the player is in any vehicle, simply remove this part of your code:

else
			SetTextFont(4)
        SetTextProportional(1)
        SetTextScale(0.0, 0.38)
        SetTextColour(255, 0, 0, 255)
        SetTextDropshadow(0, 0, 0, 0, 255)
        SetTextEdge(1, 0, 0, 0, 255)
        SetTextDropShadow()
        SetTextOutline()
        SetTextEntry("STRING")
		
		lastStreetName = "BELT"
		
		AddTextComponentString(lastStreetName)
            DrawText(0.26, 0.929)

I’m looking to draw the text when inside a vehicle and make it disappear when not. I just don’t know if it’s possible to use the if statement to change the colour of the text depending on if it’s engaged or not and make it hide when not in a vehicle.

I got it:

Citizen.CreateThread(function()
    while true do
        Citizen.Wait(0)
		ped = GetPlayerPed(source)
		if on == true and IsPedInAnyVehicle(ped) then
			SetTextFont(4)
        SetTextProportional(1)
        SetTextScale(0.0, 0.38)
        SetTextColour(0, 255, 0, 0.6)
        SetTextDropshadow(0, 0, 0, 0, 255)
        SetTextEdge(1, 0, 0, 0, 255)
        SetTextDropShadow()
        SetTextOutline()
        SetTextEntry("STRING")
		
		lastStreetName = "BELT"
		
		AddTextComponentString(lastStreetName)
            DrawText(0.26, 0.929)
			elseif on == false and IsPedInAnyVehicle(ped) then
			SetTextFont(4)
        SetTextProportional(1)
        SetTextScale(0.0, 0.38)
        SetTextColour(255,0,0, 0.6)
        SetTextDropshadow(0, 0, 0, 0, 255)
        SetTextEdge(1, 0, 0, 0, 255)
        SetTextDropShadow()
        SetTextOutline()
        SetTextEntry("STRING")
		
		lastStreetName = "BELT"
		
		AddTextComponentString(lastStreetName)
            DrawText(0.26, 0.929)
			
			end
		end
end)

Thanks for making me take another perspective. @Vespura

1 Like