Play two different sounds when gui is visible or invisible

I want to make one sound play when the GUI is turned off and another sound is played when it turns on

local u = game:GetService("UserInputService")
local on = false
local off = false
local char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local light = Instance.new("SpotLight")
light.Range = 1000 --adjust as needed
light.Parent = char:WaitForChild("Head")
light.Enabled = false
local NightVisionONSound = script.Parent.NVOn
local NightVisionOFFSound = script.Parent.NVOff

u.InputBegan:Connect(function(input) -- input is when you press a key
	if input.KeyCode == Enum.KeyCode.N then -- click N
		on = not on
		script.Parent.Frame.Visible = on
		game.Lighting.NightVision.Enabled = on
		game.Lighting.Bloom.Enabled = on
		light.Enabled = on
		NightVisionONSound:Play()
		if NightVisionONSound.Ended and script.Parent.Frame.Visible == on then
		NightVisionOFFSound:Play()
		end
		end
end)
1 Like

the current code will play both sounds almost simultaneously when you press the N key because the NightVisionONSound:Play() and NightVisionOFFSound:Play() are not correctly set for the on and off states of the gui, try

if script.Parent.Frame.Visible then
			NightVisionONSound:Play()
		else
			NightVisionOFFSound:Play()
		end

sounds are played when entering the game and are no longer produced
does this scripts have errors?

local u = game:GetService("UserInputService")
local on = false
local off = false
local char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local light = Instance.new("SpotLight")
light.Range = 1000 --adjust as needed
light.Parent = char:WaitForChild("Head")
light.Enabled = false
local NightVisionONSound = script.Parent.NVOn
local NightVisionOFFSound = script.Parent.NVOff
if script.Parent.Frame.Visible then
	NightVisionONSound:Play()
else
	NightVisionOFFSound:Play()
end

u.InputBegan:Connect(function(input) -- input is when you press a key
	if input.KeyCode == Enum.KeyCode.N then -- click N
		on = not on
		script.Parent.Frame.Visible = on
		game.Lighting.NightVision.Enabled = on
		game.Lighting.Bloom.Enabled = on
		light.Enabled = on
		end
end)


You might be a little slow but thats a okay!!!

Put the function inside of (a function is the thing you cloned in your script)
image

A explaination: You are doing the function once, when you first load in, not when you press N.
so move it inside that box.

2 Likes

it should be placed inside the u.InputBegan:Connect function

3 Likes

what to do if the off sound is made twice after the on sound and also this is the other way around?

could be due to the sound not being stopped before a new one is played. You can add :Stop() before :Play() so that any playing sounds can be made sure that they are stopped

My problem is that the ON sound is produced if I press any key, even the mouse buttons

you should place it inside of the

if input.KeyCode == Enum.KeyCode.N then – click N part

, not the

u.InputBegan:Connect(function(input) – input is when you press a key

You could try doing this. I put some more stuff into it such as not switching it when the player is typing in the chat.

I didnt test it though so I’m not sure if it works.

u.InputBegan:Connect(function(input, gpe) 
	if gpe then return end -- Checks if the player is in the chat or doing something where they shouldn't be able to activate the GUI
	if input.KeyCode == Enum.KeyCode.N then
		on = not on -- changes the state of the night vision to the opposite
		local sound = on and NightVisionONSound or NightVisionOFFSound -- if NightVision is on, then sound = NightVisionONSound  or if off then sound = NightVisionOFFSound
		sound:Play() -- Play the sound
		-- Set Lighting and Night Vision Effects
		script.Parent.Frame.Visible = on
		game.Lighting.NightVision.Enabled = on 
		game.Lighting.Bloom.Enabled = on
		light.Enabled = on
	end
end)
2 Likes

You put the function inside of the wrong thing.
image

Red is when you detect ANY input, even mouse buttons.
orange is when you detect the input of N. so move it down, like what @Chrasher_06 said, use his version! he has gameprocessing which stops chat!

1 Like

Thank you very much, this solution worked!

2 Likes

no problem, happy to serve the community🫡

1 Like

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