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.
EDIT: The gif doesn’t really show how the gui just keeps going off the screen infinitely.
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.
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
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.
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.
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