Why does this pattern not loop how i want it to?

Hi everyone! so ive been trying to make a menu with a background/pattern looping forever but its not working how i wanted it to

This is when i play in studio

And this is when i play in the game


how do i fix this?
also, heres the script i used:

local MovingImage = script.Parent

while true do
	MovingImage:TweenPosition(UDim2.new(0, -5, -0.137, 0),"Out","Linear",2,true)
	wait(1)
	MovingImage:TweenPosition(UDim2.new(-0.075, 0, -0.25, 0),"Out","Linear",0,true)
	wait(0)
end

Thank you for reading :grinning_face_with_smiling_eyes:

2 Likes

I believe it’s due to the second tween playing before the first tween is completed
In order to do them asynchronously, you should use TweenService | Roblox Creator Documentation, (this is preferred)
Example :

while true do
    local firstTween = TweenService:Create(movingImage, TweenInfo.new(timeDuration, style, direction, ...), { -- table of all the properties to tween
        Position = Udim2.new(0, -5, -0.137, 0),
    })
    firstTween:Play()
    firstTween.Completed:Wait()
    -- same thing with the second tween
end

See all arguments that TweenService | Roblox Creator Documentation take.
See all arguments that TweenInfo | Roblox Creator Documentation take.

1 Like

so, i did what you told me (well atleast i think i did) but it doesn’t work

local TweenService = game:GetService("TweenService")

while true do
	local MovingImage = script.Parent
    local firstTween = TweenService:Create(MovingImage, TweenInfo.new(1, "Linear", "Out"), {
        Position = Udim2.new(0, -5, -0.137, 0),
    })
    firstTween:Play()
	firstTween.Completed:Wait()
	
	local secondTween = TweenService:Create(MovingImage, TweenInfo.new(1, "Linear", "Out"), {
		Position = Udim2.new(-0.075, 0, -0.25, 0),
	})
	secondTween:Play()
	secondTween.Completed:Wait()
end

and heres the error
image
also, the Udim2 had a blue line under it but idk if that helps or not

1 Like

You must use Enum | Roblox Creator Documentation in TweenInfo, e.g Enum.EasingStyle.Linear instead of "Linear" and Enum.EasingDirection.Out instead of "Out"

1 Like
local TweenService = game:GetService("TweenService")
local MovingImage = script.Parent

function CreateTween(Object, Info, Properties)
    local Tween = TweenService:Create(Object, Info, Properties)

    Tween:Play()
    Tween.Completed:Wait()
end

while true do
    CreateTween(MovingImage, TweenInfo.new(1, Enum.EasingStyle.Linear), {
        Position = UDim2.fromScale(0, -0.135)
    })

    MovingImage.Position = Udim2.fromScale(-0.075, -0.25)
end

you got an error because you need to use the global Enum
anyways I made a function that works like TweenService:Create, except is also plays the tween and waits for it to finish

also I didn’t use EasingDirection because Enum.EasingDirection.Out is in there by default

local MovingImage = script.Parent

while true do
	MovingImage:TweenPosition(UDim2.new(0, -5, -0.137, 0),"Out","Linear",2,true)
	task.wait(2)
	MovingImage:TweenPosition(UDim2.new(-0.075, 0, -0.25, 0),"Out","Linear",0.5,true)
	task.wait(0.5)
end

You set the length of time the tween plays for to two seconds but only wait 1 second before playing the next tween, you also didn’t specify a length of time that the second tween should play for and wait that length of time too so I’ve used 0.5 as an example.

1 Like

im not sure what to do, but the Udim2 has blue line under it and has an error
image

it also has this error in the output when i play the game

1 Like

that’s my fault, I over looked this
try UDim2.new instead

you have to capitalize “U” and “D”

1 Like

it works now, but it goes in both directions and not in a loop

1 Like

The fact that it works in studio, and does also work in game but not as expected, leads me to think that the problem could possibly be the image aspect ratio scaling. You can see that the studio window size is a different, skinnier size than the game window.

1 Like

one problem I see is the fact that you were using 0, -5, -0.137, 0
use scale for both, not scale and offset

ScaleX, OffsetX, ScaleY, OffsetY

scale should work for all devices

there is also a problem with TweenInfo that I am going to fix in my original code

2 Likes

I would also recommend using UDim2.fromScale(xScale, yScale)

1 Like

i fixed that and it works, but it goes in both directions at the same speed, what i want is for it to go in one direction and then instantly teleport back to its original position so it looks like it loops forever.

That seems to be caused by using the same TweenInfo with a Time parameter of 1 twice instead of 0 the second time. At that point you could just set the Position as well instead of tweening. Can you please reply with the latest script?

I just fixed TweenInfo, you have to fix UDim2.new(0, -5, -0.137, 0)

you can look at my code again

EDIT:
fixed it myself since you showed what it was

local TweenService = game:GetService("TweenService")
local MovingImage = script.Parent
local TweenInfo = TweenInfo.new(1, Enum.EasingStyle.Linear)

function CreateTween(Object, Info, Properties)
	local Tween  = TweenService:Create(Object, Info, Properties)

	Tween:Play()
	Tween.Completed:Wait()
end

while true do
	CreateTween(MovingImage, TweenInfo, {
		Position = UDim2.new(0, 0, -0.135, 0)
	})
	CreateTween(MovingImage, TweenInfo, {
		Position = UDim2.new(-0.075, 0, -0.25, 0)
	})
end

use my newer code instead of this

Try this? Curious to see if it works

local TweenService = game:GetService("TweenService")
local MovingImage = script.Parent
local TweenInfo = TweenInfo.new(1, Enum.EasingStyle.Linear)

function CreateTween(Object, Info, Properties)
	local Tween  = TweenService:Create(Object, Info, Properties)

	Tween:Play()
	Tween.Completed:Wait()
end

while true do
	CreateTween(MovingImage, TweenInfo, {
		Position = UDim2.new(0, 0, -0.135, 0)
	})

	MovingImage.Position = UDim2.new(-0.075, 0, -0.25, 0)
end

it works in studio and looks good, but in-game it still glitches a little bit

Possibly because you originally used -0.137 and somewhere it changed to -0.135. Changed it back to -0.137.

local TweenService = game:GetService("TweenService")
local MovingImage = script.Parent
local TweenInfo = TweenInfo.new(1, Enum.EasingStyle.Linear)

function CreateTween(Object, Info, Properties)
	local Tween  = TweenService:Create(Object, Info, Properties)

	Tween:Play()
	Tween.Completed:Wait()
end

while true do
	CreateTween(MovingImage, TweenInfo, {
		Position = UDim2.new(0, 0, -0.137, 0)
	})

	MovingImage.Position = UDim2.new(-0.075, 0, -0.25, 0)
end