I’m making a script where if an event fires, it plays a tween, and I want to make it so that if the event fires again, it cancels the tween that’s playing (if one is playing at that time)
Tried a lot of different solutions, but nothing seems to be working. This is my current script (yes I know it’s messy, i will make it better after I fix this)
local tweenservice = game:GetService("TweenService")
local activeTween
game:GetService("ReplicatedStorage").Events.SetAnimals.OnClientEvent:Connect(function(prodtime, amounttonext, creating)
if activeTween then activeTween:Cancel() end
if not creating then
if not amounttonext then
activeTween = tweenservice:Create(script.Parent, TweenInfo.new(prodtime, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {Size = UDim2.new(0.971, 0,0.615, 0)})
activeTween:Play()
activeTween.Completed:Wait()
script.Parent.Size = UDim2.new(0,0,0.615,0)
activeTween = nil
else
local timecompleted = prodtime-amounttonext
script.Parent.Size = UDim2.new(timecompleted/prodtime*0.971,0,0.615,0)--5.4/14.4
activeTween = tweenservice:Create(script.Parent, TweenInfo.new(amounttonext, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {Size = UDim2.new(0.971, 0,0.615, 0)})
activeTween:Play()
activeTween.Completed:Wait()
script.Parent.Size = UDim2.new(0,0,0.615,0)
activeTween = nil
end
else
if not amounttonext then
activeTween = tweenservice:Create(script.Parent, TweenInfo.new(prodtime, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {Size = UDim2.new(0.971, 0,0.615, 0)})
activeTween:Play()
activeTween.Completed:Wait()
script.Parent.Size = UDim2.new(0,0,0.615,0)
activeTween = nil
else
local timecompleted = prodtime-amounttonext
script.Parent.Size = UDim2.new(timecompleted/prodtime*0.971,0,0.615,0)--5.4/14.4
activeTween = tweenservice:Create(script.Parent, TweenInfo.new(amounttonext, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {Size = UDim2.new(0.971, 0,0.615, 0)})
activeTween:Play()
activeTween.Completed:Wait()
script.Parent.Size = UDim2.new(0,0,0.615,0)
activeTween = nil
end
end
end)