How to make a SpotLight follow the player's mouse

Hey dev’s! I’m trying to make a horror game. And i want a flashlight that follows the players mouse [That is not a tool]. I just placed an attachment inside the character and added a spotlight. Please someone help me.

[All of the replies are the solutions]

5 Likes

Do you want it to look and act like a tool, or do you want it to be possible to “point” the light anywhere, even 100 studs ahead of the player?

1 Like

like a real life flashlight, that’s what i want,

1 Like

also it’s supposed to be like a flashlight that goes on the head

1 Like

Then you have to make a part that has the SpotLight, and then rotate that part towards the players mouse. Read this question

1 Like

hmm i can try to make the flashlight move using this link, thanks.

1 Like

i cant make it work, it’s mostly an infinite yield or the game bugs out.

1 Like

Could you please show the script you tried and the output?

1 Like

local RS = game:GetService(“RunService”)
local player = game.Players.LocalPlayer
local Mouse = player:GetMouse()
local character = player.Character or player.CharacterAdded:Wait()

while wait() do
local x, y, z = Mouse.Hit:ToEulerAnglesXYZ()

local Flashlight = character:WaitForChild("HumanoidRootPart"):WaitForChild("FlashlightAttachment")
print(Mouse.Hit.y)

Flashlight.CFrame = CFrame.Angles(0, math.rad(Mouse.Hit.y), 0)

end

2 Likes

I want the flashlight to move up and down too

1 Like

I wouldn’t recommend rotating attachments, so try this instead:

local plr = game.Players.LocalPlayer;
local mouse = plr:GetMouse();

while wait() do
	LIGHT.CFrame = CFrame.lookAt(LIGHT.Position, mouse.Hit.Position); -- LIGHT is the headlight
end
1 Like

is the light the spotlight or the humanoid rootpart?

1 Like

The SpotLight. And if the attachment doesn’t work, you can use the same loop to position the spotlight above the players head

1 Like

Position is not a valid member of SpotLight

1 Like

You have to set the position of the PART that contains the SpotLight, a.k.a the part that lights

1 Like

so i need to change the attachment to a part? got it.

2 Likes

it works but the player keeps moving and rotating
i just want the part to rotate

1 Like

MouseHit has a property called “LookVector”.

Taken from the DevForum:

local mouseHitDirection = mouse.Hit.lookVector
That same documentation also has an example which is similar to what you're looking for.. of course you'd have to adapt it.

Mouse.Hit Laser Beam

The code in this sample, when placed inside a LocalScript within StarterPlayerScripts will draw a red laser beam between the character’s head and Mouse.Hit at all times.

Note, this beam will pass directly through obstructions in third person as the Mouse ’s raycasting is done from the Workspace.CurrentCamera not the head.

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
 
-- grab local player
local localPlayer = Players.LocalPlayer
 
-- create beam
local beam = Instance.new("Beam")
beam.Segments = 1
beam.Width0 = 0.2
beam.Width1 = 0.2
beam.Color = ColorSequence.new(Color3.new(1, 0, 0))
beam.FaceCamera = true
 
-- create attachments
local attachment0 = Instance.new("Attachment")
local attachment1 = Instance.new("Attachment")
beam.Attachment0 = attachment0
beam.Attachment1 = attachment1
 
-- parent attachments to Terrain 
beam.Parent = workspace.Terrain
attachment0.Parent = workspace.Terrain
attachment1.Parent = workspace.Terrain
 
-- grab the mouse
local mouse = localPlayer:GetMouse()
 
-- connect to RenderStepped (update every frame)
RunService.RenderStepped:Connect(function()
 
	-- make sure the character exists
	local character = localPlayer.Character
	if not character then
		-- disable the beam
		beam.Enabled = false
		return
	end
 
	-- make sure the head exists
	local head = character:FindFirstChild("Head")
	if not head then
		-- disable the beam
		beam.Enabled = false
		return
	end
 
	-- enable the beam
	beam.Enabled = true
 
	-- define origin and finish
	local origin = head.Position
	local finish = mouse.Hit.p
 
	-- move the attachments 
	attachment0.Position = origin
	attachment1.Position = finish
end)
2 Likes

i already made the flashlight work but the part that has the light moves the character and makes the character also look at the mouse.

1 Like

Is the SpotLight inside the RootPart? If it is, make a different part and move the PointLight to it, then it will work

1 Like