Scrolling tile image stops when looping

I have a tile of images that scroll to the left using a script with RunService. This is everything that you need to know.
The issue has been labelled in the comments

local tileSize = math.round(screenGui.AbsoluteSize.Y / 1.9) -- size of the tile in pixels
tile.TileSize = UDim2.fromOffset(tileSize,tileSize) 

local gametime = 0
local initialPos = UDim2.new(1,0,0.5,0) -- the starting position of the gui

game:GetService("RunService").PreRender:Connect(function(dt)

	print(tile.Position.X.Offset)
	gametime += dt

	local distance = (gametime * screenGui.AbsoluteSize.Y) / 8 
-- this is how the scrolling works
	local thing = screenGui.AbsoluteSize.Y / 3
	tile.Position = initialPos + UDim2.fromOffset(distance, 0)
	
-- this is the bugged part. for some reason when a full tile has been passed
-- the entire thing is forced back in that position and it cant move anymore
	if tile.Position.X.Offset > tileSize then 
		tile.Position = initialPos
	end
	
end)

Thanks in advance to anyone who can propose an answer!

fixed the title someone pls help

Im not really sure what the problem is

I first noticed that you create a variable called thing but never used it. did you mean to use it somwehere but forgot?

-- this is the bugged part. for some reason when a full tile has been passed
-- the entire thing is forced back in that position and it cant move anymore
	if tile.Position.X.Offset > tileSize then 
		tile.Position = initialPos
	end

Yes. This code snippet is at the end of the function which means that it will always run last. Earlier in the function you already set the tile.Position.X.Offset. In this case that happens to be above the trigger point for the if statement every time, hence why it goes back to the starting position.

As i said im not sure what the problem is, or what you’re trying to achieve. A more detailed explanation would definetly help.

yeah, the thing variable is unrelated.

at the start of the PreRender event, the gametime is a number showing the time since the game started. The time passed determines the position of the tile image. the speed is determined by the vertical resolution of the player’s monitor.
when the tile (the image texture) moves the distance of a full tile, the position is forced back to the start to continue the loop.

for some reason, the bug is that the if statement responsible for looping the position continuously fires after the first loop, keeping the image still. here is what it looks in-game:

the print statement is from this line:

print(tile.Position.X.Offset)

Oh i think i get what you want to do now.

In the if statement, Instead of setting the tile.Position to initialPos you should set gametime to 0:

-- this is the bugged part. for some reason when a full tile has been passed
-- the entire thing is forced back in that position and it cant move anymore
	if tile.Position.X.Offset > tileSize then 
		gametime = 0
	end

but this can cause slight jitters due to the way you are using gametime to position the ui.

Incase there is jitter you would need to instead subtract the max gametime from gametime:

-- this is the bugged part. for some reason when a full tile has been passed
-- the entire thing is forced back in that position and it cant move anymore
	if tile.Position.X.Offset > tileSize then 
		gametime -= <InsertMaxGameTimeHere>
	end

How you would calculate the max gametime im not exactly sure. but it should be set to the number gametime is when it has done a full loop of the ui element

1 Like

Hey! Looks like the issue is that the tile’s position gets reset when it goes beyond the tile size, but the reset doesn’t account for the previous offset. I added a fix to properly reset the position and make the tile loop correctly after it scrolls past its size. Hope this works for you!

local tileSize = math.round(screenGui.AbsoluteSize.Y / 1.9) 
tile.TileSize = UDim2.fromOffset(tileSize, tileSize)

local gametime = 0
local initialPos = UDim2.new(1,0,0.5,0) 

game:GetService("RunService").PreRender:Connect(function(dt)
    print(tile.Position.X.Offset)
    gametime += dt

    local distance = (gametime * screenGui.AbsoluteSize.Y) / 8

    local height = screenGui.AbsoluteSize.Y / 3
    tile.Position = initialPos + UDim2.fromOffset(distance, 0)

    if tile.Position.X.Offset > tileSize then
        tile.Position = initialPos + UDim2.fromOffset(distance - tileSize, 0) 
    end
end)

when i tried this, the position didn’t change and the whole image just slid off the screen. Do you think you can make it loop so that doesn’t happen?

1 Like

I see the issue now. The tile is just sliding off the screen instead of looping. Try this: instead of resetting the position directly, we can adjust the position smoothly by subtracting the tileSize from the distance. This should make the tile loop back to the starting position without sliding off the screen. Let me know if that works for you!

if tile.Position.X.Offset > tileSize then
    tile.Position = initialPos + UDim2.fromOffset(distance - tileSize, 0)
end

I dont think this will work either as gametime keeps increasing indefinetly, moving the ui more and more.

1 Like

Yeah, you’re right — since gametime keeps increasing, the UI will keep moving further. I think the proper fix would be to reset or wrap gametime itself once it goes past the full loop distance (like tileSize). That way the distance calculation never grows indefinitely, and the looping stays smooth.

For clairity, this could also be drastically simplified using TweenService instead of a function dependant on RunService.

Create a tween that moves the image to the distance you want, and set the repeat count to -1 (to repeat indefinitely) and play the tween. It would behave the same way as this code but be condensed to a single line, the tween can also be canceled at any time.

i would have done that but i also have some other animations dependant on runservice that i want to add

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.