Hello, I am making a looping camera for my game’s menu, however for some reason the repeat wait(1) until is not working.
Script 1
local tweening = script.Parent.Frame.tweening
local function tween()
local cam = workspace.CurrentCamera
cam.CameraType = Enum.CameraType.Scriptable
local cam1 = workspace['cam1']
local cam2 = workspace['cam2']
local cam3 = workspace['cam3']
local cam4 = workspace['cam4']
local ts = game:GetService('TweenService')
local t1 = ts:Create(cam,TweenInfo.new(5) ,{CFrame = cam1.CFrame})
local t2 = ts:Create(cam,TweenInfo.new(5) ,{CFrame = cam2.CFrame})
local t3 = ts:Create(cam,TweenInfo.new(5) ,{CFrame = cam3.CFrame})
local t4 = ts:Create(cam,TweenInfo.new(5) ,{CFrame = cam4.CFrame})
cam.CFrame = cam4.CFrame
tweening.Value = true
local shouldbreak = false
local function tweenai()
t1:Play()
wait(5)
t2:Play()
wait(5)
t3:Play()
wait(5)
t4:Play()
end
repeat tweenai()
until tweening:GetPropertyChangedSignal()
print('stopped tweening')
end
local function menu()
game.Lighting.Blur.Size = 24
script.Parent.Frame.Visible = true
tween()
end
menu()
script.Parent.Frame.TextButton.Activated:Connect(function()
game.Lighting.Blur.Size = 0
script.Parent.Frame.Visible = false
local cam = workspace.CurrentCamera
cam.CameraType = Enum.CameraType.Follow
end)
Script 2 (inside the text button used to press “play”)
local tweening = script.Parent.Frame.tweening
local function tween()
local cam = workspace.CurrentCamera
cam.CameraType = Enum.CameraType.Scriptable
local cam1 = workspace['cam1']
local cam2 = workspace['cam2']
local cam3 = workspace['cam3']
local cam4 = workspace['cam4']
local ts = game:GetService('TweenService')
local t1 = ts:Create(cam,TweenInfo.new(5) ,{CFrame = cam1.CFrame})
local t2 = ts:Create(cam,TweenInfo.new(5) ,{CFrame = cam2.CFrame})
local t3 = ts:Create(cam,TweenInfo.new(5) ,{CFrame = cam3.CFrame})
local t4 = ts:Create(cam,TweenInfo.new(5) ,{CFrame = cam4.CFrame})
cam.CFrame = cam4.CFrame
tweening.Value = true
local shouldbreak = false
local function tweenai()
t1:Play()
wait(5)
t2:Play()
wait(5)
t3:Play()
wait(5)
t4:Play()
end
repeat tweenai()
until tweening.Value == false
print('stopped tweening')
end
local function menu()
game.Lighting.Blur.Size = 24
script.Parent.Frame.Visible = true
tween()
end
menu()
script.Parent.Frame.TextButton.Activated:Connect(function()
game.Lighting.Blur.Size = 0
script.Parent.Frame.Visible = false
local cam = workspace.CurrentCamera
cam.CameraType = Enum.CameraType.Follow
end)
I did some debugging to find out that it’s the repeat wait(1) until’s problem, not the text problem.
True = tweening is enabled
False = tweening is meant to be disabled
As you can see, it detected that tweening was false. However, the repeat
I also put a print(“stopped tweening”) to debug at the part when the repeat wait(1) until stopped.
However, this did not print even when tweening was false.
I am not sure how to solve this error, any suggestions?
why dont you pull out the functions outside of a function? it sounds like that you are calling the tweenai function without waits until tweening value is false and you dont even set the tweening value to false.
The tweenai function takes 15 seconds to complete, the ‘until’ wont be tested again until it has finished, so probably you just need to wait a little longer!
You may need to think about cancelling any tweens which are running, and adding a check instead of waiting regardless, just to return from the tweenai function sooner.
Before addressing this question, there’s something I’d recommend changing in the function(s) where the Tweens are activated:
To avoid any inconsistencies that could come with manually using task.wait(#), I would recommend using the Tween.Completed event, since that’s a built-in way of waiting until the Tween has concluded playing.
Example revision:
local function tweenai()
t1:Play()
t1.Completed:Wait()
t2:Play()
t2.Completed:Wait()
t3:Play()
t3.Completed:Wait()
t4:Play()
t4.Completed:Wait()
end
As far as cancelling any ongoing Tweens when tweening.Value == false goes, here’s a full revision of the script that would make that possible (and if you have any questions about the specifics, feel free to ask!)
Example final revision
local TweenService = game:GetService("TweenService")
local Lighting = game:GetService("Lighting")
local Frame = script.Parent.Frame
local tweening = Frame.tweening
--[[
Based on the original post, you included this function in 2 separate scripts
but it's probably only necessary in one or the other
(or else you'll have multiple scripts doing the exact same thing)
--]]
Frame.TextButton.Activated:Connect(function()
tweening.Value = false
Lighting.Blur.Size = 0
Frame.Visible = false
local cam = workspace.CurrentCamera
cam.CameraType = Enum.CameraType.Follow
end)
---
local tweensTable = {}
tweening.Changed:Connect(function(newValue)
if newValue == false then
for currentIteration, tweenToPlay in tweensTable do
--print(currentIteration, tweenToPlay)
if tweenToPlay.PlaybackState == Enum.PlaybackState.Playing then
tweenToPlay:Cancel()
end
tweenToPlay:Destroy()
--warn(tweensTable)
end
table.clear(tweensTable)
end
end)
local function tween()
local cam = workspace.CurrentCamera
cam.CameraType = Enum.CameraType.Scriptable
-- It would be better to place all of these objects into a model with ModelStreamingMode set to "Atomic" so you only need to wait for the Model to load in
local cam1 = workspace:WaitForChild("cam1")
local cam2 = workspace:WaitForChild("cam2")
local cam3 = workspace:WaitForChild("cam3")
local cam4 = workspace:WaitForChild("cam4")
local tweenInfo = TweenInfo.new(1)
local t1 = TweenService:Create(cam, tweenInfo, {CFrame = cam1.CFrame})
local t2 = TweenService:Create(cam, tweenInfo, {CFrame = cam2.CFrame})
local t3 = TweenService:Create(cam, tweenInfo, {CFrame = cam3.CFrame})
local t4 = TweenService:Create(cam, tweenInfo, {CFrame = cam4.CFrame})
-- There are other ways of adding it to the table, however, I've done it this way for clarity
table.insert(tweensTable, t1)
table.insert(tweensTable, t2)
table.insert(tweensTable, t3)
table.insert(tweensTable, t4)
cam.CFrame = cam4.CFrame
tweening.Value = true
--local shouldbreak = false -- This variable was never referenced, so I have omitted it from the revised version
local function tweenai()
for currentIteration, tweenToPlay in tweensTable do
tweenToPlay:Play()
tweenToPlay.Completed:Wait()
print("Completed "..currentIteration)
end
end
repeat tweenai() until tweening.Value == false
warn('stopped tweening')
end
local function menu()
Lighting.Blur.Size = 24
Frame.Visible = true
tween()
end
menu()