[Already Index an error] When player joins it works, but it not removes when player lefts

game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, false)
local M = Instance.new("StringValue")
local Player = game.Players.LocalPlayer
local locked = true
while true do
	local o = game.Players:GetChildren()
 
		
	for S = 1,#o do
		game.ReplicatedStorage.Players:ClearAllChildren()
	
		wait(0.0001)
		local TextButton = game.ReplicatedStorage.TextButton:Clone()
		local Am = M:Clone()
		Am.Name = o[S].Name
	
		Am.Parent = game.ReplicatedStorage.Players
			if locked == false then
		
		for i,v in pairs(o[S].PlayerGui.ScreenGui:GetChildren()) do
			if v[i] == nil then
				else

				if game.Players:FindFirstChild(v[i].Name) then
					
				else
				o[S].PlayerGui.ScreenGui:GetChildren(v[i].Name):Destroy()
				end 
				end
				end
		end
		
		if Player:WaitForChild("PlayerGui").ScreenGui.ScrollingFrame:FindFirstChild(o[S].Name) then
			
				
			

			else


			local TextButton = game.ReplicatedStorage.TextButton:Clone()
				locked = true
		TextButton.Parent = Player:WaitForChild("PlayerGui").ScreenGui.ScrollingFrame
		TextButton.Text = o[S].Name
			TextButton.Name = o[S].Name
			
		end
		
	end
   
end

What is the exact problem here?

for i,v in pairs(o[S].PlayerGui.ScreenGui:GetChildren()) do
like these things

Can you add me at Discord and I explain u there? OfficialSpy#0001 , for me its hard to explain over DevForum^^
but I will send end solution here after we talked on Discord.

Wait i think i index an error,

locked = true

You should use Renderstepped
aka
local RS = game:GetService(“RunService”)

RS.RenderStepped.connect(function()


local o = game.Players:GetChildren()
 
		
	for S = 1,#o do
		game.ReplicatedStorage.Players:ClearAllChildren()
	
		wait(0.0001)
		local TextButton = game.ReplicatedStorage.TextButton:Clone()
		local Am = M:Clone()
		Am.Name = o[S].Name
	
		Am.Parent = game.ReplicatedStorage.Players
			if locked == false then
		
		for i,v in pairs(o[S].PlayerGui.ScreenGui:GetChildren()) do
			if v[i] == nil then
				else

				if game.Players:FindFirstChild(v[i].Name) then
					
				else
				o[S].PlayerGui.ScreenGui:GetChildren(v[i].Name):Destroy()
				end 
				end
				end
		end
		
		if Player:WaitForChild("PlayerGui").ScreenGui.ScrollingFrame:FindFirstChild(o[S].Name) then
			
				
			

			else


			local TextButton = game.ReplicatedStorage.TextButton:Clone()
				locked = true
		TextButton.Parent = Player:WaitForChild("PlayerGui").ScreenGui.ScrollingFrame
		TextButton.Text = o[S].Name
			TextButton.Name = o[S].Name
			
		end
		
	end
locked=false
 end)