Tween Not Working in Loop

So Im trying to tween somehting in a runservice loop but it doesnt want to work

spawn(function()
	New = game:GetService("RunService").Heartbeat:Connect(function()
			print("working")
			Methods.Tween(Location, .35, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut, 0, false,0, {StudsOffset = Vector3.new(0,math.random(12,14),0)})
		end)
	end)
end
3 Likes

You need to wait for the current tween to finish before you do the next one, also why are using the RunService?

Why are you trying to Tween every Heartbeat? This means it will replay the Tween again practically before it starts. I don’t know what you are doing, but RunService does not seem like the right option here.

The thing is I need it to continuously tween over and over again, and a runservice loop is the only thing I could think of

Since tweens doesn’t yeilds you need to wait for it to finish and you also need to play it

Thats not the problem thats a custom tween module I made

If this is the case, just set the “repeat count” property of TweenInfo to -1. This will cause it to repeat infinitely.

Example:

local tweenInfo = TweenInfo.new(
	.35, -- Time
	Enum.EasingStyle.InOut, -- EasingStyle
	Enum.EasingDirection.Out, -- EasingDirection
	-1, -- RepeatCount (when less than zero the tween will loop indefinitely)
	false, -- Reverses (tween will reverse once reaching it's goal)
	0 -- DelayTime
)

Then just call Tween:Cancel() to exit the loop.

The problem is that I need the tween to also stop after the player presses a certain button

This will not work, waiting in a connection will not stop the signal from firing again.

You must wait until it finishes that is

Tween.Completed:Wait()

else it will fire too many times and cancel itself each time it plays
You can also use @ExcessEnergy Idea i think it’s better

Then just call Tween:Cancel() when you want it to end.

You can use @ExcessEnergy method, or to make your current one work just make that tween method return the tween after it is played from the module, and do this: (Also, you do not need to use spawn because the connection won’t yield the thread, also if you want to make a new thread use coroutines instead)

local Tween

New = game:GetService("RunService").Heartbeat:Connect(function()
		if (Tween) and (Tween.PlaybackState ~= Enum.PlaybackState.Completed) then
		   return 
		end
		Tween = Methods.Tween(Location, .35, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut, 0, false,0, {StudsOffset = Vector3.new(0,math.random(12,14),0)})
	end)
end)

exactly he must disconnect the event or something