EDIT: I fixed the bug, I figured out I forgot to define the "player" first.

I am trying to make a custom chat, and It keeps throwing the error "Unable to cast value to Object." Here is my code:

Server Script:

game.ReplicatedStorage.ChatEvents.SendChatMessageToServer.OnServerEvent:Connect(function(player,message)
	local messageToSend = player.Name..": "..message
	print("Player "..player.Name.." sent message "..messageToSend)
	game.ReplicatedStorage.ChatEvents.SendChatMessageToClient:FireAllClients(messageToSend,player)
end)

local function filterString(message,from,to)
	local newMessage = game.Chat:FilterStringAsync(message,from,to)
	return newMessage
end

game.ReplicatedStorage.ChatEvents.FilterString.OnServerInvoke = filterString

Client Script:

script.Parent.Send.MouseButton1Click:Connect(function()
	if string.len(script.Parent.TextBox.Text) > 0 then
game.ReplicatedStorage.ChatEvents.SendChatMessageToServer:FireServer(script.Parent.TextBox.Text)
	end
end)

game.ReplicatedStorage.ChatEvents.SendChatMessageToClient.OnClientEvent:Connect(function(message,from)
	local items = 0
	for i, v in pairs(script.Parent.ScrollingFrame:GetChildren()) do
		if v.ClassName == "TextLabel" and v.Name ~= "template" then
			v.Position = UDim2.new(v.Position.X.Scale,v.Position.X.Offset,v.Position.Y.Scale,v.Position.Y.Offset-20)
		end
		items = items + 1
	end
	local newItem = script.Parent.ScrollingFrame.template:Clone()
	newItem.Parent = script.Parent.ScrollingFrame
	newItem.Name = "ClonedMessage"
	print(message)
	print(from.Name)
	print(game.Players.LocalPlayer.Name)
	local newMessage = game.ReplicatedStorage.ChatEvents.FilterString:InvokeServer(message,from,game.Players.LocalPlayer)
	newItem.Text = newMessage
	newItem.Visible = true
	script.Parent.ScrollingFrame.CanvasSize = UDim2.new(0,0,0,items*20)
	script.Parent.ScrollingFrame.CanvasPosition = Vector2.new(0,items*20)
	script.Parent.TextBox.Text = ""
end)

Note: The topbar is disabled.

Thanks!

4 comments

0

Where is it throwing the error

0

ServerScript Line 8, ClientScript 21

0

The issue is that InvokeServer, just like FireServer, passes the player who called it as the first argument. In your filterString function you are not accounting for this at all.

0

Additionally this is not at all how I would handle custom chat filtering.