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

RemoteFunction/RemoveEvent running before server is invoked/fired?

Asked by 5 years ago
Edited 5 years ago

I have a script to filter a textbox upon pressing a button.

local filteredtext = remote:InvokeServer(textbox.Text)
textbox.Text = filteredtext

The part above is causing this error: Players.hpenney2.PlayerGui.ScreenGui.Check.LocalScript:13: bad argument #3 to 'Text' (string expected, got nil) Specifically, textbox.Text = filteredtext is causing the error. This is the filter script:

local filter = game:GetService("TextService")
local repStorage = game:GetService("ReplicatedStorage")
local remote = repStorage:WaitForChild("Filter")

remote.OnServerInvoke = function(plr, unfilteredtext)
    local success, errorMessage = pcall(function()
        local filteredtext = filter:FilterStringAsync(unfilteredtext, plr.UserId)
    end)
    if not success then
        warn("Error filtering text:", unfilteredtext, ":", errorMessage)
        wait()
        local success, errorMessage = pcall(function()
            filteredtext = filter:FilterStringAsync(unfilteredtext, plr.UserId)
        end)
    elseif success then
        return filteredtext
    end
end

Something I noticed is that before the server is even invoked (the server is invoked once a button is pressed), this warn shows up in the output: Error filtering text: nil : Argument 1 missing or nil Which means the filter is trying to run before the button is even pressed, which it can’t because the argument (textbox.Text, which is, well, a TextBox’s text) isn’t passed to it until it’s invoked. How can I fix this?

EDIT: I tried adding the following code to the top of the filter script:

if unfilteredtext == nil then
    unfilteredtext = ""
end

This makes the warn go away, but the error still occurs upon pressing the button.

EDIT 2: Switching to using a RemoteEvent also does not fix this issue.

0
Make sure the TextBox contains something before the remote is fired? Azarth 3141 — 5y
0
The remote isn't fired until a button is pressed, that's the strange thing. The warn appears when the server is loaded. hpenney2 53 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

Solved! Apparently, FilterStringAsync() does NOT return a string, but rather returns a TextFilterResult. This can then be filtered with one of three functions, which I used GetNonChatStringForUserAsync() for my case.

Ad

Answer this question