Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

GUI script that switches between images in order?

In short, what I'm trying to accomplish is:

  • Player presses the right arrow button
  • It switches to the next image in the list
  • Once at the last image it'll change back to the original image and repeats the process

Here's my current script. It'll switch to the first image but won't change to any other images after the first button press. Sorry if it's bad, this is my first time attempting to script a GUI.

script.Parent.MouseButton1Click:connect(function(player)
	script.Parent.Parent.ImageLabel.Image = "rbxassetid://32360394"
end)
	script.Parent.MouseButton1Click:connect(function(player)
	script.Parent.Parent.ImageLabel.Image = "rbxassetid://443148891"
end)
script.Parent.MouseButton1Click:connect(function(player)
	script.Parent.Parent.ImageLabel.Image = "rbxassetid://21824667"
end)
script.Parent.MouseButton1Click:connect(function(player)
	script.Parent.Parent.ImageLabel.Image = "rbxassetid://443149001"
end)

And sorry if this isn't a properly formatted question, this is my first time using this site.

1 answer

✓ Accepted answer

Hey AnomalousRobot, I hope your day is going well!

The script you provided is very incorrect, but it's alright. You're basically switching images all at once and we do not want to do that. Here's a script I wrote with comments. Hopefully, it helps you!

local Images = {

	"rbxassetid://32360394",
	"rbxassetid://443148891",
	"rbxassetid://21824667",
	"rbxassetid://443149001",

} --  First you would need to create a table that contains all your images ID.

local CurrentIndex = 1 -- This variable will give us the position/index of the currentImage.


script.Parent.MouseButton1Click:Connect(function() 

	CurrentIndex += 1 -- Everytime the mouse is click, we increase our position/index to go to the next image.

	if not Images[CurrentIndex] then -- Over here, I'm checking if the position we want in the table exist. If not, were going back to the first image.
		CurrentIndex = 1 -- As you can see, I set the position back to 1.
	end

	script.Parent.Parent.ImageLabel.Image = Images[CurrentIndex] -- I'm indexing the image using our position.

end)

Glad I could help!

This discussion is preserved as it was. New answers, comments, and votes are closed.