Trouble with Camera Tween

Hey, developers!

I have a big issue that I’ve been unable to fix in the past day. In my game, Megalodon Survival, I’ve been trying to replace a GUI transition with a transition that rotates around the map before teleporting players.

The issue I’ve come across is that I can’t seem to play this tween more than once… it’s fired for all clients from the server using a remote event that plays the tween but it simply won’t play for any transitions after the first time it’s been fired. If I enable a loop, I can’t manage to stop the camera tween with anything as it goes on infinitely.

You can take a look at the main script which fires the events, the camera script, and a preview of what it looks like during the transition.

Main:

	--CAMERA TEST
	
	game.Lighting.DepthOfField.Enabled = false
	transitionFunction:FireAllClients({Action = 'Play'})
	
	wait(10)
	
	game.Lighting.DepthOfField.Enabled = false
	transitionFunction:FireAllClients({Action = 'Stop'})
	
	--CAMERA TEST

Camera:

local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")

local target = workspace:FindFirstChild("transitionCamera")  -- The object to rotate around
local camera = workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Scriptable
local rotationAngle = Instance.new("NumberValue")
local tweenComplete = false

local cameraOffset = Vector3.new(0, 64, 384) --0, 10, 12
local rotationTime = 10  -- Time in seconds
local rotationDegrees = 360
local rotationRepeatCount = 0  -- Use -1 for infinite repeats
local lookAtTarget = true  -- Whether the camera tilts to point directly at the target

local plr = game.Players.LocalPlayer
local transitionFunction = game.ReplicatedStorage:WaitForChild('Transition')

transitionFunction.OnClientEvent:Connect(function(args)
	local function updateCamera()
		if not target then return end
		camera.Focus = target.CFrame
		local rotatedCFrame = CFrame.Angles(0, math.rad(rotationAngle.Value), 0)
		rotatedCFrame = CFrame.new(target.Position) * rotatedCFrame
		camera.CFrame = rotatedCFrame:ToWorldSpace(CFrame.new(cameraOffset))
		if lookAtTarget == true then
			camera.CFrame = CFrame.new(camera.CFrame.Position, target.Position)
		end
	end

	-- Set up and start rotation tween
	local tweenInfo = TweenInfo.new(rotationTime, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, rotationRepeatCount)
	local tween = TweenService:Create(rotationAngle, tweenInfo, {Value=rotationDegrees})
	tween.Completed:Connect(function()
		tweenComplete = true
	end)

	if args.Action == 'Play' then
		tween:Play()
	end
	if args.Action == 'Stop' then
		tween:Cancel()
	end

	-- Update camera position while tween runs
	RunService.RenderStepped:Connect(function()
		if tweenComplete == false then
			updateCamera()
		end
	end)
end)

Preview:
https://gyazo.com/5252c542c59078ca59a148fa5dd16ae9

I hope I can get some help on this!!! If you can help, I really appreciate it. :slight_smile:

1 Like

It could be because you are only setting the variable tweenComplete once at the start of the script.

So when the tween completes it sets tweenComplete to true meaning that every other time the function gets fired the RunService doesnt do anything because tweenComplete is true.

A few things I would mention though is watch out for memory leaks because at the moment you are making a new RunService connection every time the event is fired and you are making a new tween Completed function as well. You should disconnect these functions when the tween finishes or just move the outside the function to create them only once.

I tried commenting it out/calling it at different times and all that did was break it.

Wait what did you comment out? All you would need to do is just add

tweenComplete = false

at the top of your onClientEvent function

:thinking: Hm, exactly where should I add that? Sorry, I just wanna make sure I’m getting it right

transitionFunction.OnClientEvent:Connect(function(args)
    tweenComplete = false  -- Over here !!!!

	local function updateCamera()
		if not target then return end
		camera.Focus = target.CFrame
		local rotatedCFrame = CFrame.Angles(0, math.rad(rotationAngle.Value), 0)
		rotatedCFrame = CFrame.new(target.Position) * rotatedCFrame
		camera.CFrame = rotatedCFrame:ToWorldSpace(CFrame.new(cameraOffset))
		if lookAtTarget == true then
			camera.CFrame = CFrame.new(camera.CFrame.Position, target.Position)
		end
	end

	-- Set up and start rotation tween
	local tweenInfo = TweenInfo.new(rotationTime, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, rotationRepeatCount)
	local tween = TweenService:Create(rotationAngle, tweenInfo, {Value=rotationDegrees})

    -- Also I would recommend remove this tween.Completed and instead moving the tweenComplete over to the Stop action

	if args.Action == 'Play' then
		tween:Play()
	end
	if args.Action == 'Stop' then
        tweenComplete = true
		tween:Cancel()
	end

	-- Update camera position while tween runs
	RunService.RenderStepped:Connect(function()
		if tweenComplete == false then
			updateCamera()
		end
	end)
end)

Thank you!

So what happened after I added that in is that it put the camera into position just fine each round, but only the first time the transition ran did it rotate.

Wait sorry is that a bug of the code I put above or is everything working for you?

Well I don’t believe it’s your fault, the camera goes into position instead of doing nothing after the first round, it just doesn’t rotate anymore

Ok I have to go now but I will take another look at it later. If anybody else finds anything be free to reply.

1 Like

If you can help me with this, please do! We’ve gotten some progress through help, now I just need to figure out another issue! :slight_smile: