String.len: attempt to compare boolean and number, however said boolean is a string?

Hello,

I’m currently trying to get the length of a string using string.len, however, Roblox thinks it’s a boolean. I did some print debugging and it looks like the said boolean is a string. I’m filtering the string using GetNonChatForBroadcastAsync(), and FilterStringAsync(). I’m not the best in the filtering department, so I did take the filter code from the DevHub. Here is my code (sorry if the formatting is messy):

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 TextFilterResult:", errorMessage)
            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 newUsernameTextObject = getTextObject(newUsername, player.UserId)

        local newUsernameToSet = getFilteredMessage(newUsernameTextObject)

        print(newUsernameToSet)

        if newUsernameToSet ~= false and typeof(newUsernameToSet) == "string" then
            if not string.find(newUsernameToSet, "#") then
                if not string.find(newUsernameToSet, " ") then
                        if not string.find(newUsernameToSet, "_") then
                            if not string.len(newUsernameToSet) < 3 then
                                if not string.len(newUsernameToSet) > 20 then
                                    player.NewPlayer.Username.Value = newUsernameToSet
                                    return "Username successfully set to " .. newUsernameToSet .. "."
                                else
                                    return "Username is more than 20 characters."
                                end
                            else
                                return "Username is less than 3 characters."
                            end
                        else
                            return "Username contains an underscore."
                        end
                else
                    return "Username contains a space."
                end
            else
                return "Username contains filtered characters."
            end
        else
            return "Username failed to filter."
        end

Thanks,
p0s_0.

The not of this expression would be:

string.len(newUserNameToSet) >= 3

You could also wrap the expression as

not (string.len(newUsernameToSet) < 3)

The thread thinks you’re using .len as a boolean

1 Like

Do this:

 if not (string.len(newUsernameToSet) < 3) then
 if not (string.len(newUsernameToSet) > 20) then
2 Likes

Another way to do what the others are mentioning is this

if #newUsernameToSet >= 3 then
if #newUsernameToSet <= 20 then

The length operator # does the same as string.len

3 Likes