How to make moving background scroll infinitely?

Hey devforum! I am trying to create a GUI background that is infinitely moving to the side. To do this, I am having an image scroll to the side until it has gone across the screen once, mind you the gui is a size of {2,0}{2,0}. When the image has gone across the screen it should teleport back to the starting point and keep going. For some reason it isn’t jumping back.

Here is my code:

while true do
	script.Parent.Position = script.Parent.Position - UDim2.new(0.01, 0, 0, 0)
	if script.Parent.Position == UDim2.new(-1, 0, -0.5, 0) then
		script.Parent.Position = UDim2.new(0,0,-0.5,0)
	end
	wait(0.05)
end

Here’s what happens.
notworkinggui
EDIT: The gif doesn’t really show how the gui just keeps going off the screen infinitely.

Does anyone know why this might be happening?

11 Likes

Okay, so, first you need to have an image with two repeated patterns so that when the edge of the image is reached, you can go back to the beginning without any noticeable difference.

Something like this:

4 Likes

Alright, but my problem is that the script is not moving the image back to the right side of the screen.

1 Like

Instead of using a while loop to change the position, use TweenPosition, which provides a lot more options and is much easier overall.

local image = script.Parent

local function whenTweenCompleted(state)

    if state == Enum.TweenStatus.Completed then --if the tween actually completed and not interrupted, but you don't need this if you set override to false like we did

        image.Position = UDim2.new(0, 0, -0.5, 0)
    
    end
end

image:TweenPosition(UDim2.new(-1, 0 -0.5, 0), "Out", "Linear", 0.5, false, whenTweenCompleted) --set the easing style and direction to what you like
20 Likes

Thanks for the help! I tried to use position just because in the beginning I thought it would be easier to loop than tweening the image. But tweening made the background move well, I even made it move diagonally.
workinggui

5 Likes

I just did it and my image goes live at an incredible speed it disappears

2 Likes

The 0.5 before “false” is the speed of the tween.

How did you manage to make it move diagonally? I have been trying to get mine to move that way but I’ve been unable to find the right sized image and positions to use.

3 Likes

how were you able to make it loop smoothly? When I try to loop it the thing stutters and doesn’t look good.

4 Likes

You can just do that with tween service by setting the target position to like the position but X and Y are -10 than the normal

1 Like

You need to take the image size into consideration, when the background moves by the size of that image you just end the tween and teleport the image back, otherwise the image will just teleport

2 Likes