How to add a limit to how many messages can be seen in custom chat

hello i was wondering how i could add a limit to how many messages can be seen at a time like regular chat plz help idk how

1 Like

I’ll have to refer you to the About Section of this category. We can’t help you if you don’t follow this in at least one way or another.

1 Like

Idk what else to say i just want to know how to put some sort of limit to how many messages can be seen in a scrollingframe or a chat gui because idk how but heres a video of what im talking about and yes i searched all over the devforum and found nothing
first one is my chat gui it doesnt have a limit to how many messages u can put and i would really like to implement that
robloxapp-20220307-1631329.wmv (1.0 MB)
second one is normal roblox chat and it has a limit because at the end the last chat dissapears cuz it has a limit and i have no clue how to do that
robloxapp-20220307-1627392.wmv (942.9 KB)

i mean, send source code. No one knows how it’s used/

heres the script for my chat gui
Local Script

script:FindFirstAncestorOfClass("ScreenGui").Enabled = false
script:FindFirstAncestorOfClass("ScreenGui").Enabled = game:GetService("Chat"):CanUserChatAsync(game:GetService("Players").LocalPlayer.UserId)
wait()
-- Functions
function createInstance(ClassName, properties)
	local object = Instance.new(ClassName)
	for i,v in pairs(properties) do
		object[i] = v
	end
	return object
end
function CreateChatMessage(Message:string, NameColor:Color3, Name:string, TextColor:Color3, FontName:string, TextSize:number, ChannelFrame:Instance, Icon:boolean, PlayerMessage:boolean)
	local MessageFrame = Instance.new("Frame")
	local MessageText = Instance.new("TextLabel", MessageFrame)
	MessageFrame.BackgroundTransparency = 1
	MessageFrame.Size = UDim2.new(1, 0, 1, 0)
	MessageText.BackgroundTransparency = 1
	MessageText.Font = FontName
	MessageText.TextStrokeTransparency = 0.75
	MessageText.TextSize = TextSize
	MessageText.TextWrapped = true
	MessageText.TextColor3 = TextColor
	MessageText.Size = UDim2.new(1, -14, 1, 0)
	MessageText.Position = UDim2.new(0, 8, 0, 0)
	MessageText.TextXAlignment = Enum.TextXAlignment.Left
	MessageText.TextYAlignment = Enum.TextYAlignment.Top
	local NameColorR, NameColorG, NameColorB = math.round(NameColor.R*255), math.round(NameColor.G*255), math.round(NameColor.B*255)
	MessageFrame.Parent = ChannelFrame
	if PlayerMessage then
		local PlayerNameButton = createInstance("TextButton", {
			["TextColor3"] = NameColor;
			["TextSize"] = TextSize;
			["TextStrokeTransparency"] = 0.75;
			["Font"] = FontName;
			["TextXAlignment"] = Enum.TextXAlignment.Left;
			["TextYAlignment"] = Enum.TextYAlignment.Top;
			["BackgroundTransparency"] = 1;
			["Parent"] = MessageText
		})
		PlayerNameButton.Size = UDim2.new(0, PlayerNameButton.TextBounds.X, 0, PlayerNameButton.TextBounds.Y)
		if Icon then
			PlayerNameButton.Text = "   [" .. Name .. "]:"
		else
			PlayerNameButton.Text = "[" .. Name .. "]:   "
		end
		local spacing = ""
		for i = 1,(PlayerNameButton.TextBounds.X / 3) - 1 do
			spacing = spacing .. " "
		end
		MessageText.Text = spacing .. Message
	else
		MessageText.Text = Message
	end
	if Icon then
		local IconFrame = Instance.new("Frame", MessageFrame)
		IconFrame.Position = UDim2.new(0, 0, 0, 2)
		IconFrame.Size = UDim2.new(0, TextSize, 0, TextSize)
		IconFrame.BackgroundTransparency = 1
		local IconImage = Instance.new("ImageLabel", IconFrame)
		IconImage.AnchorPoint = Vector2.new(0.5, 0.5)
		IconImage.Position = UDim2.fromScale(0.5, 0.5)
		IconImage.Size = UDim2.fromOffset(14, 14)
		IconImage.BackgroundTransparency = 1
		IconImage.Image = "rbxasset://textures/ui/chat_teamButton@2x.png"
		IconImage.ImageColor3 = Color3.fromRGB(NameColorR, NameColorG, NameColorB)
		MessageText.Text = "   " .. MessageText.Text
	end
	MessageFrame.Size = UDim2.new(1, 0, 0, MessageText.TextBounds.Y)
	ChannelFrame.CanvasPosition = Vector2.new(0, ChannelFrame.AbsoluteCanvasSize.Y)
end
local function GetNameValue(pName)
	local value = 0
	for index = 1, #pName do
		local cValue = string.byte(string.sub(pName, index, index))
		local reverseIndex = #pName - index + 1
		if #pName%2 == 1 then
			reverseIndex = reverseIndex - 1
		end
		if reverseIndex%4 >= 2 then
			cValue = -cValue
		end
		value = value + cValue
	end
	return value
end
-- Objects
local ChatFrame = script.Parent.ChatFrame
local ChatBar = ChatFrame.ChatBar
local BoxFrame = ChatBar.BoxFrame
local ChatBoxContents = BoxFrame.ChatBoxContents
local ChatBox = ChatBoxContents.TextBox
local PlaceholderText = ChatBoxContents.Placeholder
local Channels = ChatFrame.Channels
local ChatLogDisplay = Channels.MessageLogDisplay
-- Variables
local Emotes = {
	"dance",
	"dance2",
	"dance3",
	"wave",
	"laugh",
	"point",
	"cheer"
}
local MutedPlayers = {}
local color_offset = 0
local NAME_COLORS =
	{
		Color3.new(253/255, 41/255, 67/255), -- BrickColor.new("Bright red").Color,
		Color3.new(1/255, 162/255, 255/255), -- BrickColor.new("Bright blue").Color,
		Color3.new(2/255, 184/255, 87/255), -- BrickColor.new("Earth green").Color,
		BrickColor.new("Bright violet").Color,
		BrickColor.new("Bright orange").Color,
		BrickColor.new("Bright yellow").Color,
		BrickColor.new("Light reddish violet").Color,
		BrickColor.new("Brick yellow").Color,
	}
-- ExtraFunctions
local function ComputeNameColor(pName)
	return NAME_COLORS[((GetNameValue(pName) + color_offset) % #NAME_COLORS) + 1]
end
-- Services
local UIS = game:GetService("UserInputService")
local TS = game:GetService("TextService")
-- Chat
game:GetService("StarterGui"):SetCoreGuiEnabled(Enum.CoreGuiType.Chat, false)
local chatEvent = Instance.new("BindableEvent")
game.StarterGui:SetCore("CoreGuiChatConnections", {ChatWindow = {MessagePosted = chatEvent}})
UIS.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		local keyPressed = input.KeyCode
		if keyPressed == Enum.KeyCode.Slash then
			wait()
			ChatBox:CaptureFocus()
		end
	end
end)
ChatBox.FocusLost:Connect(function(enterPressed)
	if enterPressed then
		chatEvent:Fire(ChatBox.Text)
		ChatBox.Text = ""
		PlaceholderText.Visible = true
	else
		if ChatBox.Text == "" then
			PlaceholderText.Visible = true
		end
	end
end)

ChatBox.Focused:Connect(function()
	PlaceholderText.Visible = false
end)
function plrChatted(plr, msg, filteredmsg)
	for _,mutedPlayer in pairs(MutedPlayers) do
		if plr == mutedPlayer then
			return
		end
	end
	if msg ~= "" then
		local split = string.split(msg, " ")
		if msg == "/clear" then
			if plr == game:GetService("Players").LocalPlayer then
				for i,v in pairs(ChatLogDisplay.Scroller:GetChildren()) do
					if v:IsA("GuiObject") then
						v:Destroy()
					end
				end
			end
		elseif split[1] == "/e" then
			if plr == game:GetService("Players").LocalPlayer then
				local emote = false
				for _,Emote in pairs(Emotes) do
					if split[2] == Emote then
						emote = true
						return
					end
				end
				if not emote then
					CreateChatMessage("You can't use that Emote.", Color3.fromRGB(0, 0, 0), "", Color3.fromRGB(245, 50, 50), "SourceSansBold", 18, ChatLogDisplay.Scroller, false, false)
				end
			end
		elseif split[1] == "/mute" then
			if plr == game:GetService("Players").LocalPlayer then
				if split[2] then
					local player = game:GetService("Players"):FindFirstChild(split[2])
					if player then
						table.insert(MutedPlayers, player)
					end
				end
			end
		elseif split[1] == "/unmute" then
			if plr == game:GetService("Players").LocalPlayer then
				if split[2] then
					local player = game:GetService("Players"):FindFirstChild(split[2])
					if player then
						for i,v in pairs(MutedPlayers) do
							if v == player then
								table.remove(MutedPlayers, i)
							end
						end
					end
				end
			end
		elseif split[1] == "/help" or split[1] == "/?" then
			if plr == game:GetService("Players").LocalPlayer then
				CreateChatMessage("These are the basic chat commands.", Color3.fromRGB(0, 0, 0), "", Color3.fromRGB(255, 255, 255), "SourceSansBold", 18, ChatLogDisplay.Scroller, false, false)
				CreateChatMessage("/mute <speaker> : mute a speaker.", Color3.fromRGB(0, 0, 0), "", Color3.fromRGB(255, 255, 255), "SourceSansBold", 18, ChatLogDisplay.Scroller, false, false)
				CreateChatMessage("/unmute <speaker> : unmute a speaker.", Color3.fromRGB(0, 0, 0), "", Color3.fromRGB(255, 255, 255), "SourceSansBold", 18, ChatLogDisplay.Scroller, false, false)
			end
		else
			CreateChatMessage(filteredmsg, ComputeNameColor(plr.Name), plr.Name, Color3.fromRGB(255, 255, 255), "SourceSansBold", 18, ChatLogDisplay.Scroller, true, true)
		end
	end
end
game:GetService("ReplicatedStorage").Chatted.OnClientEvent:Connect(function(plr, msg, filteredmsg)
	plrChatted(plr, msg, filteredmsg)
end)

CreateChatMessage("Chat '/?' or '/help' for a list of chat commands.", Color3.fromRGB(0, 0, 0), "", Color3.fromRGB(255, 255, 255), "SourceSansBold", 18, ChatLogDisplay.Scroller, false, false)

Server Script

local TS = game:GetService("TextService")

local function filterText(fromUserId, text)
	local textFilterResult = ""
	local success, errorMessage = pcall(function()
		textFilterResult = TS:FilterStringAsync(text, fromUserId)
	end)
	if not success then
		warn(errorMessage)
	else
		return textFilterResult:GetNonChatStringForBroadcastAsync()
	end
end

game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(msg)
		local filteredtext = filterText(plr.UserId, msg)
		game:GetService("ReplicatedStorage").Chatted:FireAllClients(plr, msg, filteredtext)
	end)
end)

I’m going to assume you have a cache of the message objects (guis)

if so, you can do something like this:

local Limit = 30 -- change this.
local Cache = table.create(Limit)

local function MessageReceived(player, msg)
  -- blah blah your code here
  -- assuming MessageFrame is the newly created frame holding the message (from a template or smth)
  if #Cache == Limit then
    local Temp = table.remove(Cache, 1)
    Temp:Destroy()
  end
  table.insert(Cache, MessageFrame)
end
-- ... etc

Just find a way to implement this.

Wow ok MessageFrame was in fact the name of it…

Thx so much it works perfectly, no more spam in the chat