How could I loop videos?

I easily made my own little code that loops a bunch of videos and then when the current video has been finished it gets cloned to the ReplicatedStorage and vice versa, to further expand on this, here is little code I made:


local Videos = game.ReplicatedStorage.Videos:GetChildren() -- table that contains all of the videos

local MainPart = game.Workspace.MainPart -- the part that will have the video showed on

local SelectedVid = 0 -- the selected video within the table

while true do

if SelectedVid == #Videos then -- if the selected vid has reached the length of the table then restart it, 
--meaning that if all of the videos in the table has been played then make it go back to the start

SelectedVid = 0

end

SelectedVid = SelectedVid + 1

local VideoName = Videos[SelectedVid].Name -- gets the video name from the table

game.ReplicatedStorage.Videos[VideoName].Parent = MainPart.SurfaceGui -- grabs the video from replicated storage and clone it to the part to play it on

local Vid = MainPart.SurfaceGui[VideoName] -- gets the current video

Vid:Play() -- Use the :Play() function to play the video

Vid.Ended:Wait(Vid.Video) -- waits until the current video has been ended with a parameter which accepts the video's id property

print("finished")

Vid.TimePosition = 0

Vid.Parent = game.ReplicatedStorage.Videos -- when finished clone it back to the replicated storage

Vid = nil -- set vid to nil so that it can iterate to the next video

wait(1)

end

If you are ever wondering why I didn’t destroy the video rather than cloning it back to RS, its because it sort of lagged dramatically and I like it this way anyway.

If this is too abstract for you then feel free to look for a simpler code.