Tween won't play again after pausing

So I am making a minigame test and the tween won’t play after pause. Here’s the video.


Local Script

local Gui = script.Parent.Parent
local Holder = Gui:WaitForChild("Holder")
local CircleToMove = Holder:WaitForChild("MainCircle")
local CircleTrigger = Holder:WaitForChild("CircleTrigger")
local CircleStart = Holder:WaitForChild("CircleStart")
local CircleEnd = Holder:WaitForChild("CircleEnd")
local TweenService = game:GetService("TweenService")
local Events = game.ReplicatedStorage.Events
local MinigameStart = Events:WaitForChild("CageMinigameStart")
local UserInputService = game:GetService("UserInputService")
local ImageLabel = Holder:WaitForChild("Icon")
local CircleCollisions = false
local Lives = 3
local Interactions = 0
local tween1 = TweenService:Create(CircleToMove, TweenInfo.new(1.5, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut), {Position = CircleStart.Position})
local tween2 = TweenService:Create(CircleToMove, TweenInfo.new(1.5, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut), {Position = CircleEnd.Position})

function collidesWith(gui1, gui2)  -----some parameter you can put your GUI1 and GUI2 in 
	local gui1_topLeft = gui1.AbsolutePosition
	local gui1_bottomRight = gui1_topLeft + gui1.AbsoluteSize

	local gui2_topLeft = gui2.AbsolutePosition
	local gui2_bottomRight = gui2_topLeft + gui2.AbsoluteSize

	return ((gui1_topLeft.x < gui2_bottomRight.x and gui1_bottomRight.x > gui2_topLeft.x) and (gui1_topLeft.y < gui2_bottomRight.y and gui1_bottomRight.y > gui2_topLeft.y)) -- return their values
end

function bump()
	local random_rotation
	local ogsize = ImageLabel.Size
	if math.random(1, 2) == 1 then
		random_rotation = -30
	else
		random_rotation = 30
	end
	ImageLabel.Rotation = random_rotation
	ImageLabel.Size = ogsize + UDim2.new(0,20,0,20)
	local tween = TweenService:Create(ImageLabel, TweenInfo.new(0.3, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out), {Rotation = 0})
	local tween2 = TweenService:Create(ImageLabel, TweenInfo.new(0.3, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out), {Size = ogsize})
	tween:Play()
	tween2:Play()
end

MinigameStart.OnClientEvent:Connect(function()
	Gui.Enabled = true
	Interactions = 0
	Lives = 3
	Holder.Position = UDim2.new(0.5,0,-1,0)
	local tween = TweenService:Create(Holder, TweenInfo.new(1, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out), {Position = UDim2.new(0.5,0,0.5,0)})
	tween:Play()
	tween.Completed:Wait()
	CircleCollisions = true
end)

UserInputService.InputBegan:Connect(function(input, engine_processed)
	if engine_processed then
		return
	end

	if input.UserInputType == Enum.UserInputType.MouseButton1 or input.KeyCode == Enum.KeyCode.Space then
		if CircleCollisions then
			if collidesWith(CircleToMove, CircleTrigger) then
				local tween = TweenService:Create(CircleTrigger, TweenInfo.new(0.3, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out), {Size = CircleTrigger.Size + UDim2.new(-0.025,0,0,0)})
				tween:Play()
				Interactions += 1
				bump()
			else
				Lives -= 1
				print(Lives)
			end
		end
	end
end)

game:GetService("RunService").RenderStepped:Connect(function()
	if Lives <= 0 then
		game.Players.LocalPlayer.Character.Humanoid.Health = 0
	end
	if Interactions >= 8 then
		Interactions = 0
		CircleCollisions = false
		tween1:Pause()
		tween2:Pause()
		task.wait(0.5)
		local tween3 = TweenService:Create(Holder, TweenInfo.new(1, Enum.EasingStyle.Cubic, Enum.EasingDirection.In), {Position = UDim2.new(0.5,0,-1,0)})
		tween3:Play()
		tween3.Completed:Wait()
		Gui.Enabled = false
	end
end)

while wait() do
	if CircleCollisions then
		tween1:Play()
		tween1.Completed:Wait()
		tween2:Play()
		tween2.Completed:Wait()
	end
end
1 Like
while wait() do
	if CircleCollisions then
		tween1:Play()
		tween1.Completed:Wait()
		tween2:Play()
		tween2.Completed:Wait()
	end
end

Runs once, you will need to wrap it in a function to call it again when you reset.

By the way, wait() is deprecated and should be replaced with task.wait(), and you shouldn’t wait for too many children, once you wait for one you’ve basically waited for them all.

2 Likes

What code should I wrap into a function?

1 Like

Add the play, the pause and the resume lines all in one function so it can be infinitely played.

1 Like

So I made a few changes that you said to my code and it still doesn’t work. What did I do wrong?

local Gui = script.Parent.Parent
local Holder = Gui:WaitForChild("Holder")
local CircleToMove = Holder:WaitForChild("MainCircle")
local CircleTrigger = Holder:WaitForChild("CircleTrigger")
local CircleStart = Holder:WaitForChild("CircleStart")
local CircleEnd = Holder:WaitForChild("CircleEnd")
local TweenService = game:GetService("TweenService")
local Events = game.ReplicatedStorage.Events
local MinigameStart = Events:WaitForChild("CageMinigameStart")
local UserInputService = game:GetService("UserInputService")
local ImageLabel = Holder:WaitForChild("Icon")
local CircleCollisions = false
local Lives = 3
local Interactions = 0
local TriggerOriginSize = CircleTrigger.Size
local CircleOriginPosirion = CircleToMove.Position
local tween1 = TweenService:Create(CircleToMove, TweenInfo.new(1.5, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut), {Position = CircleStart.Position})
local tween2 = TweenService:Create(CircleToMove, TweenInfo.new(1.5, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut), {Position = CircleEnd.Position})
local PlayerWeld = Instance.new("WeldConstraint")

function collidesWith(gui1, gui2)  -----some parameter you can put your GUI1 and GUI2 in 
	local gui1_topLeft = gui1.AbsolutePosition
	local gui1_bottomRight = gui1_topLeft + gui1.AbsoluteSize

	local gui2_topLeft = gui2.AbsolutePosition
	local gui2_bottomRight = gui2_topLeft + gui2.AbsoluteSize

	return ((gui1_topLeft.x < gui2_bottomRight.x and gui1_bottomRight.x > gui2_topLeft.x) and (gui1_topLeft.y < gui2_bottomRight.y and gui1_bottomRight.y > gui2_topLeft.y)) -- return their values
end

function loop()
	print("im playing")
	tween2:Play()
	tween2.Completed:Wait()
	print("aaaa")
	tween1:Play()
	tween1.Completed:Wait()
end

function start(object)
	PlayerWeld.Enabled = true
	PlayerWeld.Parent = object
	PlayerWeld.Part0 = object
	PlayerWeld.Part1 = game.Players.LocalPlayer.Character.PrimaryPart
	CircleTrigger.Size = TriggerOriginSize
	CircleToMove.Position = CircleOriginPosirion
	CircleCollisions = false
	Gui.Enabled = true
	Interactions = 0
	Lives = 3
	Holder.Position = UDim2.new(0.5,0,-1,0)
	local tween = TweenService:Create(Holder, TweenInfo.new(1, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out), {Position = UDim2.new(0.5,0,0.5,0)})
	tween:Play()
	tween.Completed:Wait()
	CircleCollisions = true
end

function bump()
	local random_rotation
	local ogsize = ImageLabel.Size
	if math.random(1, 2) == 1 then
		random_rotation = -30
	else
		random_rotation = 30
	end
	ImageLabel.Rotation = random_rotation
	ImageLabel.Size = ogsize + UDim2.new(0,20,0,20)
	local tween = TweenService:Create(ImageLabel, TweenInfo.new(0.3, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out), {Rotation = 0})
	local tween2 = TweenService:Create(ImageLabel, TweenInfo.new(0.3, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out), {Size = ogsize})
	tween:Play()
	tween2:Play()
end

MinigameStart.OnClientEvent:Connect(function(object)
	start(object)
end)

UserInputService.InputBegan:Connect(function(input, engine_processed)
	if engine_processed then
		return
	end

	if input.UserInputType == Enum.UserInputType.MouseButton1 or input.KeyCode == Enum.KeyCode.Space then
		if CircleCollisions then
			if collidesWith(CircleToMove, CircleTrigger) then
				local tween = TweenService:Create(CircleTrigger, TweenInfo.new(0.3, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out), {Size = CircleTrigger.Size + UDim2.new(-0.025,0,0,0)})
				tween:Play()
				Interactions += 1
				bump()
			else
				Lives -= 1
				print(Lives)
			end
		end
	end
end)

game:GetService("RunService").RenderStepped:Connect(function()
	if Lives <= 0 then
		game.Players.LocalPlayer.Character.Humanoid.Health = 0
	end
	if Interactions >= 8 and CircleCollisions then
		CircleCollisions = false
		PlayerWeld.Enabled = false
		tween1:Pause()
		tween2:Pause()
		task.wait(0.5)
		local tween3 = TweenService:Create(Holder, TweenInfo.new(1, Enum.EasingStyle.Cubic, Enum.EasingDirection.In), {Position = UDim2.new(0.5,0,-1,0)})
		tween3:Play()
		tween3.Completed:Wait()
		Gui.Enabled = false
	end
end)

while task.wait() do
	if CircleCollisions == true then
		loop()
	end
end
1 Like

Did you turn CircleCollisions back on?

1 Like

Yes, I did turn CircleCollisions back on.

1 Like

Try canceling the tween instead of pausing it.

1 Like

Although it did fix the problem, if the circle was going right, somehow when all tweens are cancelled, the circle would go left.

local Gui = script.Parent.Parent
local Holder = Gui:WaitForChild("Holder")
local CircleToMove = Holder:WaitForChild("MainCircle")
local CircleTrigger = Holder:WaitForChild("CircleTrigger")
local CircleStart = Holder:WaitForChild("CircleStart")
local CircleEnd = Holder:WaitForChild("CircleEnd")
local TweenService = game:GetService("TweenService")
local Events = game.ReplicatedStorage.Events
local MinigameStart = Events:WaitForChild("CageMinigameStart")
local UserInputService = game:GetService("UserInputService")
local ImageLabel = Holder:WaitForChild("Icon")
local CircleCollisions = false
local Lives = 3
local Interactions = 0
local TriggerOriginSize = CircleTrigger.Size
local CircleOriginPosirion = CircleToMove.Position
local tween1 = TweenService:Create(CircleToMove, TweenInfo.new(1.5, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut), {Position = CircleStart.Position})
local tween2 = TweenService:Create(CircleToMove, TweenInfo.new(1.5, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut), {Position = CircleEnd.Position})
local InfoGui = Holder:WaitForChild("InfoGui")
local OgSpot1 = InfoGui.MainCircle1.Position
local OgSpot2 = InfoGui.MainCircle2.Position
local ogsize = ImageLabel.Size

function collidesWith(gui1, gui2)  -----some parameter you can put your GUI1 and GUI2 in 
	local gui1_topLeft = gui1.AbsolutePosition
	local gui1_bottomRight = gui1_topLeft + gui1.AbsoluteSize

	local gui2_topLeft = gui2.AbsolutePosition
	local gui2_bottomRight = gui2_topLeft + gui2.AbsoluteSize

	return ((gui1_topLeft.x < gui2_bottomRight.x and gui1_bottomRight.x > gui2_topLeft.x) and (gui1_topLeft.y < gui2_bottomRight.y and gui1_bottomRight.y > gui2_topLeft.y)) -- return their values
end

function loop()
	print("im playing")
	tween2:Play()
	tween2.Completed:Wait()
	print("aaaa")
	tween1:Play()
	tween1.Completed:Wait()
end

function fadein(value)
	if value == "In" then
		local fadeintween = TweenService:Create(script.Parent.InfoGui, TweenInfo.new(0.3, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {Transparency = 0})
		fadeintween:Play()
		for i,v in InfoGui:GetDescendants() do
			if v:IsA("Frame") or v:IsA("UIStroke") or v:IsA("ImageLabel") then
				if v.Transparency == 1 then
					if not (v.Name == "CircleStart1" or v.Name == "CircleStart2" or v.Name == "CircleEnd1" or v.Name == "CircleEnd2") then
						if (v.Name == "CorrectIcon" or v.Name == "WrongIcon") then
							local fadeintween = TweenService:Create(v, TweenInfo.new(0.3, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {ImageTransparency = 0})
							fadeintween:Play()
						else
							local fadeintween = TweenService:Create(v, TweenInfo.new(0.3, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {Transparency = 0})
							fadeintween:Play()
						end
					end
				end
			end
		end
	else
		local fadeintween = TweenService:Create(script.Parent.InfoGui, TweenInfo.new(0.3, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {Transparency = 1})
		fadeintween:Play()
		for i,v in InfoGui:GetDescendants() do
			if v:IsA("Frame") or v:IsA("UIStroke") or v:IsA("ImageLabel") then
				if not (v.Name == "CircleStart1" or v.Name == "CircleStart2" or v.Name == "CircleEnd1" or v.Name == "CircleEnd2") then
					if (v.Name == "CorrectIcon" or v.Name == "WrongIcon") then
						local fadeintween = TweenService:Create(v, TweenInfo.new(0.3, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {ImageTransparency = 1})
						fadeintween:Play()
					else
						local fadeintween = TweenService:Create(v, TweenInfo.new(0.3, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {Transparency = 1})
						fadeintween:Play()
					end
				end
			end
		end
	end
end

function start()
	InfoGui.MainCircle1.Position = OgSpot1
	InfoGui.MainCircle2.Position = OgSpot2
	InfoGui.Transparency = 1
	for i,v in InfoGui:GetDescendants() do
		if v:IsA("Frame") or v:IsA("UIStroke") or v:IsA("ImageLabel") then
			if (v.Name == "CorrectIcon" or v.Name == "WrongIcon") then
				v.ImageTransparency = 1
			else
				v.Transparency = 1
			end
		end
	end
	CircleTrigger.Size = TriggerOriginSize
	CircleToMove.Position = CircleOriginPosirion
	CircleCollisions = false
	Gui.Enabled = true
	Interactions = 0
	Lives = 3
	Holder.Position = UDim2.new(0.5,0,-1,0)
	local tween = TweenService:Create(Holder, TweenInfo.new(1, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out), {Position = UDim2.new(0.5,0,0.5,0)})
	tween:Play()
	tween.Completed:Wait()
	fadein("In")
	task.wait(0.8)
	TweenService:Create(InfoGui.MainCircle1, TweenInfo.new(1, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut), {Position = InfoGui.CircleEnd1.Position}):Play()
	task.wait(1.5)
	TweenService:Create(InfoGui.MainCircle2, TweenInfo.new(1, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut), {Position = InfoGui.CircleEnd2.Position}):Play()
	task.wait(1.5)
	fadein("Out")
	task.wait(0.5)
	CircleCollisions = true
end

function bump()
	local random_rotation
	if math.random(1, 2) == 1 then
		random_rotation = -30
	else
		random_rotation = 30
	end
	ImageLabel.Rotation = random_rotation
	ImageLabel.Size = ogsize + UDim2.new(0,20,0,20)
	local tween = TweenService:Create(ImageLabel, TweenInfo.new(0.3, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out), {Rotation = 0})
	local tween2 = TweenService:Create(ImageLabel, TweenInfo.new(0.3, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out), {Size = ogsize})
	tween:Play()
	tween2:Play()
end

MinigameStart.OnClientEvent:Connect(function()
	start()
end)

UserInputService.InputBegan:Connect(function(input, engine_processed)
	if engine_processed then
		return
	end

	if input.UserInputType == Enum.UserInputType.MouseButton1 or input.KeyCode == Enum.KeyCode.Space then
		if CircleCollisions then
			if collidesWith(CircleToMove, CircleTrigger) then
				local tween = TweenService:Create(CircleTrigger, TweenInfo.new(0.3, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out), {Size = CircleTrigger.Size + UDim2.new(-0.025,0,0,0)})
				tween:Play()
				Interactions += 1
				bump()
			else
				Lives -= 1
				print(Lives)
			end
		end
	end
end)

game:GetService("RunService").RenderStepped:Connect(function()
	if Lives <= 0 then
		game.Players.LocalPlayer.Character.Humanoid.Health = 0
	end
	if Interactions >= 8 and CircleCollisions then
		CircleCollisions = false
		tween1:Cancel()
		tween2:Cancel()
		task.wait(0.5)
		local tween3 = TweenService:Create(Holder, TweenInfo.new(1, Enum.EasingStyle.Cubic, Enum.EasingDirection.In), {Position = UDim2.new(0.5,0,-1,0)})
		tween3:Play()
		tween3.Completed:Wait()
		Gui.Enabled = false
	end
end)

while task.wait() do
	if CircleCollisions == true then
		loop()
	end
end

Cancelling a tween will reset it back to the original position before the tween was played.

Adding onto what Acorn said, make it tween to the position you want it to go to after the tweens have been canceled.