Filter Roleplay Names

Hello Developers! Recently, I made a roleplay name system, and realised that that was the bad comment zone. You don’t wanna know how I know this had no filter …Jk, I spammed random letters. Back to the subject; does anyone know if they could edit this script to my needs. Thanks:

local serverstorage = game:GetService("ServerStorage")
local gui = serverstorage:WaitForChild("BillboardGui")

game.ReplicatedStorage.RolePlayEvent.OnServerEvent:Connect(function(player, text)
	local char = player.Character or player.CharacterAdded:Wait()
	local head = char.Head
	
	local clonegui = gui:Clone()
	clonegui.Name = "RolePlayName"
	clonegui.TextLabel.Text = text
	clonegui.Parent = head
	
	if head.RolePlayName and head.RolePlayName ~= clonegui then
		head.RolePlayName:Destroy()
	end
end)

Will I get banned if there is no filter?

Double Thanks!

Your game will be taken down if you don’t filter user input properly, this function should work

function filterString(player: Instance, str: string): any
    if typeof(str) == "string" then
        local filteredString
        local suc, err = pcall(function()
            filteredString = game:GetService("Chat"):FilterStringForBroadcast(str, player)
        end)
        if err then
            return nil
        end
        
        return filteredString
    end
end
1 Like

Thanks, but could you help me insert this into my script the right way.

Sure: (the filter doesn’t work in studio so it wont get filtered)

local serverstorage = game:GetService("ServerStorage")
local gui = serverstorage:WaitForChild("BillboardGui")

local function filterString(player: Instance, str: string)
    if typeof(str) == "string" then
        local filteredString
        local suc = pcall(function()
            filteredString = game:GetService("Chat"):FilterStringForBroadcast(str, player)
        end)
        if not suc then
            return nil
        end
        
        return filteredString
    end
end

game.ReplicatedStorage.RolePlayEvent.OnServerEvent:Connect(function(player, text)
	local char = player.Character or player.CharacterAdded:Wait()
	local head = char.Head

	local filteredString = filterString(player, text)

	if filteredString ~= nil then
		local clonegui = gui:Clone()
		clonegui.Name = "RolePlayName"
		clonegui.TextLabel.Text = filteredString
		clonegui.Parent = head

		if head.RolePlayName and head.RolePlayName ~= clonegui then
			head.RolePlayName:Destroy()
		end
	else
		-- failed to filter so nothing happens
	end
end)
1 Like

Nope. Really sorry but it’s not working for me. :cold_sweat:

Just tested it, this function will work in game but not in studio (i have no clue why studio doesn’t let the filter do it’s job)

1 Like

Oh ok, I will play as the game and get back to you :sweat_smile:

I know this doesn’t matter much, but you can make this into one line easily

return suc and filteredString

also I recommend making Variables full words because it helps readability

2 Likes

Thanks so much man! Don’t know what I’d do without your amazing scripting!