.OnIncomingMessage firing twice?

i have this script that fires commands, and it only runs when a message gets sent (via TextChatService | OnIncomingMessage)

but for some reason, the function gets called twice?

local tChatService = game:GetService("TextChatService")
local rStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")

-- Player variables;
local player = players.LocalPlayer

-- Path to events;
local eventsFolder = rStorage:WaitForChild("Events")
local fromClient = eventsFolder:WaitForChild("FromClient")
local commandEvents = fromClient:WaitForChild("Command")

-- Events;
local fireCommand = commandEvents:WaitForChild("FireCommand")

-- Functions;
local function OnIncomingMessage(message: TextChatMessage)
	if message.TextSource and message.TextSource.UserId ~= player.UserId then return end
	print(`uwu`)

	local text = message.Text
	if text:sub(1, 1) == "/" then
		fireCommand:FireServer(text)
		print(`fired remote lol`)
	end
end

-- Runtime;
tChatService.OnIncomingMessage = OnIncomingMessage

i am VERY confused, i don’t know what’s going on
i’m not seeing any sort of “double callbacking” in the documentations, so i’m just boldly going to assume that this is some sort of coding error

1 Like

It’s possible you accidentally have the script running in two places. Try printing the location of the script before firing, see if you get two places.

this is an instant no

this is the only place where a .OnIncomingMessage callback is located

1 Like

It could be fired twice, it cannot have been connected twice.

uh, what?

This is expected behaviour. You can print message.Status and will notice that it will be different both times. It’s just an internal system. Just use message.Status to ensure it fires once.

Essentially, just use this if statement:

if message.Status.Name ~= "Success" then return end

…oh wow

how exactly do you know this?
this isn’t written in the docs anywhere (unless i am extremely blind)

besides, what’s the point of message statuses? doesn’t OnIncomingMessage only run when the actual message is filtered?

Quoted from the docs:

“Use this to decorate TextChatMessages. If this callback returns a TextChatMessageProperties, those properties are merged with the TextChatMessage parameter to create a new TextChatMessage.”

By this logic, we understand that message.Status is important as it allows the developer to know if the message is going to be sent or if it’s already sent or has errored.

I searched about it a bit, noticed a topic mentioning it, tested it myself and that’s it.

1 Like

alright,

many thanks :steamhappy:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.