I have tried out the second example in Text and Chat Filtering, except instead of a SurfaceGui, I tried using it with a BillboardGui.
However, as seen in the image above, there are three errors.
Here’s the script that contains the code:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Pet = game.Workspace.Pet
local PetBillboardGui = Pet.BillboardGui
local TextLabel = PetBillboardGui.TextLabel
local SetPetText = ReplicatedStorage.SetPetText
local function getTextObject(message, fromPlayerId)
local textObject
local success, errorMessage = pcall(function()
textObject = TextService:FilterStringAsync(message, fromPlayerId)
end)
if success then
return textObject
elseif errorMessage then
print("Error generating textObject")
end
return false
end
local function getFilteredMessage(textObject)
local filteredMessage
local success, errorMessage = pcall(function()
filteredMessage = textObject:GetNonChatStringForBroadcastAsync()
end)
if success then
return filteredMessage
elseif errorMessage then
print("Error filtering message", errorMessage)
end
return false
end
local function onSetPetText(player, text)
if text ~= "" then
local messageObject = getTextObject(text, player.UserId)
local filteredText = ""
local success, errorMessage = pcall(function()
filteredText = getFilteredMessage(messageObject)
end)
if success then
TextLabel.Text = filteredText
elseif errorMessage then
print("Error Filtering message for broadcast")
end
end
end
SetPetText.OnServerEvent:Connect(onSetPetText)
I think you are just wrapping your functions in too many pcalls, just use it in the main function that’s supposed to do everything, onSetPetText. Otherwise, you will have a very hard time handling errors:
local function getTextObject(message, fromPlayerId)
return TextService:FilterStringAsync(message, fromPlayerId)
end
local function getFilteredMessage(textObject)
return textObject:GetNonChatStringForBroadcastAsync()
end
local function onSetPetText(player, text)
if text ~= "" then
local messageObject
local success1, errorMessage1 = pcall(function()
messageObject = getTextObject(text, player.UserId)
end)
if success1 then
local filteredText = ""
local success2, errorMessage2 = pcall(function()
filteredText = getFilteredMessage(messageObject)
end)
if success2 then
TextLabel.Text = filteredText
else
print("Error filtering message:", errorMessage2)
end
else
print("Error getting text object:", errorMessage1)
end
end
end
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Player = Players.LocalPlayer
local PlayerGui = Player.PlayerGui
local Screen = PlayerGui:WaitForChild("PetNameScreen")
local Frame = Screen:WaitForChild("Frame")
local NameBox = Frame.Name
local SendButton = Frame.Send
local SetPetText = ReplicatedStorage:WaitForChild("SetPetText")
local function onClick()
local message = NameBox.Text
if message ~= "" then
SetPetText:FireServer(message)
Frame.Visible = false
end
end
SendButton.MouseButton1Click:Connect(onClick)
Could you try renaming the NameBox's name to something like just NameBox? It may be that the Name property of the frame is overwriting access to the name box, and just returning a string.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Player = Players.LocalPlayer
local PlayerGui = Player.PlayerGui
local Screen = workspace.Pet.BillboardGui
local Screen2 = PlayerGui:WaitForChild("PetNameScreen")
local Frame = Screen2:WaitForChild("Frame")
local NameBox = Frame.Name
local SendButton = Frame.Send
local SetPetText = ReplicatedStorage:WaitForChild("SetPetText")
local function onClick()
local message = NameBox.Text
if message ~= "" then
SetPetText:FireServer(message)
Frame.Visible = false
end
end
SendButton.MouseButton1Click:Connect(onClick)
local Screen = workspace.Pet.BillboardGui
local Frame = Screen:WaitForChild("Frame")
This should work if I’m not wrong.
The reason it was wrong before this (I think) is because you are referencing the playergui object, not the billboardgui. Hence what you got was “” - an empty string.