I am making a ROBLOX script to block messages which are toxic from getting to other people. For some reason, it isn't working and still allows the toxic messages to be seen by others. Any ideas on how to fix it?
I have tried a super basic filter, which just removes words it finds to be “bad” from a table, and that surprisingly works.
ONLY SCRIPT:
local TextChatService = game:GetService("TextChatService")
local Channels = TextChatService:WaitForChild("TextChannels")
local HttpService = game:GetService("HttpService")
local TextClassifier = "https://api.example.com" -- not actual thing, actual api returns json data
local MaxScore = 0.7
local function isMessageToxic(message: string, speaker: TextSource): boolean
if message == "" then return false end
pcall(function()
response = HttpService:PostAsync(
TextClassifier,
HttpService:JSONEncode({
text = message
}),
Enum.HttpContentType.ApplicationJson
)
response = HttpService:JSONDecode(response).response
end)
if response.Error or not response then
print("There has been an error.") -- no body or etc
return false
end
if response[1].score > (MaxScore or 0.7) then
print("Message has been flagged.")
return true
else
print("Message has passed.")
return false
end
end
local function channelAdded(channel: TextChannel)
if not channel:IsA("TextChannel") then return end
channel.ShouldDeliverCallback = function(message, textSource)
return not isMessageToxic(message.Text, textSource)
end
end
for _, channel in pairs(Channels:GetChildren()) do
channelAdded(channel)
end
Channels.ChildAdded:Connect(channelAdded)
What is sent to the API:
{
"text": "i hate you”
}
What it responds with:
{
"response": [{
"label": "NEGATIVE",
"score": 0.9993651509284973
}, {
"label": "POSITIVE",
"score": 0.0006348910392262042
}]
}
What is printed:
17:19:59 -- Message has been flagged.
17:19:59 -- Message has been flagged.
