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.
1 | local filteredtext = remote:InvokeServer(textbox.Text) |
2 | 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:
01 | local filter = game:GetService( "TextService" ) |
02 | local repStorage = game:GetService( "ReplicatedStorage" ) |
03 | local remote = repStorage:WaitForChild( "Filter" ) |
05 | remote.OnServerInvoke = function (plr, unfilteredtext) |
06 | local success, errorMessage = pcall ( function () |
07 | local filteredtext = filter:FilterStringAsync(unfilteredtext, plr.UserId) |
10 | warn( "Error filtering text:" , unfilteredtext, ":" , errorMessage) |
12 | local success, errorMessage = pcall ( function () |
13 | filteredtext = filter:FilterStringAsync(unfilteredtext, plr.UserId) |
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:
1 | if unfilteredtext = = nil then |
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.