Make a flashlight turn on when in an area

Hi! I want to make a flashlight system that only turns on when underground. But I cannot figure out how to insert a light into the player, then turn it on and off when they enter and exit an area. Can someone help me?

local plr = game.Players.LocalPlayer

local UnderGround = workspace.SoundRegions.Underground

local soundManagement = {}

if UnderGround then

	local info = {}

	local region3 = Region3.new(UnderGround.Position-(UnderGround.Size/2),UnderGround.Position+(UnderGround.Size/2))
	UnderGround.Transparency = .5

	info.Region = region3
	info.Sound = script.SoundRegions:FindFirstChild(UnderGround.name).Sound

	table.insert(soundManagement,info)

end

game:GetService("RunService").RenderStepped:Connect(function()

	for _,soundInfo in pairs(soundManagement) do

		local region = soundInfo.Region
		local sound = soundInfo.Sound
		local parts = workspace:FindPartsInRegion3WithWhiteList(region,plr.Character:GetDescendants())

		if #parts > 0 then

			if not sound.IsPlaying then

				sound:Play()

			end

		else

			sound:Stop()

		end

	end

end)

(I am trying to transform a area music script.)
I haven’t found anything similar to this on the developer hub.
image
This is my current explorer, I want to insert both Light and LightR into the player and make it follow wherever the player is looking.

do you want it to go to the cursor, the direction of the camera, or the direction of the character?

I would like it to point towards the cursor.

*Watch this tutorial :point_up_2:

Hope this helps fix your problem! :wrench: :slightly_smiling_face:

local plr = game.Players.LocalPlayer

local UnderGround = workspace.SoundRegions.Underground

local soundManagement = {}

local LightPart = script.LightPart

local Light = LightPart.Light

local LightR = LightPart.LightR

local Mouse = plr:GetMouse()

if UnderGround then

	local info = {}

	local region3 = Region3.new(UnderGround.Position-(UnderGround.Size/2),UnderGround.Position+(UnderGround.Size/2))

	info.Region = region3

	table.insert(soundManagement,info)

end

plr.CharacterAdded:Wait()

game:GetService("RunService").RenderStepped:Connect(function()
	
	LightPart.CFrame = CFrame.new(plr.Character.HumanoidRootPart.Position, Vector3.new(Mouse.Hit.Position.X, Mouse.Hit.Position.Y, Mouse.Hit.Position.Z))
	
	for _,soundInfo in pairs(soundManagement) do

		local region = soundInfo.Region
		local parts = workspace:FindPartsInRegion3WithWhiteList(region,plr.Character:GetDescendants())

		if #parts > 0 then

			LightPart.Parent = workspace

			else

				LightPart.Parent = script

			end

		end

	end)

Have this be in a local script in starterplayerscripts, and parent a block called “LightPart” to the script, parent the lights to the LightPart. I didn’t check if this works with other types of lights than spotlights, so I would recommend using those.

Edit: Make sure to have CanQuery off for the “Underground” part, else you can cast the light on the side of part.

1 Like