TextChatService returning "Instance"

Hello developers, I need some help splitting a string using TextChatService. The current issue I am having is when using string.split() to split a message TextChatService is returning a Instance instead and I can’t figure out how to get around it! If anyone could help me would be greatly appreciated!

local function CheckWord(String, player)
	for _, v in ipairs(string.split(String, "")) do
		Characters += 1
		if v ~= v:lower() then 
			UppercaseCharacters += 1
		end
	end
	local Percentage = math.floor((UppercaseCharacters)/(Characters) * 100)
	if Percentage >= MinPercentage then
		-- do something here
	end
end

TextChatService.OnIncomingMessage = function(message)
	CheckWord(message, Player)
end

What outputs in console

message is actually a TextChatMessage. try string.split(String.Text, "")

Doesn’t seem to work either, as it still still the same thing as doing message.Text

try this:

function CheckWord(String)
    if String then
    local text = tostring(String.Text)
    Characters = text:len()
	for i = 1, Characters do
		if text:sub(i, i) ~= text:sub(i, i):lower() then
			UppercaseCharacters += 1
		end
	end
	local Percentage = math.floor((UppercaseCharacters)/(Characters) * 100)
	  if Percentage >= MinPercentage then
		-- do something here
  	  end
   else
      return
   end
end

TextChatService.OnIncomingMessage = CheckWord
2 Likes

Even then nothing happens when using a print statement message goes through using the other method but nothing happens as the message isn’t being detected

my bad, that was me changing the last few lines

try now

1 Like
local TextChatService = game:GetService("TextChatService")
local Player = game:GetService("Players").LocalPlayer

local MinPercentage = 50

local function IsUppercase(s: string): boolean
	local v = s:byte()
	return v >= 65 and v <= 90
end

local function CheckWord(Message: TextChatMessage)
	if Message.Status ~= Enum.TextChatMessageStatus.Success then return end

	local Text = Message.Text
	local Characters, UppercaseCharacters = #Text, 0
	
	--[[
		faster way to check for uppercase characters
		previous method of ipairs + split uses ipairs AND creates a new table which is slow
		:gsub iterates through each character and looks at their byte value to really check if its an uppercase letter
	]]
	Text:gsub(".", function(c)
		if not IsUppercase(c) then return end
		UppercaseCharacters += 1
	end)
	local Percentage = math.floor((UppercaseCharacters / Characters) * 100)
	print(("%d%% of [%s] contains uppercase letters"):format(Percentage, Text))
	if Percentage >= MinPercentage then
		print("hokey pokey")
	end
end

TextChatService.OnIncomingMessage = CheckWord

image


if u want to strip spaces to count non spaces then change
local Text = Message.Textlocal Text = Message.Text:gsub(" ", "")

So can you explain how you got passed the “Instance” that TextChatService was returning? And thank you for the help!

yes of course anytime

TextChatService.OnIncomingMessage = CheckWord

… is the equivalent of

TextChatService.OnIncomingMessage = function(TextChatMessage)
	CheckWord(TextChatMessage)
end

the .OnIncomingMessage callback passes a TextChatMessage object (documentation here)
in the script, it’s hooked to the CheckWord function which uses the TextChatMessage passed by .OnIncomingMessage

1 Like

I Understand a bit more now and hopefully can use this information to move forward! :raised_hands:

1 Like

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