I’m trying to use a RemoteFunction to filter text, but I have a little problem. I cannot get OnClientInvoke to run at all. I tried to solve it for a while, but nothing worked.
This is a LocalScript in StarterGui.
--Variables
local box = script.Parent
local filterText = game.ReplicatedStorage.FilterText
--Setting filtered text
box.FocusLost:Connect(function()
filterText:InvokeServer(box.Text)
filterText.OnClientInvoke = function(filteredText)
print("e") --This never prints
box.Text = filteredText
end
end)
And this is the server script in ServerScriptService.
--Variables
local textService = game:GetService("TextService")
local filterText = game.ReplicatedStorage.FilterText
--Filtering text
filterText.OnServerInvoke = function(player, text)
local success, filteredText = pcall(function()
return textService:FilterStringAsync(text, player.UserId)
end)
if success then
print("lol")
filterText:InvokeClient(player, filteredText.Name)
else
filterText:InvokeClient(player, "error filtering text lol sry")
end
return
end
What do you want to achieve? I want to use a RemoteFunction to filter text.
What is the issue? OnClientInvoke never runs.
What solutions have you tried so far? I looked at the Developer Hub and searched for similar posts, but I couldn’t find any answers.
OnClientInvoke is defined after the InvokeServer-call has finished, so by the time you reach :InvokeClient on the server you are still to connect OnClientInvoke on the client.
To save you for some work, simply return the error message or the filteredText.Name rather than trying to re-invoke the Client because this is already part of an invocation dialog between server and client.
To put an example to it:
-- server
remFunction.OnServerInvoke = function(player, text)
print(player, "sent the text", text)
return "Success!"
end)
-- client
local returnValue = remFunction:InvokeServer("Yarr, harr, fidelidee")
print("Server returned the value:", returnValue)
-- output:
--> "Player sent the text Yarr, harr fidelidee" (server)
--> "Server returned the value: Success!" (client)
Avoid invoking the client. Return the result directly from your script. That’s what a RemoteFunction is for: returning results upon being called. The above post should do well to explain that and provide you with an example. So, in your case, you just need to return the filtered text in place of invoking the client.
A personal recommendation: avoid having the server do too much work and let the client decide what should be done with the result of a filter. You also did not fetch the string correctly, so that’s a problem that you’re going to have to fix.
local TextService = game:GetService("TextService")
local FilterText = game:GetService("ReplicatedStorage").FilterText
FilterText.OnServerInvoke = function(player, text)
local success, result = pcall(function ()
return TextService:FilterStringAsync(text, player.UserId)
end)
if success then
-- Need a method to get filtered text, not the instance's name
result = result:GetNonChatStringForBroadcastAsync()
else
result = nil
end
return success, result
end
From the client’s end, it can use the first returned result to determine if it should show a filter error message to the player or proceed with the message. If the server pcall fails, it would not return a message (otherwise it’d be giving the client the server-side error - if that’s what you’d like, you can remove the else clause).
local success, result = FilterText:InvokeServer(textField)