Issue with Player Logging

I’m currently working on a PlayerLogging system so we can track user’s activity throughout the playing experience.

I’m assuming it saves but doesn’t save fully so it just deletes practically all the logs except for one

I’m pretty stumped on what I should do at this point since there isn’t much information about this on the forums.

My current setup

I’m using Cmdr Admin for my system (what I use to retrieve the logs)
then a separate system that does all the saving and logging.

Main Script

It’s not giving off any errors or warns

local API = {}
local AllPlayerLogs = {}
local DatastoreService = game:GetService("DataStoreService")
local PlayerLogs = DatastoreService:GetDataStore("PlayerLogs")
local HttpService = game:GetService("HttpService")

function API.RequestLogs(UserId)
	print(UserId)
	if AllPlayerLogs[UserId] then
		return AllPlayerLogs[UserId]
	else
		return false
	end
end

function API.AddLog(UserId, Information, Type)
	local success, errormsg = pcall(function()
		local osTime = os.time()
		AllPlayerLogs[UserId] = {}
		AllPlayerLogs[UserId][osTime] = {osTime, Information, Type}
		warn("Recieved")
	end)
	
	if not success then
		warn("Couldn't Complete: ", errormsg)
		return false
	else
		return true
	end
end

function API.SaveLogs(UserId)
	local success, errormsg = pcall(function()
		PlayerLogs:UpdateAsync(UserId, function(Array)
			return AllPlayerLogs[UserId]
		end)
	end)

	if not success then
		warn("There was an error saving "..UserId.." Data")
		warn(errormsg)
	end
end

-- API.AddLog(Player.UserId, "Hey", "Server")
function API:Initialize()
	game.Players.PlayerAdded:Connect(function(Player)
		AllPlayerLogs[Player.UserId] = {}
		local Data
		local success, errormsg = pcall(function()
			Data = PlayerLogs:GetAsync(Player.UserId)
			AllPlayerLogs[Player.UserId] = Data and Data
		end)

		if success then
			AllPlayerLogs[Player.UserId] = Data or AllPlayerLogs[Player.UserId]
		else
			warn(errormsg)
		end
		
		API.AddLog(Player.UserId, "Joined Server", "Server")
		API.SaveLogs(Player.UserId)
		
	end)

	game.Players.PlayerRemoving:Connect(function(Player)
		API.AddLog(Player.UserId, "Left Server", "Server")
		API.SaveLogs(Player.UserId)
	end)
end
return API

Cmdr Retrieving Player Logs

local PlayerLogs = require(game:GetService("ServerScriptService").ServerInit.Datalog)
local PlayerLogService = game:GetService("DataStoreService"):GetDataStore("PlayerLogs")
local Sort = require(game:GetService("ReplicatedStorage").Modules.Sort)
local Logs = {}

function Sort()
	table.sort(Logs, function(a,b)
		return a.osTime < b.osTime
	end)
end

return function (CC, PlayerId)
		CC:Reply(PlayerId.." Logs", Color3.fromRGB(255, 223, 93))
	
		local Data
		local success, errormsg = pcall(function()
			Data = PlayerLogService:GetAsync(PlayerId)
		end)
	
		if Data then
			for i, ChangeToArray in pairs(Data) do
				table.insert(Logs, {osTime = tonumber(ChangeToArray[1]), Type = ChangeToArray[3], Information = ChangeToArray[2]})
			end
			Sort()
		else
			return "User does not have logs"
		end
	
		if not success then
			warn(errormsg)
		end
	
		for i, Return in pairs(Logs) do
			local success, errormsg = pcall(function()
				CC:Reply(os.date("%A %d %b %I:%M%p %Y %Z", Return.osTime).." | "..Return.Type.." | "..Return.Information, Color3.fromRGB(255, 255, 255))
			end)
		
			if not success then
				CC:Reply("Error | Couldn't Load Data")
			end
		end
		table.clear(Logs)
	return ("Data returned for Id "..PlayerId)
end

This is what It looks like adding a new log

LoggingService.AddLog(Player.UserId, InformationSent, Type)

Visual Example

Datastore View

image

8728703052 Is my UserID
1623007588 Is the OS.time() it was added

and all the other information inside it is just used for getting data.

Isn’t that literally clearing the table?

2 Likes

I agree it seems that the table would clear everytime the user joins the game

Yeah, they should check if the table is empty or not.

Yes! I totally forgot I had added that in there. I had the system working a while back and was messing with it to fix some errors it was giving and I guess that was part of it.

image

function API.AddLog(UserId, Information, Type)
	local success, errormsg = pcall(function()
		local osTime = os.time()
		if not AllPlayerLogs[UserId] then
			AllPlayerLogs[UserId] = {}
		end
		AllPlayerLogs[UserId][osTime] = {osTime, Information, Type}
		warn("Recieved")
	end)
	
	if not success then
		warn("Couldn't Complete: ", errormsg)
		return false
	else
		return true
	end
end