How would I simplify this image-changing script?

How would I simplify this image changing script?

local img0 = script.Parent:WaitForChild("Img0")
local img1 = script.Parent:WaitForChild("Img1")
local img2 = script.Parent:WaitForChild("Img2")
local img3 = script.Parent:WaitForChild("Img3")
local img4 = script.Parent:WaitForChild("Img4")
local img5 = script.Parent:WaitForChild("Img5")
local img6 = script.Parent:WaitForChild("Img6")
local img7 = script.Parent:WaitForChild("Img7")
local img8 = script.Parent:WaitForChild("Img8")
local img9 = script.Parent:WaitForChild("Img9")
local img10 = script.Parent:WaitForChild("Img10")
local img11 = script.Parent:WaitForChild("Img11")
local img12 = script.Parent:WaitForChild("Img12")
local img13 = script.Parent:WaitForChild("Img13")
local img14 = script.Parent:WaitForChild("Img14")
local img15 = script.Parent:WaitForChild("Img15")
local img16 = script.Parent:WaitForChild("Img16")
local img17 = script.Parent:WaitForChild("Img17")
local img18 = script.Parent:WaitForChild("Img18")
local img19 = script.Parent:WaitForChild("Img19")
local img20 = script.Parent:WaitForChild("Img20")
local img21 = script.Parent:WaitForChild("Img21")

local function loop()
	img0.Visible = true
	wait(0.02)
	img0.Visible = false
	img1.Visible = true
	wait(0.02)
	img1.Visible = false
	img2.Visible = true
	wait(0.02)
	img2.Visible = false
	img3.Visible = true
	wait(0.02)
	img3.Visible = false
	img4.Visible = true
	wait(0.02)
	img4.Visible = false
	img5.Visible = true
	wait(0.02)
	img5.Visible = false
	img6.Visible = true
	wait(0.02)
	img6.Visible = false
	img7.Visible = true
	wait(0.02)
	img7.Visible = false
	img8.Visible = true
	wait(0.02)
	img8.Visible = false
	img9.Visible = true
	wait(0.02)
	img9.Visible = false
	img10.Visible = true
	wait(0.02)
	img10.Visible = false
	img11.Visible = true
	wait(0.02)
	img11.Visible = false
	img12.Visible = true
	wait(0.02)
	img12.Visible = false
	img13.Visible = true
	wait(0.02)
	img13.Visible = false
	img14.Visible = true
	wait(0.02)
	img14.Visible = false
	img15.Visible = true
	wait(0.02)
	img15.Visible = false
	img16.Visible = true
	wait(0.02)
	img16.Visible = false
	img17.Visible = true
	wait(0.02)
	img17.Visible = false
	img18.Visible = true
	wait(0.02)
	img18.Visible = false
	img19.Visible = true
	wait(0.02)
	img19.Visible = false
	img20.Visible = true
	wait(0.02)
	img20.Visible = false
	img21.Visible = true
	wait(0.02)
	img21.Visible = false
	
	
	
	
	

end

while true do
	if script.Parent.Parent.Parent.Model.Flip18.Value == true then
		loop()
	else
		wait(0.02)
	end
end
1 Like

Perhaps order them in a table using something like:

local Images = {}
local Iteration = 0
local Previous = [randomly choose an image]
for i=1, 21 do
    Iteration += 1
    local Image = parent:FindFirstChild("Img"..Iteration)
    table.insert(Images, Iteration, Image)
end

Then loop the table.
It’s more just housekeeping though.

The ‘previous’ local could be updated for each image change, then hidden on the next one.

3 Likes

That script is so inconsistent

Use this if the children of the parent of the script are images only.

for _, image in pairs(script.Parent:GetChildren()) do
      image.Visible = true
      wait(0.02)
      image.Visible = false
end

If not then make a folder inside script.Parent named Images and put all ImageLabels in it and use this script

for _, image in pairs(script.Parent.Images:GetChildren()) do
      image.Visible = true
      wait(0.02)
      image.Visible = false
end

The loop here will just change pick out an image at random order. @Irideon 's solution would be a more reasonable solution. Although in @Irideon 's solution, the line Iteration += 1 will run first and change the 1 to 2; this also skips “Img0.” To add on, this would be a good solution:

local Children = script.Parent:GetChildren()

for i = 1, #Children, 1 do
    script.Parent["Img"..tostring(i - 1)].Visible = true
    wait(0.02)
    script.Parent["Img"..tostring(i - 1)].Visible = false
end

I checked out on that and you are wrong on that one. It will select the ImageLabel in the correct order. So my script is the simplest, smallest and the most efficient one. It depends on how the image labels are placed. If they are placed in a different order use this script -

for i = 0, #script.Parent:GetChildren() - 1 do
      script.Parent["Img"..tostring(i)].Visible = true
      wait(0.02)
      script.Parent["Img"..tostring(i)].Visible = false
end

@KaylaPls This script shall help you. The posts above me by different people also work perfectly.

First of all, I wasn’t wrong. The for loop doesn’t always get the contents in correct order. Second of all, I have no idea why you just rescripted my solution and posted it again.

  1. Yes true, but in that case the order of the ImageLabel also matters.
  2. Sorry about that, just changed a small thing in there

I’m not much of a scripter, but do you mind incorporating the value being ticked to true in there?

local function loop()
      for i = 0, #script.Parent:GetChildren() - 1 do
            script.Parent["Img"..tostring(i)].Visible = true
            wait(0.02)
            script.Parent["Img"..tostring(i)].Visible = false
      end
end
while true do
	if script.Parent.Parent.Parent.Model.Flip18.Value == true then
		loop()
	else
		wait(0.02)
	end
end

Is this what you meant to say?

If you mean to say that Visible is not being not ticked when using the script then either the wait is too short. Increase the wait to around 1 second and then see. Or you are looking inside the StartGui while in game, you shall look inside your player gui that is inside your Player in game.Players. Inside there, Visible is being ticked and unticked.

Yes, but an error came up. On line 3 & 5 it says ‘got frame instead’

Can you show the complete error so that i can figure out what is going wrong

1 Like

Can you also send a picture of the place where the script is located inside the Explorer. Just send a screenshot of the StarterGui in the explorer showing its components if you dont mind

Also change i = 1 to i = 0 in the for loop

image

The frame must be in StartGui to be visible. You cannot use Server scripts to change Screen gui objects.

It’s not a screengui, it’s on a part.

Remove the script from within the frame and put it outside AdornFrame.