Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Struggling to add text filtration to a player self-naming button in Roblox Studio?

Asked by 1 year ago

So recently I have added a feature to my Roblox game that allows the plaver to type in a name for themselves, and have it appear overhead.

For some context: I do not adequatelv know Lua well enough, I can read it over and know roughly what something does, but I cannot write it. I added this feature pre-made from someone on youtube, but annovinalv the didn't add text filtration - so now players could literally swear in their nametags if they wanted to, meaning my game could be subject to moderator action.

I do have someone that's tried to helo me. but we're still running into some problems and I will likely need to un- privatise this game very soon as it is a virtual event venue. Thev have provided me with code which I have added (To filter player's inputted text if it is bad through roblox's own system), but it keeps failing to work and is now outputting no text at all regardless of whether it is permissible or not.

Here is the code I now have. If anyone could help me and tell me what I need to change to make this work i'd be so grateful!


local NametagEvent = game:GetService("ReplicatedStorage").NameTag
local Nametag = script.TextName

game.Players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(Char)
        local TagClone = Nametag:Clone()
        if Char:FindFirstChild("Head") then
            TagClone.Parent = Char.Head
        end
    end)

end)

local NameTagEvent = game:GetService("ReplicatedStorage").NameTag
local TextService = game:GetService("TextService")

local function filterMessage(msg,fromUser)
    local result
    local success, err = pcall (function()
        result = TextService:FilterStringAsync(msg, fromUser)
    end)
    if  success then
        result:GetNonChatStringForBroadcastAsync()
    end
end

NameTagEvent.OnServerEvent:Connect(function(plr,text)
    print(plr,"wrote",text)
    plr.Character.Head.TextName.TextLabel.Text = filterMessage(text,plr)
end)

1 answer

Log in to vote
0
Answered by
imKirda 4491 Moderation Voter Community Moderator
1 year ago
Edited 1 year ago

You should add a return

local function filterMessage(msg,fromUser)
    local result
    local success, err = pcall (function()
        result = TextService:FilterStringAsync(msg, fromUser)
    end)
    if  success then
        return result:GetNonChatStringForBroadcastAsync()
    end
end

When vou write

plr.Character.Head.TextName.TextLabel.Text = filterMessage(text,plr)

It replaces filterMessage(...) with what the function returned using return, however, without return, it will be replaced bv nothing, in the end the code is equal to:

plr.Character.Head.TextName.TextLabel.Text = nil

One more thing vou should add is else in case success is not so successfull, that is if the filter fails for some reason:

local function filterMessage(msg,fromUser)
    local result
    local success, err = pcall (function()
        result = TextService:FilterStringAsync(msg, fromUser)
    end)
    if  success then
        return result:GetNonChatStringForBroadcastAsync()
    else
        warn(err)
        return "[ filter failed ): ]"
    end
end

So, if successfullv filtered the text, then return the filtered text, otherwise, return something else that will be written on the name tag, it can be emptv text "". warn(err) is helpful too, this will show vou vellow text in View > Output in Studio where will be the reason as to whv the message could not filtered, that can help vou understand what's the problem.

Make sure vou are testing this in-game not in Studio because filtering does not work in Studio!!! ):<

Ad

Answer this question