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!
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?
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.
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.
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.
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.
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?
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.