Hello DevForum! This is my first post on here, so sorry if some things are incorrect. I’m making a new bubble chat system for my game, and would like to know why Argument 2 is missing or nil. Here’s the code:
Server Script
local RS = game.ReplicatedStorage
local RF = RS.Remotes.Filter
game.Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(msg)
RF:FireClient(player,msg)
end)
end)
Local Script
local RS = game.ReplicatedStorage
local RF = RS.Remotes.Filter
local P = game.Players
local function ChatFilter(player,msg)
local PID = player.UserId
local text = game:GetService("TextService")
local filtered = text:FilterStringAsync(player, msg)
print(filtered)
end
RF.OnClientEvent:Connect(ChatFilter)
Any help would be much appreciated. Keep in mind I’m still very new to scripting so some things may not be correct.
The client is only receiving one argument. FireClient’s first argument, the player argument, is for when you want to only have a single client receive a packet. So in reality for the ChatFilter function, the msg parameter is nil and the player one has the message instead.
local function ChatFilter(player, message)
print(player, message) --> You will get a string and a nil
end
It’s like how OnServerEvent receives a player as the first parameter automatically but you’re not supposed to specify players in FireServer unless you’re interacting with another player. Rule of thumb: if you have a nil, try printing to see what your functions are getting called with.
There is documentation on this that you can read up if you want to learn more about remotes and how to use them (includes code samples and all): Remote Functions and Events
local RS = game.ReplicatedStorage
local RF = RS.Remotes.Filter
local P = game.Players
local player = P.LocalPlayer
local function ChatFilter(msg)
local PID = player.UserId
local text = game:GetService("TextService")
local filtered = text:FilterStringAsync(msg, player.UserId)
print(filtered)
end
RF.OnClientEvent:Connect(ChatFilter)
You can get the local player with “game.Players.LocalPlayer” in local scripts, the player object passed as an argument to FireClient() isn’t passed to the callback function connected to the OnClientEvent event listener.
local RS = game.ReplicatedStorage
local RF = RS.Remotes.Filter
local P = game.Players
local player = P.LocalPlayer
local function ChatFilter(msg)
local PID = player.UserId
local text = game:GetService("TextService")
local filteredRes = text:FilterStringAsync(msg, player.UserId)
local filteredMsg = filteredRes:GetNonChatStringForBroadcastAsync()
print(filteredMsg)
end
RF.OnClientEvent:Connect(ChatFilter)
Remember that FilterStringAsync() returns a TextFilterResult object through which you need to call one of the available functions in order to properly filter it.
This is the function which you call the original string on in order to receive the TextFilterResult object.
This is the TextFilterResult object class/userdata value type documentation page.
This is one of the three available functions which can be called through a TextFilterResult object in order to receive a filtered version of the original string provided.