Help with hiding drawn text when not in a vehicle

I’m new to scripting and wanted to make a simple change to:

I wan’t to make it so time only displays when a player is inside a vehicle. I tried the the following:

--local displayTime = true
local useMilitaryTime = true
local displayDayOfWeek = false
local displayDate = false

local timeAndDateString = nil
local hour
local minute
local dayOfWeek
local month
local dayOfMonth
local year

-- Display Time and Date at top right of screen -- format: | 12:13 | Wednesday | January 17, 2017 |
Citizen.CreateThread(function()
	while true do
		Wait(1)
		timeAndDateString = ""
		
		if displayTime == true then
			CalculateTimeToDisplay()
			timeAndDateString = timeAndDateString .. " " .. hour .. ":" .. minute .. " "
		end
		if displayDayOfWeek == true then
			CalculateDayOfWeekToDisplay()
			timeAndDateString = timeAndDateString .. " " .. dayOfWeek .. " |"
		end
		if displayDate == true then
			CalculateDateToDisplay()
			timeAndDateString = timeAndDateString .. " " .. month .. " " .. dayOfMonth .. ", " .. year .. " |"
		end
		
		SetTextFont(4)
		SetTextProportional(1)
		SetTextScale(0.4, 0.4)
		SetTextColour(255, 255, 255, 255)
		SetTextDropshadow(0, 0, 0, 0, 255)
		SetTextEdge(1, 0, 0, 0, 255)
		SetTextDropShadow()
		SetTextOutline()
		SetTextRightJustify(false)
		SetTextWrap(0,0.95)
		SetTextEntry("STRING")
		
		AddTextComponentString(timeAndDateString)	
		DrawText(0.168, 0.893)
	end
end)
	
	
if IsPedInAnyVehicle(PlayerPed, true) then
	displayTime = true
	else
	displayTime = false
	end

Any help would be appreciated!

Try this instead:

ped = GetPlayerPed(source)
if IsPedInAnyVehicle(ped) then
-- your code goes here.

Edit:

And make a thread on top of it:

Citizen.CreateThread(function()
         while true do
                 Citizen.Wait(0)
                       ped = GetPlayerPed(source)
                       if IsPedInAnyVehicle(ped) then
                              -- your code goes here.
                       end
          end
end)

source doesn’t even exist there. Just use PlayerPedId().

Worked great! Thanks for the reply!

1 Like