How to make a repeating "animated" background image, with GUI Elements

How to make a repeating “animated” background image, with GUI Elements


Heyo!, My Name is StarJ3M, and today I will be teaching you how to make an animated background, using tweening and run service! ##### wow, I haven't made a tutorial in a while huh. Last one was made in 2022 :woozy_face:

Its actually quite simple to make when you get the gist of it. And I will be also explaining how it works later.

Here’s what your finishing product should look like.


Part 1 - Grab That Design!

Ok so, to start off, first you either want to grab a simple repeating design, like the classic white and black checkerboard or mosaic tiles. You may want to get this image from the internet in high quality if you can’t find one or make one. You can also get something like a repeating flower design, BUT, know that to make this illusion of repeating images, you may want to make sure that the shapes or images in your background image is repeating, and matches up with the last one.

Here’s what I mean if you don’t understand this.

Bad:


Correct

If you can’t tell what the image is, in the first image, the image that I used has a design that doesn’t exactly repeat on the left and right side of its edges, therefore we have a part that doesn’t look right in the center of the two Image Labels.

When you’ve got this part down, now you can come down below this bar and check out what’s going on in the coding department!


Now that you’ve got your image, create an Image Label and name it Background1 or something else that is rememberable. Make the label’s X and Y scale {1, 0, 1, 0} ({xScale, xOffsett, yScale, yOffset}), make sure the offset is always zero, because if not then it won’t scale for all devices.

Set the Image property of Background1 to your desired image, NOTE: This will NOT work if your doing this in an unpublished place. Then duplicate the image and name it something like Background2.

Ok, now that you’ve got that down, its time to actually make the Illusion affect begin its course!


Part 2 - Putting The Image In The Game

Now what you want to do is set Background1's position to {0, 0, 0, 0}, having no offset or scale on any side of the axis. And you want to set Background2's position to {-1, 0, 0, 0}. An explanation for why we do all of this and why this works will be explained later at the end of the post, so keep on looking so that you can get more info on this!

Now, its finally time for the coding part of this, I know you were waiting for this, and yes, I’m not joking this time, I’m sorry about earlier, I know you were excited but, it was important ok?!


Part 3 - Its Scripting Time

Now we can finally script the code! It’s been a bit. If you haven’t already, add a local script to (I recommend) Background1, rename it to whatever you like. Now insert code that looks like this so that we can get the “Illusion working”. An explanation of what the code does is answered in the code, so make sure to analyze the data first, and then read the code.

local Background1 = script.Parent -- Make the first background image a variable that we can call
local Background2 = Background1.Parent:WaitForChild("Background2") -- Make the second background image a variable that we can call

game:GetService("RunService").RenderStepped:Connect(function() -- Call for RunService

	Background1.Position += UDim2.new(0.0005, 0, 0, 0) -- Update the Positions : NOTE - You can change the xScale if you want to make the ImageLabel's position move faster.
	Background2.Position += UDim2.new(0.0005, 0, 0, 0) -- Update the Positions

	if Background1.Position.X.Scale >= 1 then -- Check if the Background Position Scales X axis changed and if it has reached the scale of 1
		Background1.Position = UDim2.new(-1, 0, 0, 0) -- Reset's the position back to the start, with the second image now being around the {0, 0, 0, 0} mark for its position and taking the first images place.
	elseif Background2.Position.X.Scale >= 1 then -- Same thing but just checks for the second image.
		Background2.Position = UDim2.new(-1, 0, 0, 0) -- Also does the same thing, but now the first background image takes the crown back and is now filling in for the second background image
	end -- The background images switching places like this makes it so that one of these background images are always filling in a place for the other.
end) 

You can also used what @savio14562 said in the comments, if you want to make your code look nicer.


Ok well, now you’re done, that’s all you have to do. When your playtest your game, everything should be working smoothly. Look down below if you want a clear explanation on how this illusion works, and also for troubleshooting. If you have a question that’s not already answered on the troubleshooting tab, please reply to this post, and I can help you out.


Troubleshooting and Explanation

Q : My Image Label’s Are Overlapping Eachother

If this is happening to you, you may want to fix the positions of your two Image Label’s, because if they aren’t noted down accordingly, it can cause the image label’s to overlap, or make it so that the workspace and player can be visible.

Example:

Explanation on the illusion

I’m not sure if this particularly has a term or name, but for right now, its being called an illusion. This is an illusion because, even though it looks like were making infinite amounts of Image Labels, in reality, were actually just using two and manipulating their positions to our favor so that we can make this “infinity affect”.

And also, the reason why were setting the positions to -1 and 0is because of the way that they fit on screen. As seen in the image below, whenever we change the positions of the image label that cover the entire screen, they go back in between two points that resemble the first two points on the left side of the center.

If that isn’t very clear (which it probably isn’t), then analyze this images, because when you analyze them, you will then understand how and why the sizes of the images matter, and how they’re positions corelate to their size.



Thats all! Thanks for viewing my tutorial.

I will not be revamping this post until the 25th because I don’t currently have the time to fix any errors I made here, maybe small grammatical error’s, but not much other than that.

I will be trying my best to make fixes on this post, and reply to those who need help.

Update Times:

Adding How To Make Color Change - Adding How To Change Its Position From The Top To Bottom
2023-02-19T05:00:00Z2023-02-22T05:00:00Z

Other Tutorials:

Roblox How to | How to create a simple Player Life System - Resources / Community Tutorials - DevForum | Roblox

Guide To Making a Good Roblox Game - Resources / Community Tutorials - DevForum | Roblox


Credit Given to User @LeqenDzTR for helping me out with this, without him, I would not know how to do this.

46 Likes

Never knew how people created these, amazing tutorial!

4 Likes

Same, I didn’t realize it would be this easy to make either. But when you learn the illusion, it makes things way easier.

1 Like

I know right, I thought you would need to wait till roblox adds videos (2 year later…)

Also amazing job on tutorial layout not many tutorials are as clean and easy to read as this one. This is going to help so much.

2 Likes

I think I should edit a few bits (Mostly Images) and make them a bit better, but other than that, I also think it looks pretty easy to read.

1 Like

You can use the % operator for slightly better code but nice tutorial.
Example:

firstimage.Position = UDim2.fromScale(imagePos % 1,0)
-- you can increase imagePos by whatever and it will always repeat. 
-- if it is 1.25, position will be .25 
-- if it is 1, position should be either 1 or 0
2 Likes

May I ask what the % operator does? I still have absolutely no idea how it works.

It’s the modulus operator – it gives you the remainder of the division.

print(60 % 60) -- prints 0
print(61 % 60) -- prints 1
print(147 % 60) -- prints 27

(Hope this helps)

4 Likes

Ok, I understand now, but what makes this code better? Is it better in terms of optimization, cleaner code, or both?

1 Like

asasdasdasdasdasdasd – CHAR LIMIT

3 Likes

It removes the need of the if statement.

2 Likes

wasn’t this supposed to use RenderStepped?

1 Like

It most likely should, my knowledge on Render Stepped, Stepped, And Heartbeat is very slim, so as this post said, I simply just used heartbeat.

Confused? (A guide)

Part 1:
Make sure your ImageLabel’s ScaleType is set to “Crop” to have your image in its original proportions.
image
By X and Y scale, OP means size - if you want to script this post (loading screen, etc etc) use Background1.Size = Udim2.new(1, 0, 1, 0), etcetera.
image

Part 2:
There is enough clarification in the post.

Part 3:
Read the post and use the script provided, and read troubleshooting if you have any other issues.

This helped a bit for me, so I hope it also helps whoever needs just a bit of clarification.

1 Like