Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Textlabel not censoring words when typed?

game.Players.LocalPlayer.Chatted:Connect(function(message)
	game.Players.LocalPlayer.PlayerGui.Text.TextLabel.Text = message
	print(message)
end)

Whenever I type a curse word, it shows. How would I fix so it becomes tags instead?

2 comments

0

What happens is you did not make a filtering function

0

you can use FilterStringAsync or FilterStringForBrodcast

1 answer

✓ Accepted answer

GENERAL PRACTICE

Use :GetService() to retrieve the Players Service of the DataModel (game)

Use :WaitForChild() to make sure an item exists before changing / using it

ISSUES

In order to changing certain string values, you can use the function string.gsub() which replaces a found pattern in a string with a pre-set string. Since .Chatted gives you what the player's message was (sans hashtags) then in order to implement them, you first need a table of strings for any words you want to censor. Then you can iterate over this table when a player chats and check the message for the censored words, where they will then be replaced by your chosen string (in this case, a hashtag) If you change the string everytime you go through a word in the table, it will save your changes in case someone uses multiple censored words in the same line. After the table has completed its changes, set the label's text to the new message and the censored words will have been replaced.

REVISED LOCAL SCRIPT

local plr = game:GetService("Players").LocalPlayer
local label = plr:WaitForChild("PlayerGui"):WaitForChild("Text"):WaitForChild("TextLabel")
local array = {"word"}

plr.Chatted:Connect(function(message)
	for i, v in pairs(array) do
		local censor = string.gsub(message, v, "####")
		message = censor
	end
	label.Text = message
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.