Code Issues - Simultaneous Sound and Countdown Execution

Hello! How can I play the sound and run the countdown simultaneously, making the countdown decrease from 5 to 0 while the sound is playing? And when it reaches 0, the sound should stop. Currently, it plays the sound first and then starts the countdown, which is incorrect.

image

localscript:

-- Assume that Cooking_Buttons_List is a GUI with buttons, and FrameToShowHide is the frame to show/hide
local button = script.Parent
local frameToShowHide = script.Parent.Parent.Parent.Parent.Loading_UI
local loadingTextButton = frameToShowHide.loadingText

-- Duration in seconds to show the frame
local showDuration = 5
local countdownDuration = 5 -- Countdown duration in seconds

-- Reference to the tool in ReplicatedStorage
local specificTool = game.ReplicatedStorage.Tools:WaitForChild("Flash Light")  -- Replace "YourSpecificTool" with the actual name of your tool

local cancelCookingButton = script.Parent.Parent.Parent.cancelCookingButton

-- Variable to check if cancel button is pressed
local isCancelButtonPressed = false

-- Add the ID of the sound you want to play
local countdownSoundId = "rbxassetid://7056816387"  -- Replace 123456789 with the actual ID of your sound

-- Variable to store the sound instance
local countdownSound

-- Function to load the sound
local function loadSound()
	print("Loading sound...")
	countdownSound = Instance.new("Sound")
	countdownSound.SoundId = countdownSoundId
	countdownSound.Parent = game.Workspace
end

-- Function to play the countdown sound
local function playCountdownSound()
	print("Playing countdown sound...")
	countdownSound:Play()
	wait(countdownDuration)  -- Wait until the countdown is over
	countdownSound:Stop()
end

-- Function to handle cancel button press
local function cancelButtonPressed()
	print("Cancel button pressed.")
	isCancelButtonPressed = true
end

-- Connect the function to the MouseButton1Down event of the cancel button
cancelCookingButton.MouseButton1Down:Connect(cancelButtonPressed)

-- Function to show the frame and hide it after a specified time
local function showAndHideFrame()
	print("Showing the frame...")
	frameToShowHide.Visible = true  -- Show the frame

	-- Load and play the sound during the countdown
	loadSound()
	playCountdownSound()

	-- Countdown timer loop
	for i = countdownDuration, 0, -1 do
		loadingTextButton.Text = "The cooking is " .. i .. " seconds left until finished!"
		wait(1)  -- Wait for 1 second
	end

	print("Hiding the frame...")
	frameToShowHide.Visible = false  -- Hide the frame after the specified time

	local player = game.Players.LocalPlayer

	if player then
		-- Check if cancel button is pressed
		if isCancelButtonPressed then
			print("Cancel button pressed. Tool not added to the player's backpack.")
		else
			-- Add the specific tool to the player's backpack
			if specificTool then
				print("Adding the specific tool to the player's backpack...")
				local toolClone = specificTool:Clone()
				toolClone.Parent = player.Backpack
			else
				warn("Specific tool not found.")
			end
		end

		-- Reset the cancel button state
		isCancelButtonPressed = false
	else
		warn("Player not found.")
	end
end

-- Connect the function to the MouseButton1Down event of the button
button.MouseButton1Down:Connect(function()
	print("Button clicked, initiating showAndHideFrame function...")
	showAndHideFrame()
end)
1 Like

Oh sorry! I was able to solve it lol

local function playCountdownSound()
    print("Playing countdown sound...")
    
    coroutine.wrap(function()
        countdownSound:Play()
        wait(countdownDuration)
        countdownSound:Stop()
    end)()
end

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