Tween only plays once

local PosPosPos = {
Pos1 = {UDim2.new(0.092, 0,0.2, 0), UDim2.new(0.861, 0,0.2, 0)},
Pos2 = {UDim2.new(0.092, 0,0.4, 0), UDim2.new(0.861, 0,0.4, 0)},
Pos3 = {UDim2.new(0.092, 0,0.7, 0), UDim2.new(0.861, 0,0.7, 0)},
}
local Randompos = PosPosPos["Pos"..math.random(1,3)]
local clone = selected:Clone()
local ObjectToTween = clone
repeat
ObjectToTween:TweenPosition(Randompos[2])
task.wait(1)
ObjectToTween:TweenPosition(Randompos[1]) 
until ended == true or killed == true

hello I have this game where the user tries to click the criminal thatā€™s moving. Iā€™m using tween to make the criminal move.

I use a repeat loop to tween the criminal until the player kills the criminal (HENCE the killed variable) i also have a variable to tell if the player loses the game (HENCE the ended variable)

I added a print() inside the repeat loop and the print plays normally however it seems the tween is only playing once but is being fired correctly every time the repeat, repeats.

I could very well be wrong but it seems to me that you want to wait until killed or ended are equal to true instead of false?

until ended == false or killed == false

should be

until ended == true or killed == true

unlike a while statement, the repeat loop breaks upon a condition being true

my bad, Itā€™s my fault. In the actual code it used

until killed == true or ended == true

the reason I typed it incorrectly in the help statement is because I changed some things, so to make it more simple to read/understand I wrote it from memory and not copy + paste.

all the changes I made I reverted because other bugs started appearing from those changes.

tell me what prints when running these inserted print statements

local PosPosPos = {
Pos1 = {UDim2.new(0.092, 0,0.2, 0), UDim2.new(0.861, 0,0.2, 0)},
Pos2 = {UDim2.new(0.092, 0,0.4, 0), UDim2.new(0.861, 0,0.4, 0)},
Pos3 = {UDim2.new(0.092, 0,0.7, 0), UDim2.new(0.861, 0,0.7, 0)},
}
local Randompos = PosPosPos["Pos"..math.random(1,3)]
local clone = selected:Clone()
local ObjectToTween = clone
print(ended)
print(killed)
repeat
ObjectToTween:TweenPosition(Randompos[2])
task.wait(1)
ObjectToTween:TweenPosition(Randompos[1]) 
until ended == true or killed == true

it prints the following:

21:34:58.142 false - Client - LocalScript:118
21:34:58.142 false - Client - LocalScript:119

I also put a print inside the repeat loop and after the repeat loop and the repeat loop plays exactly as itā€™s supposed to.

I tried printing inside of the repeat loop and it repeats until the proper time

same with the print after the repeat loop. It only plays at the correct time. The issue seems to be with the tween. I tried printing the positions of the tween and the positions are correct. I also tried printing the guiā€™s current position and the tween position just in case it was tweening it to the same location for an unknown reason and itā€™s not

try

repeat
local TweenInstance = ObjectToTween:TweenPosition(Randompos[2])
TweenInstance.Completed:Wait()
TweenInstance = ObjectToTween:TweenPosition(Randompos[1]) 
TweenInstance.Completed:Wait()
until ended == true or killed == true

Iā€™m getting the follow error from your correction:
Players.SAKDEVRBLX_alt.PlayerGui.ScreenGui.Menu.Moving.Frame.LocalScript:123: attempt to index boolean with ā€˜Completedā€™

while ended ~= true or killed ~= true do
    ObjectToTween:TweenPosition(Randompos[2])
    task.wait(1)
    ObjectToTween:TweenPosition(Randompos[1]) 
    task.wait(1)
end

The corrections i previously made on my own before posting this was trying a while loop, however a while loop doesnā€™t work either.

:TweenPosition only has a callback after you Define the UDim2, TweenDirection, TweenStyle, Length, Override The callback will be assigned to a function

.Completed is not a callback for UDim2 built in tweens. you will need to use tweenservice for this

Documentation

image

ah then

repeat
local TweenInstance = ObjectToTween:Create(Randompos[2])
TweenInstance:Play()
TweenInstance.Completed:Wait()
TweenInstance = ObjectToTween:Create(Randompos[1]) 
TweenInstance:Play()
TweenInstance.Completed:Wait()
until ended == true or killed == true

will work i think

1 Like

A bit embarrassing but I forgot to put a task.wait() after the second tween.

What i had:
repeat

pcall(function()
clone:TweenPosition(Randompos[2]) task.wait(1) clone:TweenPosition(Randompos[1])
end)
until killed == true or ended == true

What iā€™m supposed to have:

repeat 
pcall(function()
clone:TweenPosition(Randompos[2]) task.wait(1) clone:TweenPosition(Randompos[1])  task.wait(1)
end)
until killed == true or ended == true

make sure to use scroller on the ā€˜what im supposed to haveā€™ btw

ah I see. I think tweenInstance.Completed:Wait() is more effective than waiting since the tween can take longer if the client is lagging. What variable is tweenservice?

It uses built in tween function because I havenā€™t memorized the Tween Service requirements.

I think itā€™s like this:

local tweenservice = game:GetService(TweenService)
local Tween = TweenInfo.new(0.5, Enum.EasingStyle.Quint, Enum.EasingDirection.In, 0, false, 0)
Tween:Play()

However I donā€™t know where to put the object and Iā€™m unsure if it will work on GUI.

Yes that is the correct way to make a TweenInfo variable.
You can mitigate some of the information as well if it is not all necessary.

local Tween = TweenInfo.new(0.5, Enum.EasingStyle.Quint, Enum.EasingDirection.In)

Here is an example of one of my tweens

local pathwayVelocity = TweenInfo.new(routeData[2], Enum.EasingStyle.Linear)
local pathwayMovement = bulletMovement:Create(bulletInstance, pathwayVelocity, {CFrame = routeData[1]}) -- bulletInstance is the object
pathwayMovement:Play()
pathwayMovement.Completed:Wait()

this is too complex for me, sorry I donā€™t understand it.
Like I said I havenā€™t even memorized the basic params.
I also see a lot of undefined variables that Iā€™m ā€œSupposedā€ to know what is.

Ah sorry I will replace all parameters with explanations below

local pathwayVelocity = TweenInfo.new(0.5, Enum.EasingStyle.Linear) -- Time to complete tween, easingStyle
local pathwayMovement = bulletMovement:Create(ObjectToTween, pathwayVelocity, {Position = Randompos[2]}) -- the object to tween, tweenInfo, and the property to tween (you may have to change the locations of each position to Vector3.new() instead of UDim2.new())
pathwayMovement:Play()
pathwayMovement.Completed:Wait()

but if the marked solution works then you should probably just use that I think