Tween playing double

I am making a card battling game where the cards slam into each other when they attack using a tween to animate that. The first time a battle is ran, everything works properly, but if the player repeats that battle, the tweens are played double for each time the battle is repeated and I’m not sure why that’s happening.

My BattleClient.StartBattle() function is called whenever the player interacts with an enemy. I am pretty sure I’ve included all of the relevant code to my issue but let me know if something else could be causing my problem as I cut out a lot of code.

(I’m new to tweening and coding in general)

function BattleClient.StartBattle(enemyName)
	
	repeat
		task.wait(1)
		BattleClient.PlayerAttackTween()
		task.wait(1)
		BattleClient.EnemyAttackTween()
		task.wait(1)
		battleRound += 1
	until isInBattle.Value == false

end

function BattleClient.PlayerAttackTween()
	
	local tweenInfo = TweenInfo.new(0.3, Enum.EasingStyle.Exponential, Enum.EasingDirection.In)
	local tweenGoal = {}
	tweenGoal.Position = UDim2.new((playerCardIcon.Position.X.Scale * 1.37), 0, playerCardIcon.Position.Y.Scale, 0)
	
	local Tween = TweenService:Create(playerCardIcon, tweenInfo, tweenGoal)
	
	Tween.Completed:Connect(function()
	BattleClient.PlayerAttack()
	playerCardIcon.Position = OGPlayerCardPos
	Tween:Destroy()
	end)

	Tween:Play()
	
end

Here is a video showcasing my issue:

As you can see, everything works properly the first time around but not the second. (The enemy has 3 wolf cards and I don’t yet have a visual for them switching cards so thats why it looks like it takes three hits to take down one wolf)

I suppose you haven’t tried a debounce check?

Otherwise, the code should look like:

local debounce = false

function exampleFunction()
	if debounce == false then
		debounce = true
		-- do your things here
		debounce = false
	end
end

It should make the animation always only play once

1 Like

Relay that, I think it may be “battleRound” or something not being reset which leads to the tweens being multiplied. I’m not sure what it is because you haven’t shown the full code, but given this knowledge I’m sure you’ll find out.

1 Like

Disable the first tween. , or just keep tweening it forever.

I found the issue after some testing. The tween doesn’t play twice if I wait several seconds to start the battle again, so I’m pretty sure if the player starts a battle immediately after just finishing one, the repeat loop is still in a task.wait() so it hasn’t gotten to the until check yet to end the loop, which leads to it playing twice because when that loop from the first battle ends and you’re in another battle already then it keeps looping alongside the new one. I’m not familiar with debounces so I’ll try that method to see if it avoids this issue.

Edit: debounce method did not solve the issue

I found a workaround, but not exactly a solution. I created a new bool value recentlyInBattle and set it to true after combat is over and after isInBattle.Value is set to false, and then set it back to false after a couple seconds to let the first repeat loop finish and break, and then made it where the player can’t enter combat while recentlyInBattle is set to true.

function BattleClient.PlayerVictory()
	
	battleRound = 1
	isInBattle.Value = false
	battleGui.background.Visible = false
	
	recentlyInBattle = true --the new variable i created
	task.wait(battleSpeed + 1)
	recentlyInBattle = false
	
end

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