I want to tween a loading bar, which goes in reverse for a set size when a condition is met, and another tween is used to tween the loading bar as fully loaded.
So my problem is that the tween returns “Completed” even though I set it on pause to run the tween back in reverse?
if plr.Character:FindFirstChildOfClass("Tool").ID.Value == "Sliced" then
tween:Play()
-- other code was not included
if currentSize.X.Scale >= 0.1 then
tween:Pause()
reverseTween:Play()
reverseTween.Completed:Wait()
tween:Play()
end
tween.Completed:Connect(function()
print("Completed")
end)
end
the function isnt being looped, its stored inside a proximity prompt. The only loops inside are two for loops to load in the possible dishes to a separate table, and a loop to which determines what image id to load in, it shouldn’t have any impact on the tween itself.
local part = loadingBar.Loading
local currentSize = loadingBar.Loading.Size -- this data is constant
local tween = tweenService:Create(part, TweenInfo.new(timer, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {
Size = Udim2.fromScale(1, 1)
})
local reverseTween = tweenService:Create(part, TweenInfo.new(timer / 2, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {
Size = Udim2.fromScale(part.Size.X.Scale - .15, 1)
})
code 2
if plr.Character:FindFirstChildOfClass("Tool").ID.Value == "Sliced" then
tween:Play()
-- other code was not included
while part.Size.X.Scale < 0.1 do
task.wait(.02)
end
tween:Pause()
reverseTween:Play()
reverseTween.Completed:Wait()
tween:Play()
tween.Completed:Connect(function()
print("Completed")
end)
end
I replaced both, however, the reverse tween sets it back to its initial scale of 0 no matter what the size is; even when I put in an item into the pot as the first iteration, the reverse tween immediately plays, and “Completed” is printed out every time I put an item into the pot.
Are you reassigning the ‘tween’ variable anywhere in your proximity prompt function? Doing so appears to fire the ‘Completed’ event of the old tween, which may be the source of your issue.
I had a look and no, I havent reassigned the tween variable anywhere in my function, only the reverseTween variable inside my if statement in the code snippet above:
if plr.Character:FindFirstChildOfClass("Tool").ID.Value == "Sliced" then
local reverseTween = tweenService:Create(part, TweenInfo.new(timer / 2, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {
Size = UDim2.fromScale(part.Size.X.Scale - 0.15, 1)
})
if currentSize.X.Scale == 0 then
tween:Play()
print("Playing Tween")
end
for itemName, indexes in pairs(foodList.food) do
if plr.Character:FindFirstChild(indexes.slicedName) then -- if found in the index then set the current
table.insert(storedItems, indexes.slicedName) -- add it to the current stored items
currentID = indexes.slicedImageID
local currentIndex = indexes.slicedName
break
end
end
local frameImage = currentTool.Handle.ImageBillboard:WaitForChild("Frame")["Image"..#storedItems]
frameImage.Image = currentID
plr.Character:FindFirstChildOfClass("Tool"):Destroy()
loadingBar.Visible = true
fire.Enabled = true
water.Enabled = true
if currentSize.X.Scale >= 0.1 then
tween:Pause()
reverseTween:Play()
reverseTween.Completed:Wait()
tween:Play()
end
tween.Completed:Connect(function()
print("Completed")
end)
end
Odd, it seems like the tween is getting restarted somewhere because the time the tween takes to complete appears to increase as you add more items to the pot.
What you could try doing is storing the connection from ‘tween.Completed:Connect()’ and disconnecting it before assigning ‘tween’ or when entering the proximity prompt function if a connection was previously made. This should clean up any previous connections so only the latest is used when the tween finishes.
Also, you may want to only have this event connected right after the tween is assigned so using the proximity prompt doesn’t duplicate the connection and cause overlapping or redundancy.
check to make sure that the tween completed with the correct PlaybackState.
When a tween is paused or stopped it will fire completed and return a PlaybackState, or the reason it stopped. To get the PlaybackState simply add it to the connect part
I modified the code so that the event is connected right after the tween is assigned, but it still did not change. I also added in the playbackState argument, and it seems that whenever I pause the current tween, and play another tween to tween it back to a smaller size, it returns with “Enum.PlaybackState.Cancelled”.
Whenever you play a tween on an instance that is already “tweening” it will automatically cancel all other tweens currently playing on that instance. I guess you could check for the cancelled and then create a new one when the second tween finishes. Just make sure to calculate new time for the new tween