Custom chat sends double messages

Sorry if things aren’t formatted properly, this is my first DevForum post, Hello!

I have this custom chat script that has a major issue with the code, when SOLO testing it in Studio/Client, it works perfectly fine, but when someone joins the game, every chat that is sent from thereon gets doubled. this adds on depending on the playercount in the server (for example, 20 people, 20 of the same chat from that player alone)

Here are 2 photos to demonstrate:
issue1
This is when i Solotest
issue2
This is in-game, with another player in the server.

Here is the script for analysis:

-- foods i love it (variables btw idk if u knew that)
replicatedstorage = game.ReplicatedStorage
sendmessage = replicatedstorage:WaitForChild("SendChatMessage")
chatframe = script.Parent.ChatFrame
chatbar = script.Parent.ChatBar.TextBox
messagetemplate = script.Message
chatbubbletemplate = script.ChatBubble

local CHAT_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,
	}


-- code

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

function ReceiveChatMessage(sender, text)
	local chatcolor = CHAT_COLORS[(GetNameValue(sender) % #CHAT_COLORS) + 1]

	local senderf = sender .. ": "
	local messageframe = messagetemplate:Clone()
	local messagetext = messageframe.MessageText
	local playername = messageframe.PlayerName

	messageframe.Visible = true
	messageframe.Fade.Disabled = false

	playername.Text = senderf

	messagetext.Text = senderf .. text
	playername.TextColor3 = chatcolor

	messageframe.Parent = chatframe

	for i,v in pairs(chatframe:GetChildren()) do
		v.Position = UDim2.new(0, 0, 1, v.Position.Y.Offset-messagetext.TextBounds.Y)
		if v.Position.Y.Offset < -209 then
			v:Destroy()
		end
	end

	local senderp = game.Players:FindFirstChild(sender)
	if senderp then
		if senderp.Character then
			if senderp.Character:FindFirstChild("Head") then
				local chatbubble = chatbubbletemplate:Clone()
				chatbubble.Enabled = true
				chatbubble.Parent = senderp.Character.Head
				chatbubble.TextLabel.Text = text
				wait(0)
				chatbubble.Body.Size = UDim2.new(0, chatbubble.TextLabel.TextBounds.X+25, 0, chatbubble.TextLabel.TextBounds.Y+25)
				game.Debris:addItem(chatbubble, 15)

				for i,b in pairs(senderp.Character.Head:GetChildren()) do
					if b.Name == "ChatBubble" then
						if b ~= chatbubble then
							local bbody = b.Body
							local btextlabel = b.TextLabel
							local btier = b.Tier
							local offset = UDim2.new(0, 0, 0, chatbubble.TextLabel.TextBounds.Y+25)

							bbody.Position = bbody.Position - offset
							btextlabel.Position = btextlabel.Position - offset
							btextlabel.TextTransparency = 0.5
							btier.Value = btier.Value + 1

							if btier.Value > 2 then
								b:Destroy()
							end
							if b:FindFirstChild("Tail") then
								b.Tail:Destroy()
							end
						end
					end
				end
			end
		end
	end
end

replicatedstorage.ReceiveChatMessage.OnClientEvent:Connect(ReceiveChatMessage)

function onKeyPress(inputObject, gameProcessedEvent)
	if not gameProcessedEvent then
		if inputObject.KeyCode == Enum.KeyCode.Slash then
			wait()
			chatbar:CaptureFocus()
		end
	end
end

game:GetService("UserInputService").InputBegan:connect(onKeyPress)

chatbar.FocusLost:Connect(function(send)
	if send then
		local text = chatbar.Text
		if chatbar.Text ~= "" then
			chatbar.Text = ""

			sendmessage:FireServer(text)
		end
	end
end)
1 Like

I would assume the issue occurs from the server script which receives “sendMessage”. Perhaps try looking there.