LogService.MessageOut: how to know which Player an error message came from on the server?

I’m starting a function to store possible game errors in a DataStore to later analyze them and fix bugs.
For that, I’m using LogService.MessageOut
This event captures both client and server messages.
However, when on the server, the message is not accompanied by the player who cause it.

I ask: how to know which Player an error message came from on the server-side?

1 Like

Maybe detect when LogService.MessageOut happens. From the client and then send a :FireServer() and get the player from that remoteevent? I’m not familiar with LogService so sorry if I couldn’t help.

That’s because a server script does not belong to any particular client (as its name suggests it runs on the server), if you are executing code in a server script that is relevant to a particular player then I’d suggest using xpcall() as it both handles errors (similar to pcall()) and allows you to define a custom error handler.

local Game = game
local Players = Game:GetService("Players")
local DataStoreService = Game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore")

local function OnPlayerAdded(Player) --Intentionally invalid key 'GetAsync' expects a string value not a nil value.
	local Success, Result = xpcall(function() return DataStore:GetAsync() end, function() return Player.Name.." encountered an error." end)
	print(Result) --<Username> encountered an error.
end

Players.PlayerAdded:Connect(OnPlayerAdded)
2 Likes

I would not recommend putting the xpcall on one line…

For readability reasons? It’s entirely up to preference, there’s no performance difference.

It was just a friendly tip for readability :slightly_smiling_face:, it’s better practice to keep a maximum of one statement per line.

As I mentioned, it’s purely down to preference, it’s technically a multi-definition (akin to a multi-declaration) line. I personally prefer my approach as opposed to the following.

local Success, Result = xpcall(function()
	--Do code.
end, function()
	--Do other code.
end)