Spotlight Toggle

help
script

#1

Hey guys been trying to use spotlights as headlights so i can use blackout too i tried to put it on a button press like the headlights “h” but wheni press H to turn on the spot lights they just flash white (the 255,255,255 one) i have tried a few things that just made it stop and had someone else look and had the same result i am new at this so probably not the best but could anyone help keep the light on but want it as a toggle so can be switched off

client.lua

Citizen.CreateThread(function()
  while true do
   Citizen.Wait(1)
     vehicle = GetVehiclePedIsUsing(GetPlayerPed(-1))
       anycarX, anycarY, anycarZ = table.unpack(GetEntityCoords(vehicle, true))
         dirX, dirY, dirZ = table.unpack(GetEntityForwardVector(vehicle))
           if IsPedSittingInAnyVehicle(GetPlayerPed(-1)) then
              if IsControlJustPressed(1, 74) then
                 if DrawSpotLight(anycarX, anycarY, anycarZ, dirX, dirY, dirZ, 255, 255, 255, 125.0, 1.0, 1.0, 20.0, 0.95) then
                      DrawSpotLight(anycarX, anycarY, anycarZ, dirX, dirY, dirZ, 0, 0, 0, 125.0, 0, 1.0, 0, 0.95)
			    end
            end
        end
    end
end)`

#2

Could you put the code with formatting please? Click this:

</>


#3
Citizen.CreateThread(function()
	local bool = false
	while true do
		Citizen.Wait(0)
		
		local anycarX, anycarY, anycarZ = table.unpack(GetEntityCoords(GetVehiclePedIsUsing(GetPlayerPed(-1)), true))
		local dirX, dirY, dirZ = table.unpack(GetEntityForwardVector(GetVehiclePedIsUsing(GetPlayerPed(-1))))
	
		if IsPedSittingInAnyVehicle(GetPlayerPed(-1)) then
			if IsControlJustPressed(1, 74) then
				bool = not bool
			end
			if bool then
				DrawSpotLight(anycarX, anycarY, anycarZ, dirX, dirY, dirZ, 255, 255, 255, 125.0, 1.0, 1.0, 20.0, 0.95)
			end
		else
			bool = false
		end
	end
end)

#4

thx for the reply i will try soon as i can

tested it works great thanks for that flatracer and obviously anyone can use it


#5

@DieselDave

I got bored and improved it. You can use it if you want to

Citizen.CreateThread(function()
	local bool = false
	while true do
		Citizen.Wait(0)
		
		local LeftHeadLight = GetWorldPositionOfEntityBone(GetVehiclePedIsIn(GetPlayerPed(-1), false), GetEntityBoneIndexByName(GetVehiclePedIsIn(GetPlayerPed(-1), false), 'headlight_l'))
		local LeftHeadLight = GetOffsetFromEntityGivenWorldCoords(GetVehiclePedIsIn(GetPlayerPed(-1), false), LeftHeadLight.x, LeftHeadLight.y, LeftHeadLight.z)
		local LeftHeadLight = GetOffsetFromEntityInWorldCoords(GetVehiclePedIsIn(GetPlayerPed(-1), false), LeftHeadLight.x, LeftHeadLight.y - 1.5, LeftHeadLight.z)
		
		local RightHeadLight = GetWorldPositionOfEntityBone(GetVehiclePedIsIn(GetPlayerPed(-1), false), GetEntityBoneIndexByName(GetVehiclePedIsIn(GetPlayerPed(-1), false), 'headlight_r'))
		local RightHeadLight = GetOffsetFromEntityGivenWorldCoords(GetVehiclePedIsIn(GetPlayerPed(-1), false), RightHeadLight.x, RightHeadLight.y, RightHeadLight.z)
		local RightHeadLight = GetOffsetFromEntityInWorldCoords(GetVehiclePedIsIn(GetPlayerPed(-1), false), RightHeadLight.x, RightHeadLight.y - 1.5, RightHeadLight.z)

		
		local LeftTailLight = GetWorldPositionOfEntityBone(GetVehiclePedIsIn(GetPlayerPed(-1), false), GetEntityBoneIndexByName(GetVehiclePedIsIn(GetPlayerPed(-1), false), 'taillight_l'))
		local LeftTailLight = GetOffsetFromEntityGivenWorldCoords(GetVehiclePedIsIn(GetPlayerPed(-1), false), LeftTailLight.x, LeftTailLight.y, LeftTailLight.z)
		local LeftTailLight = GetOffsetFromEntityInWorldCoords(GetVehiclePedIsIn(GetPlayerPed(-1), false), LeftTailLight.x, LeftTailLight.y - 0.25, LeftTailLight.z)
		
		local RightTailLight = GetWorldPositionOfEntityBone(GetVehiclePedIsIn(GetPlayerPed(-1), false), GetEntityBoneIndexByName(GetVehiclePedIsIn(GetPlayerPed(-1), false), 'taillight_r'))
		local RightTailLight = GetOffsetFromEntityGivenWorldCoords(GetVehiclePedIsIn(GetPlayerPed(-1), false), RightTailLight.x, RightTailLight.y, RightTailLight.z)
		local RightTailLight = GetOffsetFromEntityInWorldCoords(GetVehiclePedIsIn(GetPlayerPed(-1), false), RightTailLight.x, RightTailLight.y - 0.25, RightTailLight.z)

		
		local LeftBrakeLight = GetWorldPositionOfEntityBone(GetVehiclePedIsIn(GetPlayerPed(-1), false), GetEntityBoneIndexByName(GetVehiclePedIsIn(GetPlayerPed(-1), false), 'brakelight_l'))
		
		local RightBrakeLight = GetWorldPositionOfEntityBone(GetVehiclePedIsIn(GetPlayerPed(-1), false), GetEntityBoneIndexByName(GetVehiclePedIsIn(GetPlayerPed(-1), false), 'brakelight_r'))
		
		local MiddleBrakeLight = GetWorldPositionOfEntityBone(GetVehiclePedIsIn(GetPlayerPed(-1), false), GetEntityBoneIndexByName(GetVehiclePedIsIn(GetPlayerPed(-1), false), 'brakelight_m'))

		
		local LeftReversingLight = GetWorldPositionOfEntityBone(GetVehiclePedIsIn(GetPlayerPed(-1), false), GetEntityBoneIndexByName(GetVehiclePedIsIn(GetPlayerPed(-1), false), 'reversinglight_l'))
		
		local RightReversingLight = GetWorldPositionOfEntityBone(GetVehiclePedIsIn(GetPlayerPed(-1), false), GetEntityBoneIndexByName(GetVehiclePedIsIn(GetPlayerPed(-1), false), 'reversinglight_r'))

		local ForwardVector = GetEntityForwardVector(GetVehiclePedIsIn(GetPlayerPed(-1), false))
		
		if IsPedSittingInAnyVehicle(GetPlayerPed(-1)) then
			if IsControlJustPressed(1, 74) then
				bool = not bool
			end
			if bool then
				DrawSpotLight(LeftHeadLight.x, LeftHeadLight.y, LeftHeadLight.z, ForwardVector.x, ForwardVector.y, ForwardVector.z, 255, 255, 255, 50.0, 1.0, 1.0, 20.0, 5.0)
				DrawSpotLight(RightHeadLight.x, RightHeadLight.y, RightHeadLight.z, ForwardVector.x, ForwardVector.y, ForwardVector.z, 255, 255, 255, 50.0, 1.0, 1.0, 20.0, 5.0)
				DrawSpotLight(LeftTailLight.x, LeftTailLight.y, LeftTailLight.z, ForwardVector.x * -1, ForwardVector.y * -1, ForwardVector.z, 255, 0, 0, 4.0, 0.4, 0.0, 80.0, 5.0)
				DrawSpotLight(RightTailLight.x, RightTailLight.y, RightTailLight.z, ForwardVector.x * -1, ForwardVector.y * -1, ForwardVector.z, 255, 0, 0, 4.0, 0.4, 0.0, 80.0, 5.0)
			end
		else
			bool = false
		end
	end
end)

#6

nice can already tell its gonna be better


#7

Another minor improvement.

  • Added automatic xenon adaptation
  • Added distant - light
local On = false; ExtendedOn = false

Citizen.CreateThread(function()
	while true do
		Citizen.Wait(0)
		if IsPedSittingInAnyVehicle(PlayerPedId()) then
		
			local LeftHeadLight = GetWorldPositionOfEntityBone(GetVehiclePedIsIn(PlayerPedId(), false), GetEntityBoneIndexByName(GetVehiclePedIsIn(PlayerPedId(), false), 'headlight_l'))
			local RightHeadLight = GetWorldPositionOfEntityBone(GetVehiclePedIsIn(PlayerPedId(), false), GetEntityBoneIndexByName(GetVehiclePedIsIn(PlayerPedId(), false), 'headlight_r'))

			local LeftTailLight = GetWorldPositionOfEntityBone(GetVehiclePedIsIn(PlayerPedId(), false), GetEntityBoneIndexByName(GetVehiclePedIsIn(PlayerPedId(), false), 'taillight_l'))
			local RightTailLight = GetWorldPositionOfEntityBone(GetVehiclePedIsIn(PlayerPedId(), false), GetEntityBoneIndexByName(GetVehiclePedIsIn(PlayerPedId(), false), 'taillight_r'))
			
			if not IsModelBlacklisted(GetEntityModel(GetVehiclePedIsIn(PlayerPedId(), false))) then
				LeftHeadLight = GetOffsetFromEntityGivenWorldCoords(GetVehiclePedIsIn(PlayerPedId(), false), LeftHeadLight.x, LeftHeadLight.y, LeftHeadLight.z)
				LeftHeadLight = GetOffsetFromEntityInWorldCoords(GetVehiclePedIsIn(PlayerPedId(), false), LeftHeadLight.x, LeftHeadLight.y - 0.75, LeftHeadLight.z)

				RightHeadLight = GetOffsetFromEntityGivenWorldCoords(GetVehiclePedIsIn(PlayerPedId(), false), RightHeadLight.x, RightHeadLight.y, RightHeadLight.z)
				RightHeadLight = GetOffsetFromEntityInWorldCoords(GetVehiclePedIsIn(PlayerPedId(), false), RightHeadLight.x, RightHeadLight.y - 0.75, RightHeadLight.z)

				LeftTailLight = GetOffsetFromEntityGivenWorldCoords(GetVehiclePedIsIn(PlayerPedId(), false), LeftTailLight.x, LeftTailLight.y, LeftTailLight.z)
				LeftTailLight = GetOffsetFromEntityInWorldCoords(GetVehiclePedIsIn(PlayerPedId(), false), LeftTailLight.x, LeftTailLight.y - 0.25, LeftTailLight.z)

				RightTailLight = GetOffsetFromEntityGivenWorldCoords(GetVehiclePedIsIn(PlayerPedId(), false), RightTailLight.x, RightTailLight.y, RightTailLight.z)
				RightTailLight = GetOffsetFromEntityInWorldCoords(GetVehiclePedIsIn(PlayerPedId(), false), RightTailLight.x, RightTailLight.y - 0.25, RightTailLight.z)
			end
			
			local LeftBrakeLight = GetWorldPositionOfEntityBone(GetVehiclePedIsIn(PlayerPedId(), false), GetEntityBoneIndexByName(GetVehiclePedIsIn(PlayerPedId(), false), 'brakelight_l'))
			local RightBrakeLight = GetWorldPositionOfEntityBone(GetVehiclePedIsIn(PlayerPedId(), false), GetEntityBoneIndexByName(GetVehiclePedIsIn(PlayerPedId(), false), 'brakelight_r'))
			local MiddleBrakeLight = GetWorldPositionOfEntityBone(GetVehiclePedIsIn(PlayerPedId(), false), GetEntityBoneIndexByName(GetVehiclePedIsIn(PlayerPedId(), false), 'brakelight_m'))

			local LeftReversingLight = GetWorldPositionOfEntityBone(GetVehiclePedIsIn(PlayerPedId(), false), GetEntityBoneIndexByName(GetVehiclePedIsIn(PlayerPedId(), false), 'reversinglight_l'))
			local RightReversingLight = GetWorldPositionOfEntityBone(GetVehiclePedIsIn(PlayerPedId(), false), GetEntityBoneIndexByName(GetVehiclePedIsIn(PlayerPedId(), false), 'reversinglight_r'))
			local ForwardVector = GetEntityForwardVector(GetVehiclePedIsIn(PlayerPedId(), false))
			
			if GetIsVehicleEngineRunning(GetVehiclePedIsIn(PlayerPedId(), false)) and not (GetVehicleClassFromName(GetEntityModel(GetVehiclePedIsIn(PlayerPedId(), false))) == 13) then
				if IsControlJustPressed(1, 74) then
					TriggerLight()
				end
				if On then
					local R, G, B, Fade
					if IsToggleModOn(GetVehiclePedIsIn(PlayerPedId(), false), 22) then
						R, G, B, Brightness, Fade = 20, 57, 127, 15.0, 10.0
					else
						R, G, B, Brightness, Fade = 255, 255, 255, 7.5, 15.0
					end
					if ExtendedOn then
						DrawSpotLight(LeftHeadLight.x, LeftHeadLight.y, LeftHeadLight.z, ForwardVector.x, ForwardVector.y, ForwardVector.z, R, G, B, 80.0, Brightness, 0.0, 35.0, Fade)
						DrawSpotLight(RightHeadLight.x, RightHeadLight.y, RightHeadLight.z, ForwardVector.x, ForwardVector.y, ForwardVector.z, R, G, B, 80.0, Brightness, 0.0, 35.0, Fade)
					else
						DrawSpotLight(LeftHeadLight.x, LeftHeadLight.y, LeftHeadLight.z, ForwardVector.x, ForwardVector.y, ForwardVector.z, R, G, B, 40.0, Brightness, 0.0, 30.0, Fade)
						DrawSpotLight(RightHeadLight.x, RightHeadLight.y, RightHeadLight.z, ForwardVector.x, ForwardVector.y, ForwardVector.z, R, G, B, 40.0, Brightness, 0.0, 30.0, Fade)
					end
					DrawSpotLight(LeftTailLight.x, LeftTailLight.y, LeftTailLight.z, ForwardVector.x * -1, ForwardVector.y * -1, ForwardVector.z, 255, 0, 0, 4.0, 0.4, 0.0, 80.0, 5.0)
					DrawSpotLight(RightTailLight.x, RightTailLight.y, RightTailLight.z, ForwardVector.x * -1, ForwardVector.y * -1, ForwardVector.z, 255, 0, 0, 4.0, 0.4, 0.0, 80.0, 5.0)
				end
			end
		else
			On = false
			ExtendedOn = false
		end
	end
end)

function TriggerLight()
	if not On then
		On = true
	else
		if not ExtendedOn then
			ExtendedOn = true
		else
			ExtendedOn = false
			On = false
		end
	end
end

function IsModelBlacklisted(Model)
	local BlacklistedModels = {'DUMP', 'RHINO', 'BLIMP', 'BLIMP2'}

	for k, BlacklistedModel in ipairs(BlacklistedModels) do
		if Model == GetHashKey(BlacklistedModel) then
			return true
		end
	end
	
	return false
end

#8

Damn that’s nice! Mind if I use it for a police system I am working on?


#9

Take it for whatever you want, just credit me if you use it.


#10

Yes sir! :smiley: Thanks.