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
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
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
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
if u want to strip spaces to count non spaces then change local Text = Message.Text → local Text = Message.Text:gsub(" ", "")
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