How to wait until a tween is finished, but cancel when another tween starts

I want to wait for when a tween finishes, but if another tween starts, it will cancel the first one and wait for the new one to finish
But, when the second tween starts, the first tween gets canceled and it stops waiting, causing an unwanted firing of my remote event.
–Serverside

local function WinDecider(Winner)
	if Winner == "Attackers" then
		WinBar:FireAllClients("Attackers")
		AttackerWin=true
	elseif Winner == "Defenders" then
		WinBar:FireAllClients("Defenders")
		DefenderWin=true
	end
	if AttackerWin and DefenderWin then
		Draw=true
		WinBar:FireAllClients("Draw")
	end
end

–Clientside

WinBar.OnClientEvent:Connect(function(Winner)
	if Winner=="Attackers" then
		AttackerWin:Play()
		AttackerWin.Completed:Wait()
	elseif Winner=="Defenders" then
		DefenderWin:Play()
		DefenderWin.Completed:Wait()
	elseif Winner=="Draw" then
		AttackerWin:Pause()
		DefenderWin:Pause()
		Draw:Play()
		Draw.Completed:Wait()
	end
	print("a")
	WinBar:FireServer()
end)

A breakdown of how it should work goes: The winDecider runs if the timer runs out or the health reaches 0, firing the remote events with their respective keywords. Then it will play the respective tween and wait for the tween to finish before letting the serverside script continue.
If both the timer and the health reach 0 before the tween finishes, then first, the other win condition fires and starts waiting. And then Draw fires. Draw, then pauses the other two and waits for its own.
Here is where the problem lies. The pausing merely makes the other waits finish instantly and then fires the server. I see this as it prints “a” three times instead of once.
I need to find a way to stop the first 2 tweens from firing once draw starts.
I have tried a combined repeat wait() until (all 3 of the tweens.PlaybackState ~= Playing), But I could not get that to work for some reason.

2 Likes

u can code out the logic something like this

local currentTween

--some event
if currentTween then
   currentTween:Cancel()
end
currentTween = yourTween
currentTween:Play()
1 Like

I tried this:

WinBar.OnClientEvent:Connect(function(Winner)
	if Playingtween then
		Playingtween:Cancel()
	end
	if Winner=="Attackers" then
		Playingtween=AttackerWin
	elseif Winner=="Defenders" then
		Playingtween=DefenderWin
	elseif Winner=="Draw" then
		Playingtween=Draw
	end
	Playingtween:Play()
	Playingtween.Completed:Wait()
	print("a")
	WinBar:FireServer()
end)

And It has the same problem. I am thinking now, instead of having the tween time be the time where draws can happen (i.e both health and timer reach 0) I can just wait serverside and then fire once with what result happens, circumventing the issue.

1 Like

local tweentime = tweentime.duration (idk how to refer to tween time and do wait(tweentime)