Im trying to make a toggleable head lamp but Im New to this

This is like my first time ever using userinputservice, and my script seems to not give me an error but it doesnt produce the desired output.

Basically; Player presses ‘L’, and the lamp will turn off (character spawns with it on already), and pressed again makes it turn on. But it doesnt want to turn off so I am confused.

local humanoid = script.Parent:WaitForChild("Humanoid")
local player = game.Players.LocalPlayer
local character = player.Character

--parts
local lightSource = game.StarterPlayer.StarterCharacter.LightSource
local light = lightSource.HeadSpotLight.Enabled
local lightBeam = lightSource.light.Enabled

local headlampOn = true

uis.InputBegan:Connect(function(inp, processed)
	if processed  then return end
	if inp.KeyCode == Enum.KeyCode.L then
		lightSource.Material = ("Glass")
		lightSource.BrickColor = BrickColor.new("Really black")
		light = false
		lightBeam = false
		headlampOn = not headlampOn
    	if inp.KeyCode == Enum.KeyCode.L and headlampOn == false then
		     lightSource.Material = ("Neon")
	    	 lightSource.BrickColor = BrickColor.new("Institutional white")
	    	 light = true
	    	 lightBeam = true
	    	 headlampOn = headlampOn
		end
	end
end)

I may be dumb here, but why are you accessing “lightSource” through starterplayer?

if your trying to get the lightsource from the players character, then you need to access it from the character and not starter player

1 Like

well its a morph, the “headlamp” is like apart of the startercharacter, so?

No because the character is cloned upon spawn, there are 2 character models one in starter character and one in workspace because of the cloning as @TimeAndLocation mentioned.

Try his suggestion first. You can also print out the instance and click in console to make sure it is getting the correct instance.

local character = player.Character or player.CharacterAdded:Wait()
local lightSource = character .LightSource
print(character)
1 Like

oh ok this makes a lot of sense, ill try this hold on

I made a few changes, and it seems to not return an error at the moment, but its not producing the desired outcome, anything I should change or implement, like remote events/server scripts so its server sided as well?

local uis = game:GetService("UserInputService")
local humanoid = script.Parent:WaitForChild("Humanoid")
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

--parts
local lightSource = character.LightSource

local headlampOn = true

uis.InputBegan:Connect(function(inp, processed)
	if processed  then return end
	if inp.KeyCode == Enum.KeyCode.L and headlampOn == true then
		lightSource.Material = ("Glass")
		lightSource.BrickColor = BrickColor.new("Really black")
		headlampOn = false
		
		if inp.KeyCode == Enum.KeyCode.L and headlampOn == false then
			lightSource.Material = ("Neon")
			lightSource.BrickColor = BrickColor.new("Institutional white")
			headlampOn = true

		end
	end
end)

You should revisit the logic of the code with the nested if statements, I suspect this is causing the issue. I will not give out the script and just point out the issue.

You should also view other code on how an input toggle is achieved like this one using if else

Tysm, This getting the desired result, thank you again for this aswell.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.