Flashlight won't turn on/off when I press F

So, my flashlight won’t turn on/off here’s some informations:

  1. What do I want to achieve?

-Basically when I press F the flashlight turns on/off.

  1. What is the issue?
    -When I press F it just keeps printing “turning on” but doesn’t do anything.

  2. More Information
    -My flashlight is a script inside of StarterCharacterScripts.
    -The flashlight isn’t a GUI.

Here’s the script:

local UIS = game:GetService("UserInputService")
local cam = workspace.CurrentCamera
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local LightOn = false

game.StarterPlayer.CameraMode = Enum.CameraMode.LockFirstPerson

UIS.InputBegan:Connect(function(input,chatting)

	if LightOn == false and input.KeyCode == Enum.KeyCode.F and chatting == false then

		player.Character.Flashlight.Disabled = false
		print("turning on")

	elseif LightOn == true and input.KeyCode == Enum.KeyCode.F and chatting == false then

		player.Character.Flashlight.Disabled = true
		print("turning off")

	end

end)

Thank you for reading!

You need to set your LightOn variable appropriately when you turn the light on / off

1 Like

It’s because your are doing:

player.Character.Flashlight.Disabled = true

It has to be:

player.Character.Flashlight.Enabled = false

For it to turn off. And aswell for the other.

As @kaizenmarcell said, Disabled isn’t a thing.
Also why are you adding a conditional statement to elseif?

	if LightOn == false and input.KeyCode == Enum.KeyCode.F and chatting == false then

		player.Character.Flashlight.Disabled = false
		print("turning on")

	elseif LightOn == true and input.KeyCode == Enum.KeyCode.F and chatting == false then

		player.Character.Flashlight.Disabled = true
		print("turning off")

	end

Make it more simple by doing this:

	if input.KeyCode == Enum.KeyCode.F and chatting == false then
        if player.Character.Flashlight.Enabled == false then 
    		player.Character.Flashlight.Enabled = true
    		print("turning on")
	    else
	    	player.Character.Flashlight.Enabled = false
	    	print("turning off")
	    end
    end

That way you can get rid of LightOn altogether

Yeah, almost you got it right, but it’s not enbaled = false/true, it has to be disabled = false/true, since it’s a script.

I recommend just doing this to simplify it, it’s a lot more efficient than what you’re doing by the way and should also fix the issue! :slight_smile:

UIS.InputBegan:Connect(function(input,chatting)
	local flashlight = player.Character.Flashlight

	if input.KeyCode == Enum.KeyCode.F and not chatting then

		flashlight.Disabled = not flashlight.Disabled
		print("switching")

	end

end)
1 Like