Attaching a light to the camera

Ive tried to make a thing connecting a light to the camera for a flashlight but its not working (i need it attached to the camera because i have the camera attached to parts)

this is the script i have that should attach it (i took it from a open source plugin that attaches a light to your camera):

local Camera = workspace.CurrentCamera
local part = script.Parent
local spotlight = part.SpotLight

game:GetService("RunService").Heartbeat:Connect(function()
	part.CFrame = Camera.CFrame
end)
1 Like

might be a shot in the dark but dont Capitalize the C in Camera have it lowercase like camera

local camera = game.workspace.CurrentCamera

That wouldn’t change anything at all. “Camera” isn’t already a keyword, so it can be used.

I’m not sure what you’re referencing to on line 4.
It could be that “spotlight.On” isn’t a child or property of “spotlight”.

Make sure the following are correct:

  1. Every variable exist in the game
  2. The script is a local script
  3. The script is located on the client
  4. The part is being parented to the workspace

name of variable doesnt really change anything

Just so you know, this can work for either Server and Client, It ultimately Depends on your usage of the following code

If you plan to have invisible to all Players while maintaining visibility for your Local Client, you would use a LocalScript for this which can easily be accessed with the following:
StarterPlayerScripts, StarterCharacterScripts, ReplicatedStorage, ReplicatedFirst, StarterGui
And in some cases, you are able to put a LocalScript within the workspace and have it fully working by using a Regular Script and setting its RunContext to Client, which has the Script run as a LocalScript, but unlike regular LocalScripts, it can basically run within the workspace

You can try doing it with StarterCharacterScripts and use either Stepped (PreSimulation) or Heartbeat (PostSimulation) for this as RenderStepped (PreRender) only works on the Client.
However, If you want to Simplify the use, You can easily just Attach a Light to the Player, more specifically, on their Head which is way faster and more resourceful.

Instances can only be Parented to the Camera, but not Attached to it, you would need to use CFrame to have this effect.

i think you may be looking for Enabled, unless you have a Child named On

Sending Data from Client to Server can be done with RemoteEvents, However this may not be ideal.

On is a value thing
i just named it on so i dont have to type boolvalue

and i now realise the boolvalue existing is just more work so i fixed it

the camera isnt on the player, its on a part

Can you read?

the camera isnt on the head.
theres fancy scripts that change the cframe.

Try this:

local Camera = workspace.CurrentCamera
local part = script.Parent
local spotlight = part.SpotLight

Camera.CameraType = Enum.CameraType.Scriptable

game:GetService("RunService").Heartbeat:Connect(function()
	part.CFrame = Camera.CFrame
end)

Make sure you change the CameraType to Scriptable if you want to play with the camera