How would I keep this sound from repeating?

I’ve been recently making a menu system for my upcoming game and I have a “Press any key to start” button. When the player presses any key, a “Ding” sound is played and the player continues to the join match UI. The problem is you can press any key as much as you want and the sound will keep playing over and over. Since this is a InputBegan type thing, I’m not sure how to do this.

As you can see in line 103, the sound is being played.
Here’s the script:

local Player = game.Players.LocalPlayer
local Character = Player.Character
local Camera = workspace.CurrentCamera
local Mouse = game.Players.LocalPlayer:GetMouse()
local DefaultCFrame = workspace.CameraPart.CFrame
local PartyCFrame = workspace.PartyCameraPart.CFrame
local Scale = 160
local InMenu = true
local UIS = game:GetService("UserInputService")

local UI = script.Parent
local KeyButton = UI:WaitForChild('KeyFrame')
local FadeFrame = UI:WaitForChild('FadeFrame')
local SpawnText = FadeFrame:WaitForChild('Spawn')
local AnyKey = KeyButton:WaitForChild('PressKeyLabel')
local Queue = KeyButton:WaitForChild('Queue')
local tweenService = game:GetService('TweenService')
local tweenInfo = TweenInfo.new(
	0.6,
	Enum.EasingStyle.Quart,
	Enum.EasingDirection.Out,
	0,
	false,
	0
)

local tweenInfo2 = TweenInfo.new(
	0.6,
	Enum.EasingStyle.Quart,
	Enum.EasingDirection.Out,
	0,
	false,
	0
)

UI.Enabled = true

local newColor = Color3.fromRGB(216, 86, 86)
local oldColor = Color3.fromRGB(255, 255, 255)

local tween = tweenService:Create(FadeFrame, tweenInfo, {BackgroundTransparency = 0})
local tweenText = tweenService:Create(SpawnText, tweenInfo, {TextTransparency = 0})

local Presstween = tweenService:Create(AnyKey, tweenInfo, {TextTransparency = 0})
local PressStroketween = tweenService:Create(AnyKey, tweenInfo, {TextStrokeTransparency = 0})

local Queuetween = tweenService:Create(Queue, tweenInfo, {TextTransparency = 0})
local QueueStroketween = tweenService:Create(Queue, tweenInfo, {TextStrokeTransparency = 0})

local tween2 = tweenService:Create(FadeFrame, tweenInfo2, {BackgroundTransparency = 1})
local tweenText2 = tweenService:Create(SpawnText, tweenInfo2, {TextTransparency = 1})

local ColorTween = tweenService:Create(AnyKey, tweenInfo, {TextColor3 = newColor})
local ColorTween2 = tweenService:Create(AnyKey, tweenInfo, {TextColor3 = oldColor})

repeat wait()
	Camera.CameraType = Enum.CameraType.Scriptable
until Camera.CameraType == Enum.CameraType.Scriptable

local inMenuVal = Player:WaitForChild("InMenu")
local connections = {} -- this is for stopping the 'Heartbeat' function from running after character has reloaded

local function cameraFunc() -- this function should be called when player goes back into menu (if that is an option)
	if connections.playerInMenu ~= nil then -- prevent the function from running more than once
		return
	end

	connections.playerInMenu = game:GetService("RunService").Heartbeat:Connect(function()
		if not inMenuVal.Value then -- when player is not in menu
			UI.Enabled = false -- I'm going to assume 'UI' is your ScreenGui

			if connections.playerInMenu then
				connections.playerInMenu:Disconnect() -- disconnects the function
				connections.playerInMenu = nil
			end
		end
		
		Camera.Focus = DefaultCFrame
		local Center = Vector2.new(Camera.ViewportSize.X/2, Camera.ViewportSize.Y/2)
		local MoveVector = Vector3.new((Mouse.X-Center.X)/Scale, -(Mouse.Y-Center.Y)/Scale, 0)
		Camera.CFrame = DefaultCFrame * CFrame.Angles(math.rad(MoveVector.X), math.rad(MoveVector.Y), math.rad(MoveVector.Z))
		Camera.FieldOfView = 75
	end)
end

Camera.CFrame = workspace.CameraPart.CFrame

inMenuVal.Changed:Connect(function()
	if inMenuVal.Value then
		cameraFunc() -- run camera function when player goes into menu again
	else
		Camera.CameraSubject = Player.Character:WaitForChild("Humanoid")
	end
end)

cameraFunc()

wait(3.5)
Presstween:Play()
PressStroketween:Play()
UIS.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.Keyboard and inMenuVal.Value then
		KeyButton.Ding:Play()
		ColorTween:Play()
		task.wait(3.5)
		DefaultCFrame = workspace.PartyCameraPart.CFrame
		AnyKey.Visible = false
		Queue.Visible = true
		ColorTween2:Play()
		Queuetween:Play()
		QueueStroketween:Play()
	end
end)

You can try removing the connection after any key is pressed.

-- Here, you assign the connection to a variable so you can disconnect it later
local anyKeyPressConnection
anyKeyPressConnection = UIS.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.Keyboard and inMenuVal.Value then
		anyKeyPressConnection:Disconnect() -- Disconnect the connection
		KeyButton.Ding:Play()
		ColorTween:Play()
		task.wait(3.5)
		DefaultCFrame = workspace.PartyCameraPart.CFrame
		AnyKey.Visible = false
		Queue.Visible = true
		ColorTween2:Play()
		Queuetween:Play()
		QueueStroketween:Play()
	end
end)
1 Like

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