Camera tween repeating itself

Hey! I’m trying to make a script that sets the players’ camera back to their character after it’s been viewing the stage in my game. I’ve managed to figure out a script for this, however the script is somehow repeating itself randomly which is what I need help with figuring out. Any help on how to get the tween to stop repeating itself and only change the player’s camera position once would be appreciated! :heart:

Script:

local Camera = game.Workspace.CurrentCamera
local TweenTime = 2
local TweenService = game:GetService("TweenService")

FillTween.Completed:Connect(function()
script.Parent.MixButton.Visible = false
wait(2)
game.TweenService:Create(Camera, TweenInfo.new(TweenTime, Enum.EasingStyle.Sine, Enum.EasingDirection.Out,0,false,0), {CFrame = Player.Character.Head.CFrame}):Play()
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All, true)
wait(0)
Camera.CameraType = "Custom"
Camera.CameraSubject = Player.Character.Humanoid
script.Parent.BillboardHandler.Disabled = false
script.Parent.MixLevel.Visible = false
script.Parent.MixLevel.MixBar.Size = UDim2.new(0.062, 0,1, 0)
script.Parent.MixLevel.MixBar.BackgroundTransparency = 1
end)

Video:

Once again, any help would be appreciated. Thank you! :thumbsup:

2 Likes

Try setting that last 0 in the tween info to the word nil. Also if that doesn’t work, try changing that true to false.

I remember one of the last 3 values had something to do with repeating, I just don’t remember which.

1 Like

I do not believe this is a matter of accidentally infinitely repeating the tween because the repeat value in your :Create function is 0, implying that it should not repeat infinitely.

I think this may have to do with your FillTween completion. Can we take a look at the code for your FillTween tween?

1 Like

try destroying the camera after the event is finished

1 Like

None of these solutions worked, sadly. Also, it’s already false, I tried changing it to true but that didn’t work either. : (

Sure!

local FillTween = TweenService:Create(script.Parent.MixLevel.MixBar, TweenInfo.new(3, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {Size = UDim2.new(1, 0,1, 0)})

1 Like

And where is it being called/played?

This didn’t work either, it just made the camera in the middle of the water. :man_shrugging:

Well, the FillTween fills up a progress bar when the player holds down the button as shown in the video. Also, I’m not too sure whether this script I did earlier to change the camera’s position would be the cause of it either.

Camera.CameraType = Enum.CameraType.Scriptable
		game.TweenService:Create(Camera, TweenInfo.new(TweenTime, Enum.EasingStyle.Sine, Enum.EasingDirection.Out,0,false,0), {CFrame = EndCFrame}):Play()

Edit: Here’s the FillTween Play/Pause scripts.

script.Parent.MixButton.MixText.MouseButton1Down:Connect(function()
	MouseBeingHeldDown = true
	while MouseBeingHeldDown == true do
		script.Parent.MixLevel.MixBar.BackgroundTransparency = 0
		FillTween:Play()
		wait()
	end
end)

script.Parent.MixButton.MixText.MouseButton1Down:Connect(function()
	MouseBeingHeldDown = true
	while MouseBeingHeldDown == true do
		script.Parent.MixLevel.MixBar.BackgroundTransparency = 0
		FillTween:Play()
		wait()
	end
end)

From first glance it seems that you are running the FillTween function for every iteration while MouseBeingHeldDown is equal to true. This is most likely the reason for your problem. The tween should only be called once.

However your new problem will be accounting for when a player releases the mouse button mid hold. If you have any questions with implementing this, let me know and I will get back to you.

Right, I got that but I’m quite confused how I would do that. Could you provide some help?

I am currently not at home right now so I will not be able to write a full explanation on how to approach this; however, I can provide you some resources to help you go about your situation. Once I am home in about an hour, I will be able to help you out more thoroughly.

Meanwhile, take a look at these articles.

https://developer.roblox.com/en-us/api-reference/event/GuiButton/MouseButton1Up

Talk to you soon.

1 Like

I have created a little demonstration showing you how I would approach this.

Here is the code from the video. Obviously, you will not want to copy and paste this code character for character but rather implement the strategies used in the video.

local replicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
local tweenService = game:GetService("TweenService")

local player = players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local testGui = playerGui:WaitForChild("Test")
local barFrame = testGui:WaitForChild("BarFrame") -- Blackbox
local bar = barFrame:WaitForChild("Bar") -- White filament
local action = testGui:WaitForChild("Action") -- Hold button

local fillTween = tweenService:Create(bar, TweenInfo.new(3, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {Size = UDim2.new(1,0,1,0)})

local cancelled = false

action.MouseButton1Down:Connect(function()
	fillTween:Play()
end)

action.MouseButton1Up:Connect(function()
	if fillTween.PlaybackState == Enum.PlaybackState.Playing then
		cancelled = true
		fillTween:Cancel()
		print("Cancelled")
	end
end)

fillTween.Completed:Connect(function()
	bar.Size = UDim2.new(0,0,1,0)
	if cancelled == true then
		cancelled = false
		return
	end
	print("Completed")
	--Do whatever
end)

Hopefully this helped. If you have any further questions about this, let me know!

Thanks for your help with this and the video, really appreciated. Sadly, this is a bit different to what I wanted as I didn’t want it to reset when the player lets the button go. However I think I only need to change a little of the code you provided me with to adapt to it, correct?

Edit: Also, I used Tween:Pause instead of Tween:Cancel in your tutorial’s case.

Yeah, that should be the case. Let me know if you have any other specific questions.

1 Like

Yikes, I implemented your fixes and surprisingly the issue is still occurring so now I’m wondering if it may not be due to that. Would another possible solution be making the script fire when the bar is full (it’s size) since using Tween:Completed doesn’t seem too reliable?

Hmm. Could I see your revamped code?

I figured out it wasn’t because of that. Take a look.

Edit: Additionally, the only way I could find a fix for this was to make a seperate whole LocalScript for it and disable the whole script when the Tween was completed. If you know any easier way or a fix to it, let me know. :broken_heart:

I don’t exactly know what you mean by that. Could you show me your LocalScript?

Sure.

local Player = game.Players.LocalPlayer

local Camera = game.Workspace.CurrentCamera

game.ReplicatedStorage.CameraLocal.OnClientEvent:Connect(function()

wait(2)

game.TweenService:Create(Camera, TweenInfo.new(2, Enum.EasingStyle.Sine, Enum.EasingDirection.Out,0,false,0), {CFrame = Player.Character.Head.CFrame}):Play()

game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All, true)

wait(0)

Camera.CameraType = "Custom"

Camera.CameraSubject = Player.Character.Humanoid

print("changed camera view")

script.Disabled = true

end)