NightVision doesnt turn off when the keycode input isnt pressed again

I made a nightvision script but i have an issue where if you were to press E again while nightvision is on, it wont turn off

Heres the script

local uis = game:GetService("UserInputService")


local char = script.Parent


local equipRE = game.ReplicatedStorage:WaitForChild("NVGsToggled")


local defaultAmbient = game.Lighting.OutdoorAmbient



uis.InputBegan:Connect(function(input, processed)


    if processed then return end

    if input.KeyCode == Enum.KeyCode.E then


    if char:FindFirstChild("NVGs") then 

            script.Sound:Play()

            equipRE:FireServer(true)


            game.Lighting.OutdoorAmbient = Color3.fromRGB(255, 255, 255)
            game.Lighting.NVG.Enabled = true

        else
            equipRE:FireServer(false)


            game.Lighting.OutdoorAmbient = defaultAmbient
            game.Lighting.NVG.Enabled = false
        end
    end
end)

Any help would be awesome thanks!

(I had to repost this because of some bug idk)

Try this:

--//Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
local Lighting = game:GetService("Lighting")

--//Variables
local equipRE = ReplicatedStorage:WaitForChild("NVGsToggled")
local char = script.Parent

--//Controls
local defaultAmbient = Lighting.OutdoorAmbient
local isEnabled = false

--//Functions
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if gameProcessedEvent then
		return
	end

	if input.KeyCode ~= Enum.KeyCode.E then
		return
	end

	isEnabled = not isEnabled

	if isEnabled then
		if not char:FindFirstChild("NVGs") then
			isEnabled = false
			
			return
		end

		script.Sound:Play()

		equipRE:FireServer(true)

		Lighting.OutdoorAmbient = Color3.fromRGB(255, 255, 255)
		Lighting.NVG.Enabled = true
	else
		equipRE:FireServer(false)

		Lighting.OutdoorAmbient = defaultAmbient
		Lighting.NVG.Enabled = false
	end
end)
1 Like

Thank you so much bro, I forgot to mention one thing though, while i was trying to fix this bug i also found that if you were to reset your charater the night vision would stay, any way to fix that?

You can fix that by adding a function to set the UI back to normal when the player is killed.

New code:

--//Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
local Lighting = game:GetService("Lighting")

--//Variables
local equipRE = ReplicatedStorage:WaitForChild("NVGsToggled")
local char = script.Parent
local Humanoid = char:WaitForChild("Humanoid")

--//Controls
local defaultAmbient = Lighting.OutdoorAmbient
local isEnabled = false

--//Functions
Humanoid.Died:Connect(function()
	if not isEnabled then
		return
	end
	
	isEnabled = false
	
	equipRE:FireServer(false)

	Lighting.OutdoorAmbient = defaultAmbient
	Lighting.NVG.Enabled = false
end)

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if gameProcessedEvent then
		return
	end

	if input.KeyCode ~= Enum.KeyCode.E then
		return
	end

	isEnabled = not isEnabled

	if isEnabled then
		if not char:FindFirstChild("NVGs") then
			isEnabled = false

			return
		end

		script.Sound:Play()

		equipRE:FireServer(true)

		Lighting.OutdoorAmbient = Color3.fromRGB(255, 255, 255)
		Lighting.NVG.Enabled = true
	else
		equipRE:FireServer(false)

		Lighting.OutdoorAmbient = defaultAmbient
		Lighting.NVG.Enabled = false
	end
end)

when the players dead, if you press e it still turns on, im so sorry for wasting your time bro :frowning:

Try this:

--//Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
local Lighting = game:GetService("Lighting")

--//Variables
local equipRE = ReplicatedStorage:WaitForChild("NVGsToggled")
local char = script.Parent
local Humanoid = char:WaitForChild("Humanoid")

--//Controls
local defaultAmbient = Lighting.OutdoorAmbient
local isEnabled = false
local isDead = false

--//Functions
Humanoid.Died:Connect(function()
	isDead = true
	
	if not isEnabled then
		return
	end

	isEnabled = false

	equipRE:FireServer(false)

	Lighting.OutdoorAmbient = defaultAmbient
	Lighting.NVG.Enabled = false
end)

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if gameProcessedEvent or input.KeyCode ~= Enum.KeyCode.E or isDead then
		return
	end

	isEnabled = not isEnabled

	if isEnabled then
		if not char:FindFirstChild("NVGs") then
			isEnabled = false

			return
		end

		script.Sound:Play()

		equipRE:FireServer(true)

		Lighting.OutdoorAmbient = Color3.fromRGB(255, 255, 255)
		Lighting.NVG.Enabled = true
	else
		equipRE:FireServer(false)

		Lighting.OutdoorAmbient = defaultAmbient
		Lighting.NVG.Enabled = false
	end
end)

could i make it so when the players dead, the script disables?

still can turn it on when player is dead

This should work:

--//Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
local Lighting = game:GetService("Lighting")

--//Variables
local equipRE = ReplicatedStorage:WaitForChild("NVGsToggled")
local char = script.Parent
local Humanoid = char:WaitForChild("Humanoid")

--//Controls
local defaultAmbient = Lighting.OutdoorAmbient
local isEnabled = false

--//Functions
Humanoid.Died:Connect(function()	
	if not isEnabled then
		return
	end

	isEnabled = false

	equipRE:FireServer(false)

	Lighting.OutdoorAmbient = defaultAmbient
	Lighting.NVG.Enabled = false
end)

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if gameProcessedEvent or input.KeyCode ~= Enum.KeyCode.E or Humanoid.Health <= 0 then
		return
	end

	isEnabled = not isEnabled

	if isEnabled then
		if not char:FindFirstChild("NVGs") then
			isEnabled = false

			return
		end

		script.Sound:Play()

		equipRE:FireServer(true)

		Lighting.OutdoorAmbient = Color3.fromRGB(255, 255, 255)
		Lighting.NVG.Enabled = true
	else
		equipRE:FireServer(false)

		Lighting.OutdoorAmbient = defaultAmbient
		Lighting.NVG.Enabled = false
	end
end)
1 Like

Yeah that fixed it, thank you so much bro, you dont know how much i appreciate this!!

1 Like

No problem. If you have any more questions, feel free to ask.