Playing Sounds With Tweens

I want to play a sound for opening and closing a door, but adding the sound to the tween causes the tween to lag, requiring you to double click the door to open it, and it also plays all the sounds wacky like, this is the code, what do I do?

local click = script.Parent:WaitForChild("ClickDetector")
local tweenService = game:GetService("TweenService")

local open = script.Parent.Open
local close = script.Parent.Close

local door1cf = script.Parent.CFrame
local door2cf = script.Parent.Parent.Door2.CFrame

local opened = script.Parent.Parent.Opened

local tween1 = tweenService:Create(script.Parent, TweenInfo.new(.5), {CFrame = door2cf})
local tween2 = tweenService:Create(script.Parent, TweenInfo.new(.5), {CFrame = door1cf})

click.MouseClick:Connect(function()
	if opened.Value == false then
		open:Play()
		tween2:Play()
		opened.Value = true
	else
		close:Play()
		tween1:Play()
		opened.Value = false
	end
end)

opened.Changed:Connect(function()
	if opened.Value == true then
		wait(5)
		close:Play()
		tween2:Play()
		opened.Value = false
	end
end)
3 Likes

Likely because the sound and tween fires twice. When you open the door;

click.MouseClick:Connect(function()
	if opened.Value == false then
		open:Play()
		tween2:Play()
		opened.Value = true

You change the value of opened. And when the value of opened changes

opened.Changed:Connect(function()
	if opened.Value == true then
		wait(5)
		close:Play()
		tween2:Play()
		opened.Value = false
	end
end)

It attempts to play the tween and sound again.

2 Likes

Separate Sound and Tween Logic: Instead of playing the sound and starting the tween at the same time, you can use the Completed event of the tween to trigger the sound. This ensures that the tween completes before the sound is played.
Adjust Wait Time: Instead of using wait(5), consider using a shorter wait time or eliminating it altogether, as it may cause delays and affect the responsiveness of the door.

local click = script.Parent:WaitForChild("ClickDetector")
local tweenService = game:GetService("TweenService")

local open = script.Parent.Open
local close = script.Parent.Close

local door1cf = script.Parent.CFrame
local door2cf = script.Parent.Parent.Door2.CFrame

local opened = script.Parent.Parent.Opened

local tween1 = tweenService:Create(script.Parent, TweenInfo.new(.5), {CFrame = door2cf})
local tween2 = tweenService:Create(script.Parent, TweenInfo.new(.5), {CFrame = door1cf})

local function playSound()
    if opened.Value then
        close:Play()
    else
        open:Play()
    end
end

click.MouseClick:Connect(function()
    if opened.Value == false then
        tween1:Play()
        tween1.Completed:Connect(playSound)
        opened.Value = true
    else
        tween2:Play()
        tween2.Completed:Connect(playSound)
        opened.Value = false
    end
end)

opened.Changed:Connect(function()
    if opened.Value == true then
        wait(1) --  adjust this to your perffered time
        tween2:Play()
        tween2.Completed:Connect(playSound)
        opened.Value = false
    end
end)
1 Like

This helped me come up with a theory, I think @coke_cripsXD wants the door to close automatically after 5 seconds have passed but the script logic is incorrect. Here’s how the script will need to be like for that to work:

local tweenService = game:GetService"TweenService"

local click = script.Parent:WaitForChild"ClickDetector"

local opened = script.Parent.Parent:WaitForChild"Opened"

local open = script.Parent:WaitForChild"Open"
local close = script.Parent:WaitForChild"Close"

local door1cf = script.Parent.CFrame
local door2cf = script.Parent.Parent:WaitForChild"Door2".CFrame

local tween1 = tweenService:Create(script.Parent, TweenInfo.new(0.5), {CFrame = door2cf})
local tween2 = tweenService:Create(script.Parent, TweenInfo.new(0.5), {CFrame = door1cf})

click.MouseClick:Connect(function()
	if opened.Value then return end
	opened.Value = true
	open:Play()
	tween2:Play()

	task.wait(5)

	close:Play()
	tween1:Play()
	tween1.Completed:Wait()
	opened.Value = false
end)
1 Like

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