Hello,
Recently, I have been working on a system which displays background images on my game: https://www.roblox.com/games/225780602/The-Arc
I wanted to create a system that changed between multiple pictures in a loop.
With this in mind, I created a gui: https://cdn.discordapp.com/attachments/356675974928007168/531941278330650634/unknown.png
I then added the script, with a few trial images:
local back1 = script.Parent local back2 = script.Parent.Parent:FindFirstChild("Background2") images = {2721116470, 2673015024, 1417408619, 1417402706} local one = 1 local two = 2 back1.Image = (images[one]) back2.Image = (images[two]) local function change1() local succ,err = pcall(function() if one > #images then one = 1 else back1.Image = "http://www.roblox.com/asset/?id=" .. (images[one]) end end) if err then change1() end end local function change2() local succ2,err2 = pcall(function() if two > #images then two = 2 else back2.Image = "http://www.roblox.com/asset/?id=" .. (images[two]) end end) if err2 then change2() end end while true do print("image 1 live") wait(5) for i= 1,10,1 do back1.ImageTransparency = back1.ImageTransparency + 0.1 back2.ImageTransparency = back2.ImageTransparency - 0.1 wait(0.1) end print("image 2 live") local one = one + 2 local two = two + 2 change1() wait(5) for i= 1,10,1 do back1.ImageTransparency = back1.ImageTransparency - 0.1 back2.ImageTransparency = back2.ImageTransparency + 0.1 wait(0.1) end local one = one + 2 local two = two + 2 change2() end
Even with trying to use a pcall to fix it. The trial images I selected won't appear correctly. Please play the game to see for yourself.
It would be much appreciated if you could share any suggestions you have to this problem.
Some of your image ids are incorrect. I first copy the id and put it into an image label, Studio will then format and get the correct image id for you. It saves a lot of time for me.
You can then remove the pcalls in the code. Loading a bad image will not stop the code from running.
You can also simplify your code a lot by only using one counter and swapping variables around making the visible image label fade out the next loop.
I would aslo recommend that you use the tween service as you can create a tween of both image labels and run them at the same time giving you the fade in/out effect you want.
Example(As EXpodo1234ALT said you should be using alocal script for client side gui things):-
local idList = { "2721116470", "2673015010", "1417408615", "1417402635" } local tweenServ = game:GetService("TweenService") local imageLabel1 = script.Parent.ImageLabel1 local imageLabel2 = script.Parent.ImageLabel2 -- tween infromation local fadeOut = {ImageTransparency = 1} local fadeIn = {ImageTransparency = 0} local tweenInfo = TweenInfo.new(4) local nextId = 1 local function nextIndex() -- manage counter function nextId = nextId + 1 if nextId > #idList then nextId = 1 end end local function mkTween(imageLabel, tbl) return tweenServ:Create(imageLabel, tweenInfo, tbl) end while true do nextIndex() -- inc counter imageLabel2.Image = "rbxassetid://" .. idList[nextId] local tween1, tween2 = mkTween(imageLabel1, fadeOut), mkTween(imageLabel2, fadeIn) tween1:Play() tween2:Play() tween2.Completed:Wait() wait(5) -- spaw variable around for next run local tmp = imageLabel1 imageLabel1 = imageLabel2 imageLabel2 = tmp end
I have also uploaded the model here.
Hope this help, comment if you would like me to explain more about the tween service.