As the title explains, my Chat Gui is not working as supposed to, but instead, it's doing something else; It's getting the names mixed up, as it keeps changing their names to Chat1, Chat2, Chat4, Chat12, Chat7, ect., and not removing old Chats. Sadly, I can not provide any Output, as there is nothing showing up. Here is the code I am using:
repeat wait() until game.Players.LocalPlayer and script.Parent:FindFirstChild("Displayer")
local Displayer = script.Parent.Displayer;
local funcs = {};
local MaxChats = 8;
function onChat()
local num = 0; local num2 = 0; --This is used to help keep track of all the Chat's Names, and Gui positioning locations
for i = 1, #funcs do --This is used when a Player leaves, joins, or if the code just executed
funcs[i]:disconnect() --It will then proceed to disconnect the current functions it has in table 'funcs'
table.remove(funcs,i) --It will then remove it from the table 'funcs'
end
for i,plr in pairs(game.Players:GetPlayers()) do --This will loop through the Player's
local chatfunc = plr.Chatted:connect(function(msg) --This will connect the Player's Chat to the 'Gui'
for i2,v2 in pairs(script.Parent:GetChildren()) do --This is where the names are getting mixed up
if v2 ~= script and v2 ~= Displayer then --This will check if the Child is not the script itself, now the 'Displayer'
for x = 1, #v2.Name do --This is used to loop through the string of the Child's Name
if tonumber(v2.Name:sub(x,x)) then --This will check if the current character is a Number value
if tonumber(v2.Name:sub(x,x)) > MaxChats then --If so, it will check if the Number Value is over 'MaxChats', or '8', but sadly, it isn't
v2:Destroy() --It will destroy it
num = num - 1 --Minus 'num' by '1'
num2 = num2 - 1 --Minus 'num2' by '1'
end
end
end
local orig = v2.Name --This is used to save it's original name
v2.Name = orig:sub(1,4)..tonumber(orig:sub(5)) + 1 --This is used to change the Chat's Name; but sadly it just gets them all mixed up, and does not remove at that point
end
end
local Chat = Displayer:Clone() --This creates a new 'Gui' to displayer the Player's Chat
Chat.Parent = Displayer.Parent
Chat.Position = UDim2.new(0.2,0,0.04*num2,0)
Chat.Size = UDim2.new(0.4,0,0.04,0)
Chat.Name = "Chat"..num
Chat.Text = game.Players.LocalPlayer.Name..": "..msg
num = num + 1
num2 = num2 + 1
end)
table.insert(funcs,chatfunc)
end
end
game.Players.PlayerAdded:connect(onChat)
game.Players.PlayerRemoving:connect(onChat)
onChat() --
If I still did not correctly tad, just let me know.

2 comments
Please tab your code correctly, it's hard to read.
Oh, sorry, I forgot about that. I shortened some of the lines to reduce code space, I'll tab them right now.