Three errors while attempting to filter text?


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)

You got that error because getFilteredText function returned false. Where you attempt to índex the boolean I can’t seem to find where you do this.

1 Like

Well, the last two error messages are from the fact you return false when it fails to find a textObject.

Why it fails to get the object is what you’re interested in. For this you should update the print to

print("Error generating textObject")
print(errorMessage)
1 Like

This creates a new error:

Argument 1 missing or nil

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
2 Likes

Could we see the localscript firing the remotefunction “SetPetText”? The server isn’t getting the text for some reason.

2 Likes
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.

Change the screen to the billboard location (i.e. workspace.PartThatContainsBillboard.Billboard)

2 Likes

Just did that.
Current code:

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.

image
But my BillboardGui doesn’t have a frame…

Quick question, what does your playergui contain?

image
Note that “Name” is a TextBox, while “Send” is a TextButton.

Ah. I’ve misunderstood your case. Sorry about that.

Well. I guess this is probably what goldenstein64 mentioned about properties and names.

Perhaps change the name of Name to something like NewName in both the playergui and localscript?

3 Likes

That works! Thanks for the solution!

2 Likes