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)
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)
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
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)
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!