Simple Tween not tweening

I have created a simple Tween that increases and decreases the audio.
The first Tween was working for a little while, now both aren’t.

The issue is non of them are actually ‘Tweening’, It just kind of plays the audio normally.

game.ReplicatedStorage.MusicOn.Event:Connect(function()
	local Sound = game.Workspace.Sounds.chasemusic
	if Sound.Playing == false then
		local TweenService = game:GetService("TweenService")
		local Tween_Info = TweenInfo.new(3)
		local Tween_Goal = {Volume = 1}
		local Sound2 =  game.Workspace.Sounds.Scream
		local Tween = TweenService:Create(Sound, Tween_Info, Tween_Goal)
		Sound.Volume = 0
		Sound:Play()
		Sound2:Play()
		Tween:Play()
	end
end)

game.ReplicatedStorage.MusicOff.Event:Connect(function()
	local Sound = game.Workspace.Sounds.chasemusic
	if Sound.Playing == true then
		local TweenService = game:GetService("TweenService")
		local Tween_Info2 = TweenInfo.new(3)
		local Tween_Goal2 = {Volume = 0}
		local Tween2 = TweenService:Create(Sound, Tween_Info2, Tween_Goal2)
		Tween2:Play()
		Tween2.Completed:Wait()
		Sound:Stop()
	end
end)

Try printing the playback state of both:

image

Do you have any other code that can adjust properties of that? Because as is the code works flawlessly for me. I’m pretty sure the issue lies elsewhere.

1 Like

Try adding Tween.Completed:Wait() to all tweens you’re using. It looks like something else is causing the tween to be cancelled.


This is the only other thing,
It fires the Remote Event

Try putting a print after the event fires. It could be that it’s firing multiple times causing the tweens to cancel each other out. Although even if that is the case putting Tween.Completed:Wait() should fix the issue. If that doesn’t work you could always use a remote function instead which would yield the script until the tween is done.

Could you please provide a code example of what you mean? because I have put Tween.Completed:Wait() on one of my Events.

No matter what I try I can't recreate your issue

Ignore the fact the code is slightly different the main thing is the while loop was an attempt at a reconstruction of too many events firing at once.

local TweenService = game:GetService("TweenService")
local Sound = game.Workspace.Sound

local Tween_Info = TweenInfo.new(3)
local Fade = {
	In = TweenService:Create(Sound, Tween_Info, {Volume = 1}),
	Out = TweenService:Create(Sound, Tween_Info, {Volume = 0}),
}

local In = function()
	if not Sound.Playing then
		Sound.Volume = 0
		Sound:Play()
		Fade.In:Play()
		
		--local scream =  game.Workspace.Sounds.Scream
		--scream:Play()
	end
end

local Out = function()
	if Sound.Playing then
		Fade.Out:Play()
	end
end

Fade.Out.Completed:Connect(function()
	Sound:Stop()
end)

wait(5) --Without the wait studio loads the sound after the tween on my computer.  So it loads at full volume, though I doubt this is your issue.

while true do
	for i=1, 100 do
		In()
		--Out()
	end
	wait()
end

If you’re willing to supply a bare-bones studio file I can see if I can help you more, but otherwise I’ve hit a wall.

It’s also worth noting I couldn’t recreate this:
image
I can get it to cancel, but it always has a complete before the next cancel. This has multiple cancels.

Do this after each tween.

Tween:Play()
Tween.Completed:Wait()

I think you should also use a remote function instead of a remote event since the former will yield the pathfinding script until a result is returned. This would stop the event being fired multiple times, cancelling previous tweens.

Yielding isn’t really necessary. This ensures it will only fire once

if Sound.Playing == false then
    --....
    Sound:Play()
end

It is technically possible to have the issue appear on the second one though, but that’s because it takes 3 seconds to update the sound state so multiple entries is possible. But yielding for 3 seconds in the pathfinding script might not be the best idea. It will make the npc wait 3 seconds before moving again. This could be circumvented by adding an immediate debounce to that function, but in my testing I wasn’t able to replicate this issue even when firing it over and over.

I should also mention that one will cancel out the other, but that is a separate issue. The issue is that the tweens aren’t working, and that is what I am having trouble replicating.

That’s true. I didn’t see that. The issue I’m seeing is the fact that the event is clearly being fired multiple times, despite the fact that the Sound.Playing buffer exists. I expect the soundtrack he is using is shorter than 3 seconds meaning it allows for the event to fire multiple times.

I also don’t think that the remote function would interfere with his game, since he breaks the pathfinding script directly after the remote event currently. Either way, it probably isn’t an ideal solution and a better debounce would probably be more effective.

1 Like

That is true, whenever I yield in the pathfinding script the NPC waits 3/however many seconds I say wait() for. And I am using a Remote Function because it fires from the server to the server again, not Client/Server, Server/Client, etc. if Playing == true/false is the only simple debounce that seems to work for my script.

I will also provide a Studio file so you can take a look, I tinkered with a few things, but the same result.

NPC testzone.rbxl (46.4 KB)

you can do something like this

function tweenSound(sound, speed, volume)
	local tween = game:GetService("TweenService"):Create(sound, TweenInfo.new(speed), {Volume = volume})
	tween:Play()
end

try testing this and see if this is what you’re looking for

NPC testzone.rbxl (47.5 KB)

Yes! Tysm.

This worked perfectly!