Weird tween issue

Hello, I have been trying to fix this for quite a bit now (roughly 4 hours), and it’s been a real pain. What I am trying to do is make the current camera loop through cameras in the game, they are in folders in workspace called sector. The main issue with this is I cannot seem to stop the tween, I’ve tried Tween:Cancel(), Tween:Stop() and I also tried breaking the loop, but it does not work. It might be because of Tween.Completed:wait(), but I’m not entirely sure. Help would be greatly appreciated as this is essential for the game I am making.

When I press “Play”, the tween does not stop and it waits until it’s completed, but as I said before I want to stop the tween before it gets to the other camera.

GIF of the issue

The GIF above shows the Tween keep playing after I break / stop the tween.

Module code:

Code

function setup.stop()
stopped = true

camera.CameraType = Enum.CameraType.Custom
camera.CameraSubject = char

end

function setup.start()
local current_tab = 1
local current_sector = tab[1]
local default_sector = tab[1]
local current = 1
local default_camera = menu_cameras[default_sector]:FindFirstChild(“1”)

local tween_speed = 20

camera.CameraType = Enum.CameraType.Scriptable
camera.CFrame = default_camera.CFrame

local newTween

while true do
	if stopped then
		newTween:Stop()
		break
	end
	for _,v in next, menu_cameras[tab[current_tab]]:GetChildren() do
		local numCameras = #menu_cameras[tab[current_tab]]:GetChildren()

		current = current + 1	
			
		if current > numCameras then

			current = 2
			
			if #tab > 1 then
				current_tab = current_tab + 1
				
				if current_tab > #tab then
					current_tab = 1
				end
			end
			
			camera.CFrame = menu_cameras[tab[current_tab]][default_camera.Name].CFrame
		end
		
		newTween = libModule.tween(camera, TweenInfo.new(tween_speed, Enum.EasingStyle.Linear), {CFrame = menu_cameras[tab[current_tab]][current].CFrame})
		newTween:Play()
		
		newTween.Completed:wait()
	end
end

end

I call module.stop() in a LocalScript.

Like I said before, help would be greatly appreciated.

Your script is probably stuck with that newTween.Completed:wait() part. If the function you’re calling in the ModuleScript yields, your LocalScript yields too. Try wrapping the module.start() part with spawn.

Spawn(f) and coroutines allow you to call functions (even those that yield) without yielding the script.

spawn(function()
module.start()
end)

Thanks for the reply, but I already have the module.start() part wrapped in spawn, and the module.stop() part works perfectly fine, but the only issue I am having is with the tween not stopping.

You should probably do the newTween:Stop() in the module.stop() part then since the loop doesn’t end until .Completed:wait() is done.

If not, you should replace the newTween.Completed:wait() part with a repeat loop that should check if the tween is completed OR if stopped is true.

Example code (currently on mobile so I can’t test, sorry!)

repeat
wait()
if stopped then
newTween:Stop()
end
until newTween.TweenStatus == Enum.TweenStatus.Cancelled or newTween.TweenStatus == Enum.TweenStatus.Completed

How about you just tween the camera using Interpolate() and once the interpolation is finished set the cameras CFrame to a new area and then start a new cutscene/whatever your going for. Much easier and more efficient.

I have tried your reply and I get this error message.

Whoops! Replace :Stop() with :Cancel() instead, that one should work now :smiley:

Thanks for your reply once again, but it says TweenStatus is not a valid member of Tween.

Looks like Tweens use PlaybackState. Sorry for the errors, I’m on mobile and it’s hard to code lol
This one should finally work as intended:

repeat
wait()
if stopped then
newTween:Cancel()
end
until newTween.PlaybackState == Enum.PlaybackState.Cancelled or newTween.PlaybackState == Enum.PlaybackState.Completed
3 Likes

Thanks, works.

1 Like