Attempt to concatenate string with nil


Whole script:

    local textlabel = script.Parent
    local chatEvent = script.Parent.Parent.ChatEvent
    local Chat = game:GetService("Chat")
    local filteredmessage
    local filteredmessagetable = {}
    local event = script.Event

    local function toTable(s)
	    local t = {}
	    s:gsub(".", function(c) table.insert(t, c) return c end)
	    return t
    end

    chatEvent.Event:Connect(function(message, player)
	    textlabel.Text = ""
	    filteredmessage = ""
	    for i, v in ipairs(filteredmessagetable) do
		    table.remove(filteredmessagetable, i)
	    end
	    filteredmessage = Chat:FilterStringAsync(message, player, player)
	    filteredmessagetable = toTable(tostring(filteredmessage))
		    for index = 1, #filteredmessagetable do
		    textlabel.Text = textlabel.Text..filteredmessagetable[index]
		    wait(.02)
	    end
	    event:Fire(filteredmessage)
    end)

    event.Event:Connect(function(message)
	    wait(6)
	    if filteredmessage == message then
		    textlabel.Text = ""
	    end
    end)

This code should create a typewriter effect, and it works perfectly fine in studio. However, when I try running it in Roblox Player, it errors. I have no idea why, either.

ok 1 you can do
textlabel.Text ..= filteredmessagetable[index]
and 2 filteredmessagetable[index] = nil which is why you get an error

FilterStringAsnyc returns a TextFilterResult object, when you tostring it gets the name of the object, so it’ll always put TextFilterResult into the toTable function.

It likely is only erroring ingame is because string filtering only works ingame. A few things that could be related

  • The way you clear the filteredmessagetable, can’t you do table.clear(filteredmessagetable) instead?

  • You don’t need a function for toTable, if you’re trying to get all characters and put them in a table, just do (tostring(filteredmessage)):split(""). Also not sure why you’re converting to a string if it would already be a string

  • You’re not using FitlerStringAsync properly, I liked the TextFilterResult object it returns which has functions that allow you to get the correctly filtered text. Also, the 3rd parameter has to be a TextFilterContext Enum, not a player instance

  • I also recommend debugging with print statements to see what is happening if it’s not any of the things I mentioned